reorder-list.sh — zsh
linked-listtwo-pointersstack

Reorder L0 → L1 → … → Ln into L0 → Ln → L1 → Ln-1 → … and return the head.

Constraints

  • 1 <= number of nodes <= 50000
  • 1 <= node value <= 1000

Follow-up

Can you achieve O(1) auxiliary space by reversing the second half?

Examples
Example 1
Input: head = [1,2,3,4]
Output: [1,4,2,3]
Example 2
Input: head = [1,2,3,4,5]
Output: [1,5,2,4,3]
Example 3
Input: head = [1]
Output: [1]
🔒 5 hidden
Running will execute all 8 cases, including 5 hidden ones.