/
Solution.java
65 lines (57 loc) · 1.68 KB
/
Solution.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* Time : O(); Space: O()
* @tag : Linked List; Two Pointers
* @by : Steven Cooks
* @date: Jun 4, 2015
*************************************************************************
* Description:
*
* 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.
*
*************************************************************************
* {@link https://leetcode.com/problems/rotate-list/ }
* P.S. : dummy head + two pointers
*/
package _061_RotateList;
import com.leetcode.ListNode;
/** see test {@link _061_RotateList.SolutionTest } */
public class Solution {
public ListNode rotateRight(ListNode head, int k) {
if (head == null) {
return head;
}
// create dummy head
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode fast = dummy;
ListNode slow = dummy;
// count the length of list
int len = 0;
while (fast.next != null) {
len++;
fast = fast.next;
}
fast = dummy;
k = k % len;
if (k != 0) {
// let fast pointer itself moves k steps
for (int i = 0; i < k; i++) {
fast = fast.next;
}
// let fast and slow move together until fast reaches the last node
while (fast.next != null) {
fast = fast.next;
slow = slow.next;
}
// reconnect
fast.next = dummy.next;
dummy.next = slow.next;
slow.next = null;
}
return dummy.next;
}
}