-
Notifications
You must be signed in to change notification settings - Fork 0
/
0092. Reverse Linked List II.js
82 lines (76 loc) · 1.61 KB
/
0092. Reverse Linked List II.js
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Reverse a linked list from position m to n. Do it in one-pass.
// Note: 1 ≤ m ≤ n ≤ length of list.
// Example:
// Input: 1->2->3->4->5->NULL, m = 2, n = 4
// Output: 1->4->3->2->5->NULL
// 1)
/**
* 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}
*/
// function ListNode(val) {
// this.val = val
// this.next = null
// }
const reverseBetween = (head, m, n) => {
if (m === n) {
return head
}
let node = new ListNode(null)
node.next = head
let pre = node
let cur = pre.next
let stack = []
let count = 1
while (m > count++) {
pre = pre.next
cur = pre.next
}
while (n + 2 > count++) {
stack.push(cur)
cur = cur.next
}
while (n - m++ >= 0) {
pre.next = stack.pop()
pre = pre.next
}
pre.next = cur
return node.next
}
// Runtime: 68 ms, faster than 10.25% of JavaScript online submissions for Reverse Linked List II.
// Memory Usage: 34.1 MB, less than 28.57% of JavaScript online submissions for Reverse Linked List II.
// Test case:
// m n
// Input : 1->2->3->4->5->NULL, m = 2, n = 4
// Output: 1->4->3->2->5->NULL
// let head1 = {
// val: 1,
// next: {
// val: 2,
// next: {
// val: 3,
// next: {
// val: 4,
// next: {
// val: 5,
// next: null
// }
// }
// }
// }
// }
// let head2 = {
// val: 1,
// next: null
// }
// console.log(reverseBetween(head1, 2, 4));
// console.log(reverseBetween(head2, 1, 1));