-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy path25-reverse-nodes-in-k-group.cpp
More file actions
229 lines (193 loc) · 6.99 KB
/
25-reverse-nodes-in-k-group.cpp
File metadata and controls
229 lines (193 loc) · 6.99 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
Problem 25: Reverse Nodes in k-Group
https://leetcode.com/problems/reverse-nodes-in-k-group/
Given the head of a linked list, reverse the nodes of the list k at a time, and return the modified list.
k is a positive integer and is less than or equal to the length of the linked list.
If the number of nodes is not a multiple of k, then the remaining nodes, in the end, should remain as they are.
You may not alter the values in the list's nodes, only nodes themselves may be changed.
Example 1:
Input: head = [1,2,3,4,5], k = 2
Output: [2,1,4,3,5]
Example 2:
Input: head = [1,2,3,4,5], k = 3
Output: [3,2,1,4,5]
Constraints:
- The number of nodes in the list is in the range [0, 5000].
- 0 <= Node.val <= 1000
- 1 <= k <= 5000
*/
#include <iostream>
#include <vector>
#include <stack>
// 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:
// Approach 1: Iterative (using a stack)
ListNode* reverseKGroupStack(ListNode* head, int k) {
if (!head || k == 1) {
return head;
}
ListNode dummy(0);
dummy.next = head;
ListNode* prev_group_tail = &dummy;
ListNode* current = head;
std::stack<ListNode*> st;
while (current) {
ListNode* group_head = current;
int count = 0;
// Check if there are k nodes available
while (current && count < k) {
st.push(current);
current = current->next;
count++;
}
if (count == k) { // If k nodes are found, reverse them
ListNode* new_group_head = st.top();
st.pop();
prev_group_tail->next = new_group_head;
prev_group_tail = new_group_head;
while (!st.empty()) {
prev_group_tail->next = st.top();
st.pop();
prev_group_tail = prev_group_tail->next;
}
prev_group_tail->next = current; // Link to the next un-reversed part
} else { // Less than k nodes, append them as they are
prev_group_tail->next = group_head;
// Clear the stack as these nodes are not reversed
while (!st.empty()) st.pop();
break; // No more groups to process
}
}
return dummy.next;
}
// Approach 2: Iterative (without a stack - direct reversal)
ListNode* reverseKGroup(ListNode* head, int k) {
if (!head || k == 1) {
return head;
}
ListNode dummy(0);
dummy.next = head;
ListNode* prev = &dummy;
ListNode* curr = head;
while (curr) {
ListNode* tail = curr;
int count = 0;
// Check if there are k nodes remaining in the current group
while (tail && count < k) {
tail = tail->next;
count++;
}
if (count == k) { // If k nodes are available
// Reverse the current k-group
ListNode* sub_prev = nullptr;
ListNode* sub_curr = curr;
for (int i = 0; i < k; ++i) {
ListNode* next_node = sub_curr->next;
sub_curr->next = sub_prev;
sub_prev = sub_curr;
sub_curr = next_node;
}
// Connect the reversed group
prev->next = sub_prev; // prev points to the new head of the reversed group
curr->next = sub_curr; // current (old head) points to the next group's head
prev = curr; // Move prev to the tail of the current reversed group
curr = sub_curr; // Move curr to the head of the next group
} else { // Less than k nodes, append them as they are and stop
prev->next = curr;
break;
}
}
return dummy.next;
}
};
// Helper function to print the list
void printList(ListNode* head) {
ListNode* current = head;
std::cout << "[";
while (current) {
std::cout << current->val;
if (current->next) {
std::cout << ",";
}
current = current->next;
}
std::cout << "]\n";
}
// Helper function to create a list from a vector
ListNode* createList(const std::vector<int>& arr) {
if (arr.empty()) {
return nullptr;
}
ListNode* head = new ListNode(arr[0]);
ListNode* current = head;
for (size_t i = 1; i < arr.size(); ++i) {
current->next = new ListNode(arr[i]);
current = current->next;
}
return head;
}
// Helper function to delete a list
void deleteList(ListNode* head) {
ListNode* current = head;
while (current) {
ListNode* next_node = current->next;
delete current;
current = next_node;
}
}
int main() {
Solution sol;
std::cout << "Testing Reverse Nodes in k-Group (Iterative with stack):\n";
// Test 1
ListNode* head1_1 = createList({1, 2, 3, 4, 5});
std::cout << "Input: [1,2,3,4,5], k = 2 -> Output: ";
ListNode* result1_1 = sol.reverseKGroupStack(head1_1, 2);
printList(result1_1); // Expected: [2,1,4,3,5]
deleteList(result1_1);
// Test 2
ListNode* head1_2 = createList({1, 2, 3, 4, 5});
std::cout << "Input: [1,2,3,4,5], k = 3 -> Output: ";
ListNode* result1_2 = sol.reverseKGroupStack(head1_2, 3);
printList(result1_2); // Expected: [3,2,1,4,5]
deleteList(result1_2);
// Test 3
ListNode* head1_3 = createList({1, 2, 3, 4, 5});
std::cout << "Input: [1,2,3,4,5], k = 1 -> Output: ";
ListNode* result1_3 = sol.reverseKGroupStack(head1_3, 1);
printList(result1_3); // Expected: [1,2,3,4,5]
deleteList(result1_3);
// Test 4
ListNode* head1_4 = createList({1, 2, 3});
std::cout << "Input: [1,2,3], k = 4 -> Output: ";
ListNode* result1_4 = sol.reverseKGroupStack(head1_4, 4);
printList(result1_4); // Expected: [1,2,3]
deleteList(result1_4);
std::cout << "\nTesting Reverse Nodes in k-Group (Iterative without stack):\n";
// Test 5
ListNode* head2_1 = createList({1, 2, 3, 4, 5});
std::cout << "Input: [1,2,3,4,5], k = 2 -> Output: ";
ListNode* result2_1 = sol.reverseKGroup(head2_1, 2);
printList(result2_1); // Expected: [2,1,4,3,5]
deleteList(result2_1);
// Test 6
ListNode* head2_2 = createList({1, 2, 3, 4, 5});
std::cout << "Input: [1,2,3,4,5], k = 3 -> Output: ";
ListNode* result2_2 = sol.reverseKGroup(head2_2, 3);
printList(result2_2); // Expected: [3,2,1,4,5]
deleteList(result2_2);
// Test 7
ListNode* head2_3 = createList({1, 2, 3});
std::cout << "Input: [1,2,3], k = 4 -> Output: ";
ListNode* result2_3 = sol.reverseKGroup(head2_3, 4);
printList(result2_3); // Expected: [1,2,3]
deleteList(result2_3);
return 0;
}