Reorder L0 → L1 → … → Ln into L0 → Ln → L1 → Ln−1 → L2 → … in place.
Find the middle with slow/fast pointers, reverse the second half, then weave the two halves together.
1function reorderList(head: ListNode | null): void {2if (!head) return;3let slow = head, fast = head;4while (fast.next && fast.next.next) { slow = slow.next!; fast = fast.next.next; }5let prev: ListNode | null = null, curr = slow.next;6slow.next = null;7while (curr) { const nx = curr.next; curr.next = prev; prev = curr; curr = nx; }8let a: ListNode | null = head, b = prev;9while (b) {10const an: ListNode | null = a!.next, bn: ListNode | null = b.next;11a!.next = b; b.next = an; a = an; b = bn;12}13}
Move slow by 1 and fast by 2.
Space: play/pause · ←/→: step