Skip to content

Instantly share code, notes, and snippets.

@Siyu-Lei
Created June 6, 2018 23:03
Show Gist options
  • Save Siyu-Lei/3e0ebdd1a52f79c9dec439e4bd3a7131 to your computer and use it in GitHub Desktop.
Save Siyu-Lei/3e0ebdd1a52f79c9dec439e4bd3a7131 to your computer and use it in GitHub Desktop.
class Solution {
public ListNode rotateRight(ListNode head, int k) {
if (head == null) {
return head;
}
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode fast = dummy;
ListNode slow = dummy;
int length = 0;
while (fast.next != null) {
length++;
fast = fast.next;
}
for (int i = 0; i < length - k % length; i++) {
slow = slow.next;
}
fast.next = head;
head = slow.next;
slow.next = null;
return head;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment