Skip to content

Commit 149583b

Browse files
committed
092
1 parent f5f3cca commit 149583b

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

092/main.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} head
10+
* @param {number} m
11+
* @param {number} n
12+
* @return {ListNode}
13+
*/
14+
var reverseBetween = function(first, m, n) {
15+
let pos = 0
16+
const head = {
17+
val: -1,
18+
next: first
19+
}
20+
let pivot = head
21+
while (pos < m - 1) {
22+
pivot = pivot.next
23+
pos++
24+
}
25+
26+
const firstTail = pivot
27+
const secondHead = {
28+
val: -1,
29+
next: pivot.next
30+
}
31+
pivot = pivot.next
32+
pos++
33+
while (pos < n) {
34+
const neck = secondHead.next
35+
const next = pivot.next.next
36+
secondHead.next = pivot.next
37+
secondHead.next.next = neck
38+
39+
pivot.next = next
40+
pos++
41+
}
42+
firstTail.next = secondHead.next
43+
44+
return head.next
45+
};
46+
47+
if (require.main === module) {
48+
const head = {}
49+
let pivot = head
50+
for (let i = 1; i < 3; i++) {
51+
pivot.val = i
52+
if (i === 5) {
53+
pivot.next = null
54+
} else {
55+
pivot.next = {}
56+
}
57+
pivot = pivot.next
58+
}
59+
pivot = reverseBetween(head, 1, 2)
60+
while (pivot) {
61+
console.log(pivot)
62+
pivot = pivot.next
63+
}
64+
}

0 commit comments

Comments
 (0)