Reorder List
Given the head of a singly linked list L0 -> L1 -> ... -> Ln-1 -> Ln, reorder it in place to L0 -> Ln -> L1 -> Ln-1 -> L2 -> Ln-2 -> ... You may not change node values, only rearrange the nodes themselves.
Open official problem prompt ↗Rearrange the nodes into the front-back weave in place, touching each node a constant number of times.
Deal a deck by splitting it in half, flipping the bottom half over, and then dealing one card from the top half, one from the flipped half, alternating.
- Input
- head = [1, 2, 3, 4]
- Output
- [1, 4, 2, 3]
- Why
- The list is woven from both ends inward: first node, then last, then second, then second-to-last.
The number of nodes is in the range [1, 5 * 10^4]1 <= Node.val <= 1000