Skip to content

Commit bd9fcbf

Browse files
committed
Added reorderList solution
1 parent 6b99c02 commit bd9fcbf

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

reorder-list/nhistory.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var reorderList = function (head) {
2+
if (!head) return;
3+
// Create new arr to return result and current head
4+
let arr = [];
5+
let curr = head;
6+
// Make list array that has every values from linked list
7+
let list = [];
8+
while (curr) {
9+
list.push(curr.val);
10+
curr = curr.next;
11+
}
12+
// Iterate list array if i%2 === 0 then curr.val = list[Math.floor(i / 2)]
13+
// If i%2 !== 0 them curr.val = list[list.length - Math.floor((i + 1) / 2)]
14+
curr = head;
15+
for (let i = 0; i < list.length; i++) {
16+
if (i % 2 === 0) {
17+
curr.val = list[Math.floor(i / 2)];
18+
} else {
19+
curr.val = list[list.length - Math.floor((i + 1) / 2)];
20+
}
21+
curr = curr.next;
22+
}
23+
};
24+
25+
// TC: O(n)
26+
// SC: O(n)

0 commit comments

Comments
 (0)