-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0025.cpp
38 lines (38 loc) · 828 Bytes
/
0025.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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
ListNode* my_head = new ListNode(0);
ListNode* group_head = my_head;
ListNode* group_tail = my_head;
while (listLength(head) >= k) {
int times = k;
group_tail = head;
while (times -- ) {//完成一组的头插逆置
ListNode* temp = head;
head = head->next;
temp->next = group_head->next;
group_head->next = temp;
}
group_head = group_tail;
}
group_tail->next = head;
return my_head->next;
}
int listLength(ListNode* head) {
ListNode* temp = head;
int result = 0;
while (temp) {
temp = temp->next;
++ result;
}
return result;
}
};