Problems

Reorder List

medium
medium
linked-list
two-pointers
stack

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?

Execution note

Java/Go/Rust don't support this problem's ListNode/TreeNode structure yet — the starter is reference-only. Try Python or JavaScript to run and submit.

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.