-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.js
More file actions
37 lines (32 loc) · 738 Bytes
/
solution.js
File metadata and controls
37 lines (32 loc) · 738 Bytes
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
/**
* 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(head, m, n) {
if (m == n) return head
let dummy = new ListNode(-1),
pre = dummy
dummy.next = head
for (let i = 0; i < m - 1; i++) pre = pre.next
let reverse = null,
cur = pre.next
for (let i = 0; i < n - m + 1; i++) {
let next = cur.next
cur.next = reverse
reverse = cur
cur = next
}
// the tail of reverse
pre.next.next = cur
pre.next = reverse
return dummy.next
};