-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path旋转链表.cpp
81 lines (68 loc) · 1.28 KB
/
旋转链表.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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <utility>
#include <unordered_map>
#include <functional>
using namespace std;
/*
旋转链表,思路不难,先遍历一次链表,记录节点数和将尾节点与头结点相连,组成一个环。
然后计算新的头结点的位置length - k % length
最后将节点断开即可。
*/
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution
{
public:
ListNode* rotateRight(ListNode* head, int k)
{
if (head == NULL || k == 0)
return head;
ListNode* p = head;
int length = 1;
while (p->next != NULL)
{
p = p->next;
++length;
}
p->next = head;
int n = length - k%length;
p = head;
while (n != 1)
{
p = p->next;
--n;
}
ListNode* res_head = p->next;
p->next = NULL;
return res_head;
}
};
int main()
{
ListNode node1(1);
ListNode node2(2);
ListNode node3(3);
ListNode node4(4);
ListNode node5(5);
node1.next = &node2;
node2.next = &node3;
node3.next = &node4;
node4.next = &node5;
Solution a;
ListNode *head = a.rotateRight(&node1,1);
for (ListNode *p = head; p != NULL; p = p->next)
{
cout << p->val << " ";
}
cout << endl;
system("pause");
return 0;
}