-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path092.js
More file actions
42 lines (38 loc) · 717 Bytes
/
092.js
File metadata and controls
42 lines (38 loc) · 717 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
38
39
40
41
42
/**
* 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 = {val: 0, next: head};
let beforeM = dummy;
while(m > 1) {
beforeM = beforeM.next;
--m;
}
let theM = beforeM.next;
let theN = dummy;
while(n > 0) {
theN = theN.next;
--n;
}
let afterN = theN.next;
theN.next = null;
while(theM) {
let temp = theM;
theM = theM.next;
temp.next = afterN;
afterN = temp;
}
beforeM.next = afterN;
return dummy.next;
};