-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path92_reverse-linked-list-ii.js
66 lines (63 loc) · 1.61 KB
/
92_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
const { printLinkedList, buildLinkedList } = require('../_utils');
/**
*
* Problem:
* Given the head of a LinkedList and two positions "p" and "q" (1-based), reverse the
* LinkedList from position "p" to "q".
* https://leetcode.com/problems/reverse-linked-list-ii/
*
*
* Time: O(q)
* Space: O(1)
*
* @param {ListNode} head
* @param {number} p
* @param {number} q
* @return {ListNode}
*/
function reverseSubLinkedList(head, p, q) {
/**
* Corner case check, if p === q, no need to do anything.
*/
if (p === q) {
return head;
}
let i = 1;
let current = head;
let prev = null;
// O(p)
while (i < p) {
// why `<`? use real example to help understand
prev = current;
current = current.next;
i++;
}
// store the node before p because we need to modify its next pointer after reversal
const copyOfNodeBeforeP = prev;
const copyOfNodeP = current;
// O(q - p)
while (i <= q) {
// why `<=`? use real example to help understand
const next = current.next;
current.next = prev;
prev = current;
current = next;
i++;
}
// `current` is the node after q
copyOfNodeP.next = current;
/**
* Corner case check: if the node before p is not existed, which means p is the
* head node, we can return `prev` (q) directly, if it's existed, we need to modify
* its next pointer to point to `prev` (q).
*/
if (copyOfNodeBeforeP) {
copyOfNodeBeforeP.next = prev;
return head;
}
return prev;
}
// Test
const head = buildLinkedList([1, 2, 3, 4, 5]);
const newHead = reverseSubLinkedList(head, 2, 4);
printLinkedList(newHead); // 1 -> 4 -> 3 -> 2 -> 5