-
-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathreorderList.js
65 lines (56 loc) · 1.38 KB
/
reorderList.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
// TC: O(n) SC: O(1)
function reorderList(head) {
let slow = head, fast = head.next;
while(fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
let secondHalf = slow.next;
let prev = slow.next = null;
//Reverse the second half of linkedlist
while(secondHalf != null) {
let temp = secondHalf.next;
secondHalf.next = prev;
prev = secondHalf;
secondHalf = temp;
}
let firstHalf = head;
secondHalf = prev;
//Re-order the full list
while(secondHalf != null) {
let temp1 = firstHalf.next, temp2 = secondHalf.next;
firstHalf.next = secondHalf;
secondHalf.next = temp1;
firstHalf = temp1;
secondHalf = temp2;
}
return head;
}
function printLinkedList(head) {
let temp = head;
while(temp != null) {
console.log(temp.value + " ");
temp = temp.next;
}
}
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
setNext(next) {
this.next = next;
}
}
let headNode1 = new Node(10);
let node2 = new Node(20);
let node3 = new Node(30);
let node4 = new Node(40);
let node5 = new Node(50);
let node6 = new Node(60);
headNode1.setNext(node2);
node2.setNext(node3);
node3.setNext(node4);
node4.setNext(node5);
node5.setNext(node6);
printLinkedList(reorderList(headNode1));