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 7c55b86 commit 1b0d88aCopy full SHA for 1b0d88a
61. Rotate List
@@ -0,0 +1,30 @@
1
+class Solution {
2
+public:
3
+ int getLength(ListNode*head){
4
+ int len =0;
5
+ while(head){
6
+ ++len;
7
+ head= head->next;
8
+ }
9
+ return len;
10
11
+ ListNode* rotateRight(ListNode* head, int k) {
12
+ if(!head)return 0;
13
+ int len = getLength(head);
14
+ int actualRotateK = (k% len);
15
+ if(actualRotateK == 0)return head;
16
+ int newLastNodepos = len - actualRotateK - 1;
17
+ ListNode* newLastNode = head;
18
+ for(int i=0;i<newLastNodepos;i++){
19
+ newLastNode = newLastNode->next;
20
21
+ ListNode*newHead = newLastNode->next;
22
+ newLastNode->next = 0;
23
+ ListNode*it = newHead;
24
+ while(it->next){
25
+ it= it->next;
26
27
+ it->next = head;
28
+ return newHead;
29
30
+};
0 commit comments