Created
June 6, 2018 23:03
-
-
Save Siyu-Lei/3e0ebdd1a52f79c9dec439e4bd3a7131 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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