-
Notifications
You must be signed in to change notification settings - Fork 5
/
GFG_ReverseLLinGroupsOfGivenSize.cpp
179 lines (142 loc) · 3.84 KB
/
GFG_ReverseLLinGroupsOfGivenSize.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* https://practice.geeksforgeeks.org/problems/reverse-a-linked-list-in-groups-of-given-size/1
* Reverse a Linked List in groups of given size
*/
// { Driver Code Starts
#include<bits/stdc++.h>
using namespace std;
struct node
{
int data;
struct node* next;
node(int x){
data = x;
next = NULL;
}
};
/* Function to print linked list */
void printList(struct node *node)
{
while (node != NULL)
{
printf("%d ", node->data);
node = node->next;
}
printf("\n");
}
// } Driver Code Ends
/*
Reverse a linked list
The input list will have at least one element
Return the node which points to the head of the new LinkedList
Node is defined as
struct node
{
int data;
struct node* next;
node(int x){
data = x;
next = NULL;
}
}*head;
*/
class Solution
{
public:
struct node *reverse (struct node *head, int k)
{
// Complete this method
if(!head || !head->next) return head;
// return reverseLLRecursive(head, k);
return reverseLLIterative(head, k);
}//end reverse
struct node* reverseLLIterative(struct node *head, int k)
{
if(!head || !head->next) return head;
struct node* dummyHead = new struct node(0);
dummyHead->next = head;
struct node* prevGpHead = dummyHead;
struct node* nxtGpHead = head;
while(nxtGpHead)
{
struct node* prev = prevGpHead;
struct node* curr = nxtGpHead;
struct node* cnext = nullptr;
struct node* currGpOldHead = nxtGpHead;
// we don't need to check whether group of k exists or not
// reverse the group of k nodes
int countk=1;
while(curr && countk<=k)
{
cnext = curr->next;
curr->next = prev;
prev = curr;
curr = cnext;
countk++;
}
nxtGpHead = curr;
prevGpHead->next = prev; // previous group head point to current group head
currGpOldHead->next = nxtGpHead;
prevGpHead = currGpOldHead;
}// end nxtGpHead
head = dummyHead->next;
delete(dummyHead);
return head;
}// end reverseLLIterative
struct node* reverseLLRecursive(struct node *head, int k)
{
if(!head || !head->next) return head;
struct node* ptr = head;
struct node* prev = nullptr;
struct node* curr = head;
struct node* cnext = nullptr;
int countk=1;
while(curr && countk<=k)
{
cnext = curr->next;
curr->next = prev;
prev = curr;
curr = cnext;
countk++;
}
// if(cnext)
head->next = reverseLLRecursive(cnext, k); // send next group
return prev;
}// end reverseLLRecursive
};
// { Driver Code Starts.
/* Drier program to test above function*/
int main(void)
{
int t;
cin>>t;
while(t--)
{
struct node* head = NULL;
struct node* temp = NULL;
int n;
cin >> n;
for(int i=0 ; i<n ; i++)
{
int value;
cin >> value;
if(i == 0)
{
head = new node(value);
temp = head;
}
else
{
temp->next = new node(value);
temp = temp->next;
}
}
int k;
cin>>k;
Solution ob;
head = ob.reverse(head, k);
printList(head);
}
return(0);
}
// } Driver Code Ends