Skip to content

visual walkthrough

Reorder List

MediumMiddle + Reverse + MergeReported at: AmazonMicrosoftMeta+4

Chains three core routines together; a great consolidation problem.

Solve on LeetCode

The idea

The order L0, Ln, L1, Ln−1, … needs the back of the list, which a singly linked list can only reach by walking.

Split it in half, reverse the back half so its end comes first, then weave the two halves together node by node.

Complexity

approachtimespace
Array of nodesO(n)O(n)
Middle, reverse, weaveO(n)O(1)

More walkthroughs