We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent cbcb6dc commit 42a9165Copy full SHA for 42a9165
Rotated List
@@ -0,0 +1,27 @@
1
+ // 右旋链表,这个比右旋 数组要简单的多,可以直接摘链,右旋数组的话简便的方法是可以翻转三次即可得到,不用DP
2
+
3
+ class Solution {
4
+ public:
5
+ ListNode *rotateRight(ListNode *head, int k) {
6
+ int length = 0;
7
+ ListNode *p = head;
8
+ ListNode *end =nullptr;
9
+ while(p)
10
+ {
11
+ end = p;
12
+ p = p->next;
13
+ ++length;
14
+ }
15
+ p = head;
16
+ int modeK = length == 0? 0:k % length;
17
+ if(modeK == 0 || head == nullptr)
18
+ return head;
19
+ int leftK = length - modeK;
20
+ while (--leftK) // 注意一下这里
21
+ p=p->next;
22
+ ListNode *beginNode = p->next;
23
+ p->next = nullptr;
24
+ end->next = head;
25
+ return beginNode;
26
27
+ };
0 commit comments