Rotate List

Desicription

Given a list, rotate the list to the right by k places, where k is non-negative.

Example:

1
2
3
Given 1->2->3->4->5->NULL and k = 2,
return 4->5->1->2->3->NULL.

Solution

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
/**
* 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) {
int len = 0;
ListNode *tail = NULL;
for(ListNode* it = head; it; it = it->next)
len++, tail = it;
if(!len)
return NULL;
k = k % len;
int cnt = 0;
for(ListNode* it = head; it; it = it->next){
cnt++;
if(cnt == len - k){
tail->next = head;
head = it->next;
it->next = NULL;
}
}
return head;
}
};