-
Notifications
You must be signed in to change notification settings - Fork 3
/
25.cpp
40 lines (39 loc) · 1.07 KB
/
25.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
/**
* 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* recursive(ListNode* node, int k) {
ListNode* prev = nullptr;
ListNode* curr = node;
// test k
int count = 0;
while (curr && count < k) {
curr = curr->next;
count++;
}
if (count < k) return node;
curr = node;
for (int i = 0; i < k; ++i) {
ListNode* temp = curr;
ListNode* prevTemp = prev;
prev = curr;
curr = curr->next;
temp->next = prevTemp;
}
node->next = recursive(curr, k);
return prev;
}
ListNode* reverseKGroup(ListNode* head, int k) {
ListNode* sentinel = new ListNode(0, head);
sentinel->next = recursive(sentinel->next, k);
return sentinel->next;
}
};