-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLeetCode-61-Rotate-List.java
More file actions
129 lines (103 loc) · 3.29 KB
/
LeetCode-61-Rotate-List.java
File metadata and controls
129 lines (103 loc) · 3.29 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
LeetCode: https://leetcode.com/problems/rotate-list/
LintCode: http://www.lintcode.com/problem/rotate-list/
JiuZhang: http://www.jiuzhang.com/solutions/rotate-list/
ProgramCreek: http://www.programcreek.com/2014/02/leetcode-find-minimum-in-rotated-sorted-array/
Analysis:
Classic list operation.
1.Go the the position to cut the place
2.Rotate at the place
*/
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode rotateRight(ListNode head, int k) {
if(head == null || k < 1 || head.next == null) return head;
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode prev = dummy, curr = head;
int len = getLength(head);
k = len - k % len;
// get the place to cur the list
while(k > 0 && curr != null){
ListNode next = curr.next;
prev = curr;
curr = next;
k--;
}
// rotate
if(curr == null){
return dummy.next;
}
ListNode newHead = curr;
while(newHead.next != null){
newHead = newHead.next;
}
prev.next = null;
newHead.next = dummy.next;
return curr;
}
private int getLength(ListNode head){
ListNode curr = head;
int len = 0;
while(head != null){
head = head.next;
len++;
}
return len;
}
// public ListNode rotateRight(ListNode head, int k) {
// if (k == 0 || head == null) return head;
// if (head.next == null) return head;
// ListNode curr = head;
// int len = 0;
// ListNode last = null;
// while (curr != null) {
// len++;
// if (curr.next == null) last = curr;
// curr = curr.next;
// }
// // System.out.println("len: " + len + " K: " + k);
// k = len - k % len;
// System.out.println("len: " + len + " K: " + k);
// curr = head;
// int i = 0;
// while(curr != null && i < k - 1) {
// curr = curr.next;
// i++;
// }
// if (curr.next == null) return head;
// ListNode newHead = curr.next;
// curr.next = null;
// last.next = head;
// return newHead;
// }
public ListNode rotateRight(ListNode head, int k) {
if (head == null || k < 1) return head;
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode slow = dummy, fast = dummy;
// Step 1: Move fast to the tail, and count the numebr of elements
int len = 0;
while (fast.next != null) {
len++;
fast = fast.next;
}
// Step 2: move slow to the last element of the first group.
k = len - k % len;
for (int i = 0; i < k; i++) {
slow = slow.next;
}
// Step 3: reconnect
fast.next = dummy.next;
dummy.next = slow.next;
slow.next = null;
return dummy.next;
}
}