-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
64 lines (60 loc) · 1.09 KB
/
Copy pathmain.js
File metadata and controls
64 lines (60 loc) · 1.09 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
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @param {number} m
* @param {number} n
* @return {ListNode}
*/
var reverseBetween = function(first, m, n) {
let pos = 0
const head = {
val: -1,
next: first
}
let pivot = head
while (pos < m - 1) {
pivot = pivot.next
pos++
}
const firstTail = pivot
const secondHead = {
val: -1,
next: pivot.next
}
pivot = pivot.next
pos++
while (pos < n) {
const neck = secondHead.next
const next = pivot.next.next
secondHead.next = pivot.next
secondHead.next.next = neck
pivot.next = next
pos++
}
firstTail.next = secondHead.next
return head.next
};
if (require.main === module) {
const head = {}
let pivot = head
for (let i = 1; i < 3; i++) {
pivot.val = i
if (i === 5) {
pivot.next = null
} else {
pivot.next = {}
}
pivot = pivot.next
}
pivot = reverseBetween(head, 1, 2)
while (pivot) {
console.log(pivot)
pivot = pivot.next
}
}