forked from mehul-1607/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_61.java
31 lines (28 loc) · 1011 Bytes
/
_61.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.fishercoder.solutions;
import com.fishercoder.common.classes.ListNode;
public class _61 {
public static class Solution1 {
/**
* credit: https://discuss.leetcode.com/topic/26364/clean-java-solution-with-brief-explanation
* link the tail of the linked list to the head to form a circle, then count to find the pint and cut it
*/
public ListNode rotateRight(ListNode head, int k) {
if (head == null) {
return head;
}
ListNode copyHead = head;
int len = 1;
while (copyHead.next != null) {
copyHead = copyHead.next;
len++;
}
copyHead.next = head;//link the tail and head to make it a circle
for (int i = len - k % len; i > 1; i--) {
head = head.next;
}
copyHead = head.next;
head.next = null;//break the circle
return copyHead;
}
}
}