forked from jeantimex/javascript-problems-and-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreorder-list.js
83 lines (67 loc) · 1.43 KB
/
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
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
83
/**
* Reorder List
*
* Given a singly linked list L: L0→L1→…→Ln-1→Ln,
* reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…
*
* You may not modify the values in the list's nodes, only nodes itself may be changed.
*
* Example 1:
*
* Given 1->2->3->4, reorder it to 1->4->2->3.
* Example 2:
*
* Given 1->2->3->4->5, reorder it to 1->5->2->4->3.
*/
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {void} Do not return anything, modify head in-place instead.
*/
const reorderList = head => {
if (!head || !head.next) {
return;
}
// Step 1. Cut the original list into two halves
let prev = null;
let slow = head;
let fast = head;
while (fast && fast.next) {
prev = slow;
slow = slow.next;
fast = fast.next.next;
}
prev.next = null; // The cut
// Step 2. Reverse the 2nd half
const head2 = reverse(slow);
// Step 3. merge the two halves
return merge(head, head2);
};
const reverse = head => {
if (!head || !head.next) {
return head;
}
const next = head.next;
head.next = null;
const newHead = reverse(next);
next.next = head;
return newHead;
};
const merge = (l1, l2) => {
while (l1) {
const n1 = l1.next;
const n2 = l2.next;
l1.next = l2;
if (!n1) break;
l2.next = n1;
l1 = n1;
l2 = n2;
}
};
export { reorderList };