mediumLinked ListAmazonMeta
Fold Chain
A theatre seating list in a strip must be folded so attendees sit alternately from the front and the back.
Given a linked list `L0 → L1 → … → Ln`, reorder it in place to `L0 → Ln → L1 → Ln-1 → L2 → Ln-2 → …`.
Choose approach
1
2
3
mid
4
5
nil
1/5
Narrationstep 1 of 5
Find middle via slow/fast. Middle index = 2 (value 3).
Variables
middle=3
How to think about it
- Three classic linked-list sub-tasks composed in order.
- Finding the middle sets up the split without computing the length.
- Reversing the second half gives us a tail-to-head cursor.
- Weaving zips the two halves in place with four rewrites per step.
Edge cases to catch in interview
- Fewer than 3 nodes — nothing to do.
- Even length — first half gets the extra node.
- Clip the first half's tail to null to avoid cycles.
Examples
Example 1
[1,5,2,4,3]{"values":[1,2,3,4,5]}
Example 2
[1,4,2,3]{"values":[1,2,3,4]}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed