-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.js
49 lines (42 loc) · 1.09 KB
/
solution.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)
* }
*/
class ListNode {
constructor(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.
*/
const reorderList = (head) => {
let slow = head, fast = head.next;
while (fast && fast.next) {
slow = slow.next;
fast = fast.next.next;
}
let secondHalf = slow.next;
slow.next = null; // This is the magical part that breaks the links
let prev = null
while (secondHalf) {
let tmp = secondHalf.next;
secondHalf.next = prev;
prev = secondHalf;
secondHalf = tmp;
}
let part2 = prev, part1 = head;
while (part2) {
let tmp1 = part1.next,
tmp2 = part2.next;
part1.next = part2; // Link to node of reversed part
part2.next = tmp1; // Link to the tmp of the next of part1
part1 = tmp1;
part2 = tmp2;
}
};