/**
* 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) {
while (!head) {
return head;
}
int index = 0, cnt = 1;
ListNode* last = head, *newHead = 0;
while (last->next) {
last = last->next;
cnt++;
}
last->next = head;
k = (k % cnt);
for (int i = 0 ; i < (cnt - k); i++) {
last = last->next;
}
newHead = last->next;
last->next = NULL;
return newHead;
}
};