# 92. 反转链表 II
var reverseBetween = function(head, left, right) {
const dummy_node = { next: null }
dummy_node.next = head
let pre = dummy_node
for (let i = 0; i < left - 1; i++) {
pre = pre.next
}
let cur = pre.next
for (let i = 0; i < right - left; i++) {
const next = cur.next
cur.next = next.next
next.next = pre.next
pre.next = next
}
return dummy_node.next
}
console.log(
reverseBetween(
{
val: 1,
next: {
val: 2,
next: {
val: 3,
next: {
val: 4,
next: {
val: 5,
next: null,
},
},
},
},
},
2,
4
)
)
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
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
← 91. 解码方法 93. 复原 IP 地址 →