Skip to content

Commit b1d532f

Browse files
authored
Update 92-reverse-linked-list-ii.js
1 parent dc2f955 commit b1d532f

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

92-reverse-linked-list-ii.js

+40
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,43 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} head
10+
* @param {number} left
11+
* @param {number} right
12+
* @return {ListNode}
13+
*/
14+
const reverseBetween = function(head, left, right) {
15+
if(head == null) return head
16+
const dummy = new ListNode(null, head)
17+
let num = 0, cur = head
18+
while (cur) {
19+
num++
20+
cur = cur.next
21+
}
22+
let idx = 0, pre = null
23+
cur = dummy
24+
while (idx < right && idx <= num) {
25+
if (idx === left - 1) pre = cur
26+
if (idx >= left) {
27+
const tmp = pre.next
28+
pre.next = cur.next
29+
cur.next = cur.next.next
30+
pre.next.next = tmp
31+
}
32+
33+
if (idx < left) cur = cur.next
34+
idx++
35+
}
36+
return dummy.next
37+
};
38+
39+
// another
40+
141
/**
242
* Definition for singly-linked list.
343
* function ListNode(val) {

0 commit comments

Comments
 (0)