‹ DS&A interview · Socratic
DSA · Linked List · #147

Reorder List

Module 42 · difficulty 3/5·30:00starts on first keystroke

You are given the head of a singly linked list: `L0 -> L1 -> ... -> L(n-1) -> Ln` Reorder the nodes in place so the list becomes: `L0 -> Ln -> L1 -> L(n-1) -> L2 -> L(n-2) -> ...` You may not modify the node values; only the node links (`next` pointers) may change. Implement `reorderList(head)`. It mutates the list **in place** and returns `undefined` (no value). Each node has shape `{ val, next }`, with `next` being `null` at the tail.

Examples
  • head = [1,2,3,4] [1,4,2,3]Front and back nodes interleave: 1, 4, 2, 3.
  • head = [1,2,3,4,5] [1,5,2,4,3]Odd length: the middle node 3 ends up last.
  • head = [1] [1]Single node is already reordered.
Constraints
  • · The number of nodes is in the range [1, 5 * 10^4].
  • · 1 <= Node.val <= 1000
  • · Must reorder in place; do not allocate a new list of nodes.
  • · Node values must not be changed, only next pointers.
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.