1. Description
Given a list, rotate the list to the right by k places, where k is non-negative.
2. Example
Given 1->2->3->4->5->NULL and k = 2,
return 4->5->1->2->3->NULL.
3. Code
public class LeetCode0061 {
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
public ListNode rotateRight(ListNode head, int k) {
if (head == null || head.next == null || k == 0) {
return head;
}
ListNode p = head;
int n = 1;
for (; p.next != null; n++) {
p = p.next;
}
ListNode lastNode = p;
k %= n;
if (k == 0) {
return head;
}
k = n - k;
ListNode q = head;
p = head.next;
for (int i = 1; p != null && i < k; i++) {
q = p;
p = p.next;
}
q.next = null;
lastNode.next = head;
return p;
}
}
Comments | NOTHING