-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathLeetCode#50.cc
More file actions
33 lines (33 loc) · 806 Bytes
/
Copy pathLeetCode#50.cc
File metadata and controls
33 lines (33 loc) · 806 Bytes
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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *rotateRight(ListNode *head, int k) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(head==NULL) return head;
int len = 0;
ListNode* tail = head;
len=1;
while(tail->next!=NULL){++len;tail=tail->next;}
k=k%len;
if(k==0) return head;
int cnt = 0;
ListNode* cur = head;
while(cur){
cnt++;
if(cnt==len-k) break;
cur = cur->next;
}
tail->next = head;
head = cur->next;
cur->next = NULL;
return head;
}
};