Reorder List
Reorder List is LeetCode 143 (Medium). A list reads L0 → L1 → … → Ln. Rearrange it to L0 → Ln → L1 → Ln−1 → L2 → …: first node, last node, second node, second-to-last, and so on.
- Change the links, not the values stored in the nodes.
- Nothing is returned; the list is changed in place.
- The list has at least one node and up to 5 · 10⁴, so an O(n²) search for each last node is too slow.
- The number of nodes in the list is in the range [1, 5 * 10⁴].
- 1 <= Node.val <= 1000
Intuition
The answer alternates between the front of the list and the back, and the back has to be read backwards. A singly linked list only goes forwards, so the trick is to make the back half go forwards by reversing it.
- Split: find the middle, then cut the list into a front half and a back half.
- Reverse: reverse the back half, so its first node is the original last node.
- Weave: take one node from the front, one from the reversed back, and repeat.
Each part is a standard linked-list routine, and together they use O(1) extra memory.
When an answer needs a singly linked list from both ends at once, reverse one half so both halves can be read forwards. Palindrome checks on a list use the same split.
Approach
Before reading on, decide where to split so that the front half is never shorter than the back half, for both an odd and an even length. Then plan which next pointers you must save before relinking during the weave. Aim for O(n) time and O(1) extra space.
Find the end of the front half
Start slow and fast on the head. While fast has two nodes after it, move slow one step and fast two. slow stops on the last node of the front half, so the front half gets the extra node when the length is odd.
Cut the list in two
Let second = slow.next, then set slow.next = None. Without this cut the front half still runs into the back half, and the weave builds a cycle.
Reverse the back half
Walk second with the usual three-pointer reversal: save the next node, point the current node at prev, then advance. When it ends, prev is the old last node and the back half reads from the end towards the middle.
Weave the halves
With first on the head and second on prev, save both next nodes, link first to second and second to the saved front node, then move both on. Stop when the back half runs out; the front half's leftover node is already in place.
Code in Python, C++ and Java
Common pitfalls
Forgetting to cut after the middle
second = slow.next # slow.next still points into the back half
second = slow.next slow.next = None
On [1, 2, 3, 4], node 2 still points at 3 after the back half 3 → 4 is reversed to 4 → 3. In the last weave first is 2 and second is 3, and 2's old next is also 3, so 3 gets linked to itself: a loop the judge never finishes printing.
Reading first.next after changing it
first.next = second second.next = first.next # already second
n1 = first.next first.next = second second.next = n1
Once first.next is changed, it no longer leads to the old second node of the front half. Reading it afterwards makes second point at itself, so the front half's next node must be saved first.
Edge cases
The middle loop never runs and the back half is at most one node, so the weave either does nothing or links 1 → 2, which is the same list. No early return is needed because the list is never empty.
The front half is 1 → 2 → 3 and the back half reversed is 5 → 4. The weave stops after 4, and 3 stays at the end because the cut already ended it.
Complexity
Reorder List FAQ
Can I just copy the nodes into an array?
Yes. Put every node in an array, then link from both ends inward with two indexes and set the last node's next to null. It is short and correct in O(n) time, but it uses O(n) extra memory, which interviewers usually ask you to remove.
Why reverse the back half and not the front half?
The answer starts with the original head, which belongs to the front half. Reversing the back half leaves the head where it is and puts the old last node first in the other half, which is the node the weave needs second.