Notice Period

Reorder List: Fold It in Half

medium

A slideshow plays photos in upload order, but the designer wants a "ping-pong" cut: first photo, last photo, second photo, second-to-last, and so on — the chain folded onto itself.

Given a list L0 → L1 → ... → Ln, rearrange it in place to L0 → Ln → L1 → Ln-1 → L2 → ... Only pointers move; node values must not be copied or swapped. Implement _reorder(head) (mutates the chain; returns nothing).

Example 1
in values = [1, 2, 3, 4]
out [1, 4, 2, 3]
Example 2
in values = [1, 2, 3, 4, 5]
out [1, 5, 2, 4, 3]
Example 3
in values = [1]
out [1]
Constraints
  • · 0 <= number of nodes <= 5 * 10^4
  • · 1 <= node value <= 1000
  • · rearrange pointers only — no value copying
  • · O(1) extra space
similar problem on LeetCode ↗
solution.pyloading python…
test results
Hit ▶ Run to test your code against the visible cases.