-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathreorder-list.js
58 lines (53 loc) · 1.36 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
// Given a singly linked list L: L0→L1→…→Ln-1→Ln,
// reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…
//
//
// You must do this in-place without altering the nodes' values.
//
//
// For example,
// Given {1,2,3,4}, reorder it to {1,4,2,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.
*/
var reverseList = function (head) {
if (head === null || head.next === null) return head;
var prev = new ListNode(0);
prev.next = head;
head = prev;
var cur = prev.next;
while(cur.next !== null) {
var tmp = cur.next;
cur.next = tmp.next;
tmp.next = prev.next;
prev.next = tmp;
}
return prev.next;
}
var reorderList = function(head) {
if (!head || !head.next) return;
var fast = head;
var slow = head;
while (fast && fast.next) {
fast = fast.next.next;
slow = slow.next;
}
var second = reverseList(slow.next);
slow.next = null;
var first = head;
while (second) {
var temp = second.next;
second.next = first.next;
first.next = second;
first = first.next.next;
second = temp;
}
};