Rotate List
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.
public ListNode rotateRight(ListNode head, int k) {
if (head == null || head.next == null) return head;
ListNode fast = head, slow = head, dummyHead = head;
int len = 0;
while (fast.next != null){
fast = fast.next;
if (++len > k)
slow = slow.next;
}
len ++;
if(k >= len){
k %= len;
int offset = len - k;
while (-- offset > 0)
slow = slow.next;
}
if(slow.next != null){
dummyHead = slow.next;
slow.next = null;
fast.next = head;
}
return dummyHead;
}