forked from pengsida/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode25.cpp
43 lines (41 loc) · 1.04 KB
/
leetcode25.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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool gather(vector<ListNode*>& rec, ListNode* cur, int k) {
int i = 0;
for (i = 0; i < k; i++) {
if (cur == NULL)
break;
rec[i] = cur;
cur = cur->next;
}
return i == k;
}
void reverse(vector<ListNode*>& rec, int k) {
ListNode* tmp = rec[k - 1]->next;
for (int i = k - 1; i >= 1; i--)
rec[i]->next = rec[i - 1];
rec[0]->next = tmp;
}
ListNode* reverseKGroup(ListNode* head, int k) {
if (head == NULL)
return NULL;
vector<ListNode*> rec(k, NULL);
ListNode** cur = &head;
while (1) {
if (!gather(rec, *cur, k))
break;
reverse(rec, k);
*cur = rec.back();
cur = &(rec.front()->next);
}
return head;
}
};