Skip to content

Commit 5d86959

Browse files
committed
reorder-list solution
1 parent 4156619 commit 5d86959

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

reorder-list/lhc0506.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} head
10+
* @return {void} Do not return anything, modify head in-place instead.
11+
*/
12+
var reorderList = function(head) {
13+
const nodes = {};
14+
let currentNode = head;
15+
let i = 0;
16+
while (currentNode) {
17+
nodes[i] = currentNode;
18+
currentNode = currentNode.next;
19+
i++;
20+
}
21+
22+
i--;
23+
24+
for (let j = 0; j < (i / 2); j++) {
25+
nodes[j].next = nodes[i - j];
26+
nodes[i - j].next = nodes[j + 1];
27+
console.log(j)
28+
}
29+
30+
nodes[Math.ceil(i / 2)].next = null;
31+
};
32+
33+
// 시간 복잡도: O(n)
34+
// 공간 복잡도: O(n)

0 commit comments

Comments
 (0)