-
Notifications
You must be signed in to change notification settings - Fork 1
/
KReverse.cpp
95 lines (78 loc) · 1.74 KB
/
KReverse.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
92
93
94
95
#include <iostream>
using namespace std;
struct Node
{
int value;
Node* next;
Node(int x) : value(x),next(nullptr) {}
};
//反转指定范围内的节点
pair<Node*,Node*> rev(Node* head,Node* tail)
{
Node* tH = tail;
Node* tT = head;
Node* cmp = tail -> next;
Node* pre = head;
Node* cur = head -> next;
head -> next = nullptr;
while(cur != cmp)
{
Node* next = cur -> next;
cur -> next = pre;
pre = cur;
cur = next;
}
return {tH,tT};
}
Node* KReverse(Node* head,int k)
{
int K = k;
Node* cur = head;
while(cur != nullptr && --k > 0)
{
cur = cur -> next;
}
//链表节点数不足K个
// if(cur == nullptr)
// return head;
if(k > 0)
return head;
if(cur -> next == nullptr)
{
auto it = rev(head,cur);
return it.first;
}
//Node* next = KReverse(cur -> next,k); --------致命错误,k发生了变化
Node* next = KReverse(cur -> next, K);
auto it = rev(head,cur);
it.second -> next = next;
return it.first;
}
void printL(Node *l){
Node *p=l;
while(p){
cout<<p->value<<' ';
p = p->next;
}
cout<<'\n';
}
int main()
{
Node* head = new Node(1);
head -> next = new Node(2);
head -> next -> next = new Node(3);
head -> next -> next -> next = new Node(4);
head -> next -> next -> next -> next = new Node(5);
//head -> next -> next -> next -> next -> next = nullptr;
// auto it = rev(head, head -> next -> next -> next);
// Node* cur = it.first;
// printL(cur);
Node* res = KReverse(head,2);
while(res != nullptr)
{
cout << res -> value << endl;
res = res -> next;
}
system("pause");
return 0;
}