-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLC_25_ReverseNodesInKGroup.cpp
115 lines (91 loc) · 3.52 KB
/
LC_25_ReverseNodesInKGroup.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
/*
* https://leetcode.com/problems/reverse-nodes-in-k-group/
* 25. Reverse Nodes in k-Group
*/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
if(!head || !head->next) return head;
// return reverseKGroupRecursive(head,k);
return reverseKGroupIterative(head,k);
}//end reverseKGroup
ListNode* reverseKGroupIterative(ListNode* head, int k) {
if(!head || !head->next) return head;
ListNode* dummyHead = new ListNode(0);
dummyHead->next = head;
ListNode* prevGpHead = dummyHead; //previous Group Head
ListNode* nxtGpHead = head; // Next Group Head
while(nxtGpHead)
{
ListNode* prev = prevGpHead;
ListNode* curr = nxtGpHead; // currently it is head of current list
ListNode* cnext = nullptr;
ListNode* currGpOldHead = nxtGpHead;
// first we need to check whether group of k exists or not
int ck = k;
while(nxtGpHead && ck)
{
ck--;
nxtGpHead = nxtGpHead->next;
} //now it is head of next group
if(ck==0) // if group of k nodes exists
{
int countk=1;
while(curr && countk<=k)
{
cnext = curr->next;
curr->next = prev;
prev = curr;
curr = cnext;
countk++;
}
// prev is now current Group New Head
prevGpHead->next = prev; // previous group head now points to newHead of current list
prevGpHead = currGpOldHead; // update previous Gp Head to current list
currGpOldHead->next = nxtGpHead; // update current group old head to next group un updated head
//it is required in case if next group is not of k size then we still have valid pointers
}
}// outer while loop nxtGpHead
head = dummyHead->next;
delete (dummyHead);
return head;
}//end reverseKGroupIterative
ListNode* reverseKGroupRecursive(ListNode* head, int k) {
// if empty LL or single node, BASE CASE
if(!head || !head->next) return head;
// first we need to check whether group of k exists or not
ListNode *ptr = head;
for(int i=1; i<=k ; i++)
{
if(!ptr) return head; // return LL as it is without reversing
ptr = ptr->next; // then update the pointer to next otherwise Test case of k==count(LL) will not pass
}
// if group of k nodes exists, reverse the group of nodes
ListNode* prev = nullptr;
ListNode* curr = head;
ListNode* cnext = nullptr;
int countk=1;
while(curr && countk<=k)
{
cnext = curr->next;
curr->next = prev;
prev = curr;
curr = cnext;
countk++;
}
// if(cnext)
head->next = reverseKGroupRecursive(cnext, k);
return prev;
}//end reverseKGroupRecursive
};
};