forked from TonnyL/Windary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RotateList.js
100 lines (86 loc) · 2.1 KB
/
RotateList.js
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
/**
* Given a list, rotate the list to the right by k places, where k is non-negative.
*
* Example:
*
* Given 1->2->3->4->5->NULL and k = 2,
*
* return 4->5->1->2->3->NULL.
*
* Accepted.
*/
function ListNode(val) {
this.val = val;
this.next = null;
this.equals = function (node) {
return this.next == null && node.next == null || this.val === node.val && (this.next != null) && this.next.equals(node.next);
}
}
/**
* @param {ListNode} head
* @param {number} k
* @return {ListNode}
*/
let rotateRight = function (head, k) {
if (head == null || head.next == null) {
return head;
}
let node = head, length = 1;
while (node.next != null) {
length++;
node = node.next;
}
node.next = head; // Form a circle
k %= length;
for (let i = 0; i < length - k; i++) {
node = node.next;
}
head = node.next;
node.next = null;
return head;
};
if (rotateRight(null, 1) === null) {
console.log("pass")
} else {
console.error("failed")
}
let tmp = new ListNode(1);
if (rotateRight(new ListNode(1), 1).equals(tmp)) {
console.log("pass")
} else {
console.error("failed")
}
let node12 = new ListNode(1);
node12.next = new ListNode(2);
tmp.next = new ListNode(2);
if (rotateRight(node12, 0).equals(tmp)) {
console.log("pass")
} else {
console.error("failed")
}
let node123 = new ListNode(1);
node123.next = new ListNode(2);
node123.next.next = new ListNode(3);
tmp = new ListNode(3);
tmp.next = new ListNode(1);
tmp.next.next = new ListNode(2);
if (rotateRight(node123, 1).equals(tmp)) {
console.log("pass")
} else {
console.error("failed")
}
let node12345 = new ListNode(1);
node12345.next = new ListNode(2);
node12345.next.next = new ListNode(3);
node12345.next.next.next = new ListNode(4);
node12345.next.next.next.next = new ListNode(5);
tmp = new ListNode(4);
tmp.next = new ListNode(5);
tmp.next.next = new ListNode(1);
tmp.next.next.next = new ListNode(2);
tmp.next.next.next.next = new ListNode(3);
if (rotateRight(node12345, 2).equals(tmp)) {
console.log("pass")
} else {
console.error("failed")
}