-
Notifications
You must be signed in to change notification settings - Fork 0
/
25-ReverseNodesInK-Group.cpp
85 lines (81 loc) · 2.57 KB
/
25-ReverseNodesInK-Group.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
/*=============================================================================
# FileName: 25-ReverseNodesInK-Group.cpp
# Desc: AC, 25ms
# Author: Jian Huang
# Email: [email protected]
# HomePage: https://cn.linkedin.com/in/huangjian1993
# Version: 0.0.1
# LastChange: 2015-08-03 20:06:02
# History:
=============================================================================*/
#include <leetcode.h>
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode *reverseList(ListNode *head) {
if (!head || !head->next) {
return head;
}
ListNode *prev = NULL, *current = head, *next = head->next;
while (next) {
current->next = prev;
prev = current;
current = next;
next = next->next;
}
current->next = prev;
return current;
}
ListNode* reverseKGroup(ListNode* head, int k) {
if (!head || !head->next || k == 1) {
return head;
}
ListNode *start = head, *end = NULL, *nHead = NULL, *nTail = NULL;
while (true) {
end = start;
for (int i = 1; i < k; i ++) {
if (end && end->next) {
end = end->next;
} else {
if (!nTail) {
return head;
}
nTail->next = start;
return nHead;
}
}
ListNode *tmp = end->next;
end->next = NULL;
if (nHead == NULL) {
nHead = reverseList(start);
nTail = nHead;
} else {
nTail->next = reverseList(start);
}
start = tmp;
while (nTail->next) {
nTail = nTail->next;
}
}
return nHead;
}
};
int main() {
ListNode *node1 = new ListNode(1), *node2 = new ListNode(2), *node3 = new ListNode(3), *node4 = new ListNode(4), *node5 = new ListNode(5);
node1->next = node2;
node2->next = node3;
node3->next = node4;
node4->next = node5;
Solution solution;
ListNode *head = solution.reverseKGroup(node1, 3);
while (head) {
cout << head->val << " ";
head = head->next;
}
cout << endl;
return 0;
}