-
Notifications
You must be signed in to change notification settings - Fork 20
/
RotateList.java
59 lines (47 loc) · 1.23 KB
/
RotateList.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
/**
* Given a list, rotate the list to the right by k places, where k is non-negative.
* <p>
* Example:
* <p>
* Given 1->2->3->4->5->NULL and k = 2,
* <p>
* return 4->5->1->2->3->NULL.
* <p>
* Accepted.
*/
public class RotateList {
public ListNode rotateRight(ListNode head, int k) {
if (head == null || head.next == null) {
return head;
}
ListNode node = head;
int length = 1;
while (node.next != null) {
length++;
node = node.next;
}
node.next = head; // Form a circle
k %= length;
for (int i = 0; i < length - k; i++) {
node = node.next;
}
head = node.next;
node.next = null;
return head;
}
public static class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof ListNode) {
ListNode node = (ListNode) obj;
return this.next == null && node.next == null || this.val == node.val && (this.next != null) && this.next.equals(node.next);
}
return false;
}
}
}