-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0143--reorder-list.js
49 lines (44 loc) · 1.27 KB
/
0143--reorder-list.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
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {void} Do not return anything, modify head in-place instead.
*/
var reorderList = function (head) {
// find the start of second half of the list
let tailOfFirstHalf = head;
let tail = head?.next;
while (tail?.next) {
// when the fast pointer reaches the tail, slow pointer would point at the tail of the first half
tailOfFirstHalf = tailOfFirstHalf.next;
tail = tail.next.next;
}
let headOfSecondHalf = reverse(tailOfFirstHalf.next);
tailOfFirstHalf.next = null;
while (head && headOfSecondHalf) {
// reorder by mixing head and tail nodes
const headNext = head.next;
head.next = headOfSecondHalf;
const headOfSecondHalfNext = headOfSecondHalf.next;
headOfSecondHalf.next = headNext;
// update the pointers
head = headNext;
headOfSecondHalf = headOfSecondHalfNext;
}
function reverse(head) {
let previous = null;
let current = head;
while (current) {
const next = current.next;
current.next = previous;
previous = current;
current = next;
}
return previous;
}
};