Search for a command to run...
Initialization
odd starts at the head, even at the second node, and firstEven remembers where the even chain begins so it can be reattached at the end.
Two Chains, Relinked
1function oddEvenList(head) {2 if (!head || !head.next) return head;3 4 let odd = head;5 let even = head.next;//length: 66 7 // Remember where the even chain starts8 const firstEven = head.next;9 10 while (even && even.next) {11 // Each pointer skips over the other chain's node12 odd.next = odd.next.next;13 even.next = even.next.next;14 odd = odd.next;15 even = even.next;16 }17 18 // Stitch the even chain onto the end of the odd one19 odd.next = firstEven;20 21 return head;22}