Find the first-half tail, reverse the detached second half, then splice one reversed node after each first-half node.
Problem trace
Split and terminate before reversing and weaving the two halves
Read it this way:Split after node 3 and reverse the detached second half. Alternating the detached halves yields (1,5,2,4,3) without a cycle. What old link must be removed before alternating the two halves?
Sources and provenance
Full interview answer 14 min read
Rearrange a linked list into alternating nodes from its beginning and end without replacing its nodes.
Problem
Input. The head of a finite acyclic integer linked list.
Output. Rewire the same nodes in place into first, last, second, second-last order, continuing inward; return None.
Rules. Use every original node exactly once, retain its value, and end the reordered list with None.
Do not create a replacement output chain or merely exchange node values.
Values may repeat and need not be sorted. Empty and singleton lists remain unchanged.
Finish when the second half is emptyThe merge guard fails at second = null. The in-place list is 1->5->2->4->3->null.
1→5→2→4→3
result1->5->2->4->3
result
not shown→1->5->2->4->3
saved
first_next=3; second_next=null→not shown
second pointer
null→not shown
Recognize it
Use it when nodes must alternate from the front and back of a singly linked list without an auxiliary array or stack.
Keep true
The midpoint scan keeps fast twice as far along; reversal preserves a reversed prefix plus untouched suffix; merging preserves the final alternating prefix and two unconsumed chains.
Reuse it
Complex list transforms become safe compositions of small invariants: locate a boundary, detach, reverse with a saved route, then splice only after saving both continuations.
For 1->2->3->4->5, set slow = 1 and fast = 1. Both fast.next and fast.next.next exist. Step through the frames to watch the state change. The last frame shows the answer or the stopping condition.
Check: Why must 3.next become null before weaving? Where does the odd middle node end up?
Reference drawing
Problem trace
Split and terminate before reversing and weaving the two halves
Execution trace
Input and goalChange `1, 2, 3, 4, 5` into `1, 5, 2, 4, 3`.
Initialize middle pointersFor 1->2->3->4->5, set slow = 1 and fast = 1. Both fast.next and fast.next.next exist.
1slow / fast→2→3→4→5
guardfast.next=2 and fast.next.next=3
guard
fast.next=2 and fast.next.next=3
node 0
1
node 1
2
Advance middle pointers onceMove slow one link 1->2 and fast two links 1->2->3. The loop guard still passes at fast = 3.
1→2slow→3fast→4→5
movementslow: 1->2; fast: 1->2->3
movement
not shown→slow: 1->2; fast: 1->2->3
guard
fast.next=2 and fast.next.next=3→not shown
Advance middle pointers twiceMove slow 2->3 and fast 3->4->5. At fast = 5, fast.next is null, so the middle loop stops.
1→2→3slow→4→5fast
movementslow: 2->3; fast: 3->4->5guardfast.next is null
Finish when the second half is emptyThe merge guard fails at second = null. The in-place list is 1->5->2->4->3->null.
1→5→2→4→3
result1->5->2->4->3
result
not shown→1->5->2->4->3
saved
first_next=3; second_next=null→not shown
second pointer
null→not shown
Recognize it
Use it when nodes must alternate from the front and back of a singly linked list without an auxiliary array or stack.
Keep true
The midpoint scan keeps fast twice as far along; reversal preserves a reversed prefix plus untouched suffix; merging preserves the final alternating prefix and two unconsumed chains.
Reuse it
Complex list transforms become safe compositions of small invariants: locate a boundary, detach, reverse with a saved route, then splice only after saving both continuations.
For 1->2->3->4->5, set slow = 1 and fast = 1. Both fast.next and fast.next.next exist. Step through the frames to watch the state change. The last frame shows the answer or the stopping condition.
Check: Why must 3.next become null before weaving? Where does the odd middle node end up?
Read it this way:Split after node 3 and reverse the detached second half. Alternating the detached halves yields (1,5,2,4,3) without a cycle. What old link must be removed before alternating the two halves?
The slow pointer leaves the first half at least as long as the second.
Detachment prevents an old middle link from forming a cycle.
Save both successors before each splice, so every unprocessed node remains reachable.
Code
Define the platform node type before the unchanged reference.
class ListNode: def __init__(self, val: int = 0, next_node: "ListNode | None" = None): self.val = val self.next = next_node
def reorder_list(head: ListNode | None) -> None: if head is None or head.next is None: return slow = head fast = head while fast.next and fast.next.next: slow = slow.next fast = fast.next.next second = slow.next slow.next = None previous = None while second: next_node = second.next second.next = previous previous = second second = next_node first = head second = previous while second: first_next = first.next second_next = second.next first.next = second second.next = first_next first = first_next second = second_next
Cost:O(n) time and O(1) space.
The platform supplies ListNode with val and next; this snippet assumes that definition.
Change the Requirement
Preserve the Input
New task: Return a reordered copy without changing original links.
Picture and change: Copy in original order, then reuse the complete reference on the copy.
Check:1->2->3 yields fresh 1->3->2 nodes while the input stays 1->2->3.
Use when: External references require the original topology; result allocation becomes O(n).
Undo the Reordering
New task: Recover the original order after the stated transformation.
Picture and change: Keep alternating first-half nodes; prepend alternating second-half nodes, then join both chains.
Code: No node allocation is required.
def undo_reorder(head: ListNode | None) -> None: first = head second_reversed = None while first and first.next: second = first.next first.next = second.next second.next = second_reversed second_reversed = second if first.next is None: break first = first.next if first: first.next = second_reversed
Check:1->5->2->4->3 returns to 1->2->3->4->5.
Use when: The input was produced by the forward transformation.
Time remains linear and workspace constant.
Reconstruct
What happens if slow.next = None is omitted?
Reasoned answer
For 1->2->3, node two retains its old link to node three.
The alternating splice then links node three back to node two, forming a cycle.
Reversal alone does not establish disjoint ownership of the two chains.
Detaching is part of the proof, not optional cleanup.