We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 6b99c02 commit bd9fcbfCopy full SHA for bd9fcbf
reorder-list/nhistory.js
@@ -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
22
23
+};
24
+
25
+// TC: O(n)
26
+// SC: O(n)
0 commit comments