-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReverseNodesInKGroup.cpp
91 lines (89 loc) · 2 KB
/
ReverseNodesInKGroup.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
86
87
88
89
90
91
#include<iostream>
#include<vector>
using namespace std;
struct ListNode{
int val;
ListNode *next;
ListNode(int x): val(x), next(NULL){}
};
ListNode* reverseKGroup(ListNode* head, int k) {
if(k <= 1 || head == NULL ){
return head;
}
ListNode* newHead = NULL, *tmp, *t, *t1;
vector<ListNode*> sk;
t1 = head;
int flg = 0;
while(true){
//push cur k node into vector
int i = 0;
sk.clear();
while(i < k ){
if (t1 != NULL){
sk.push_back(t1);
t1 = t1->next;
i++;
}else{
break;
}
}
if(i < k){
if(sk.size() > 0){ //the left out nodes
if(newHead == NULL){
newHead = sk[0];
t = newHead;
}else{
t->next = sk[0];
}
flg = 1;
}
break;
}
//reverse
for(i = k-1; i >= 0; --i ){
if(newHead == NULL){
newHead = sk[i];
t = newHead;
}else{
t->next = sk[i];
t = t->next;
}
}
}
//这个地方需要注意,很容易出现无限链表
if(flg == 0) {
t->next = NULL;
}
if(newHead == NULL){
return head;
}
return newHead;
}
ListNode* construct(int a[], int len ){
ListNode* res = NULL, *tmp = NULL;
for(int i = 0; i < len; ++i){
ListNode* t = new ListNode(i);
if(res == NULL){
res = t;
tmp = t;
}else{
tmp->next = t;
tmp = tmp->next;
}
}
return res;
}
void print(ListNode * a){
while(a != NULL){
std::cout<<a->val<< ",";
a = a->next;
}
std::cout<<std::endl;
}
int main(){
int a[] = {1,2};
ListNode *head = construct(a, 2);
print(head);
ListNode * res=reverseKGroup(head, 2);
print(res);
}