-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRotateListbyKplaces.java
More file actions
76 lines (63 loc) · 2.23 KB
/
Copy pathRotateListbyKplaces.java
File metadata and controls
76 lines (63 loc) · 2.23 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
import util.ListNode;
public class RotateListbyKplaces {
// sol 1
public ListNode rotateKplace(ListNode head, int k) { // TC: O(n + n - k), SC: O(1)
if (head == null) return null;
ListNode cur = head;
int len = 1;
while (cur.next != null) {
cur = cur.next;
len++;
}
cur.next = head;
for (int i = 0; i < len - (k %= len); i++) cur = cur.next;
head = cur.next;
cur.next = null;
return head;
}
// sol1 ends here
// sol 2 slightly optimized
// When k > n, TC for this solution is same as solution 1
// when k < n,TC: k + 2 * (n - k) (cause we're moving 2 pointers) 2n - k, it's the same...
// SC: O(1)
public ListNode rotateKplace2(ListNode head, int k) {
if (head == null) return null;
ListNode cur = head;
int len = 1;
for (int i = 0; i < k && cur.next != null; i++) {
cur = cur.next;
len++;
}
// this means k is not longer than length of list, then we can return faster using two pointers
if (cur.next != null) return faster(head, cur);
cur.next = head;
for (int i = 0; i < len - (k %= len); i++) cur = cur.next;
head = cur.next;
cur.next = null;
return head;
}
private ListNode faster(ListNode head, ListNode fast) {
ListNode slow = head;
while (fast.next != null) {
slow = slow.next;
fast = fast.next;
}
fast.next = head;
head = slow.next;
slow.next = null;
return head;
}
// sol 2 ends here
public static void main(String[] args) {
ListNode root = ListNode.fromArray(new int[] {1, 2, 3, 4, 5});
RotateListbyKplaces sol = new RotateListbyKplaces();
System.out.println(root);
System.out.println(root = sol.rotateKplace(root, 2));
System.out.println(root = sol.rotateKplace(root, 3));
System.out.println();
System.out.println(root = sol.rotateKplace(root, 2));
System.out.println(root = sol.rotateKplace(root, 3));
System.out.println(root = sol.rotateKplace(root, 13));
System.out.println(root = sol.rotateKplace(root, 12));
}
}