Given a list, rotate the list to the right by k places, where k is non-negative.
For example:
Given 1->2->3->4->5->NULL
and k = 2
,
return 4->5->1->2->3->NULL
.
class Solution { public: ListNode *rotateRight(ListNode *head, int k) { if(!head) return head; ListNode *p = head; ListNode *newHead; int num = 1; while(p->next) { p = p->next; num++; } k = k%num; if(k==0) return head; p->next = head; p = head; for(int i = 0; i<num-k-1; i++) { p = p->next; } head = p->next; p->next = NULL; return head; } };