Bird
Raised Fist0
Interview Prepfast-slow-pointersmediumAmazonMicrosoftFacebook

Reorder List (L0→Ln→L1→Ln-1)

Choose your preparation mode3 modes available
Steps
setup

Initialize pointers and start recursion

Set 'left' pointer to head (node 1) and 'stop' flag to False. Begin recursive helper function with 'right' at head (node 1).

💡 Initializing 'left' and 'stop' prepares for the recursive traversal from the front and back simultaneously.
Line:left = head stop = False helper(head)
💡 The recursion will explore to the end of the list with 'right' while 'left' stays at the front initially.
📊
Reorder List (L0→Ln→L1→Ln-1) - Watch the Algorithm Execute, Step by Step
Watching each pointer move and link change helps you understand how recursion unwinds and how nodes are reordered without extra space.
Step 1/12
·Active fillAnswer cell
advance
1
2
3
4
advance
1
2
3
4
advance
1
2
3
4
advance
1
2
3
4
connect
1
2
3
4
compare
1
2
3
4
detach
1
2
3
4
none
1
2
3
4
traverse
1
2
3
4
Result: [1]
traverse
1
2
3
4
Result: [1, 4]
traverse
1
2
3
4
Result: [1, 4, 2]
traverse
1
2
3
4
Result: [1, 4, 2, 3]

Key Takeaways

The recursive approach uses a front pointer and a back pointer moving inward simultaneously to reorder the list in place.

This insight is hard to see from code alone because recursion hides the back pointer movement in the call stack.

Stopping conditions prevent cycles by detecting when pointers meet or cross in the middle of the list.

Understanding when and why to stop is clearer when watching the pointers and links change step-by-step.

The reordering rewires next pointers alternately from front and back nodes, preserving list integrity without extra space.

Seeing the exact pointer updates helps grasp how the list is reconstructed without losing nodes.