forked from lazzzis/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
75 lines (73 loc) · 1.68 KB
/
main.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
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @param {number} k
* @return {ListNode}
*/
var rotateRight = function(head, k) {
const dump = new ListNode(-1)
dump.next = head
let fast = dump
let slow = dump
let i = 0
while (i < k && fast.next != null) {
fast = fast.next
i++
}
if (i !== k || fast.next == null) { // k >= length
k = k % i
for (let j = 0; j < i - k; j++) {
slow = slow.next
}
} else {
while (fast.next != null) {
fast = fast.next
slow = slow.next
}
}
fast.next = dump.next
dump.next = slow.next
slow.next = null
return dump.next
};
function ListNode(val) {
this.val = val;
this.next = null;
}
if (process.env.LZS) {
const { arrToList, listToArr } = require('../utils')
const assert = require('chai').assert
assert.deepStrictEqual(
listToArr(rotateRight(arrToList([1, 2, 3]), 2)), [2, 3, 1]
)
assert.deepStrictEqual(
listToArr(rotateRight(arrToList([1, 2, 3, 4, 5]), 2)), [4, 5, 1, 2, 3]
)
assert.deepStrictEqual(
listToArr(rotateRight(arrToList([1, 2, 3]), 5)), [2, 3, 1]
)
assert.deepStrictEqual(
listToArr(rotateRight(arrToList([1]), 2)), [1]
)
assert.deepStrictEqual(
listToArr(rotateRight(arrToList([1]), 11)), [1]
)
assert.deepStrictEqual(
listToArr(rotateRight(arrToList([]), 11)), []
)
assert.deepStrictEqual(
listToArr(rotateRight(arrToList([1, 2]), 11)), [2, 1]
)
assert.deepStrictEqual(
listToArr(rotateRight(arrToList([1, 2]), 110)), [1, 2]
)
assert.deepStrictEqual(
listToArr(rotateRight(arrToList([1]), 1)), [1]
)
}