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 4465054 commit f78cad2Copy full SHA for f78cad2
Rotate List.cpp
@@ -0,0 +1,41 @@
1
+/**
2
+ * Definition for singly-linked list.
3
+ * struct ListNode {
4
+ * int val;
5
+ * ListNode *next;
6
+ * ListNode(int x) : val(x), next(NULL) {}
7
+ * };
8
+ */
9
+class Solution {
10
+public:
11
+ ListNode *rotateRight(ListNode *head, int k) {
12
+ // Start typing your C/C++ solution below
13
+ // DO NOT write int main() function
14
+ ListNode *ptr=head,*pre;
15
+ int len=0;
16
+ while (ptr)
17
+ {
18
+ ++len;
19
+ ptr=ptr->next;
20
+ }
21
+ if (!len)return head;
22
+ k%=len;
23
+ if (!k)return head;
24
+ int c=len-k;
25
+ pre=ptr=head;
26
+ while (c--)
27
28
+ pre=ptr;
29
30
31
+ pre->next=NULL;
32
+ ListNode *ed=ptr;
33
+ while (ed)
34
35
+ pre=ed;
36
+ ed=ed->next;
37
38
+ pre->next=head;
39
+ return ptr;
40
41
+};
0 commit comments