Skip to content
AI360Xpert
Beta
Difficulty: MediumLinked List

Reorder List

Problem in Plain English

Given the head of a singly linked list, reorder its nodes in place to alternate from the front and the back (first, last, second, second-last, and so on) without changing any node values.

Problem Statement

Given the head of an acyclic singly linked list, rearrange its nodes in place by taking the first node, the last node, the second node, the second-last node, and continuing inward until every node appears once.

Change only the next links. Do not change values or replace nodes with copies. The head remains the same node, the final tail points to null, and the procedure returns no new list.

Constraints

  • The number of nodes satisfies 1≤n≤500001 \le n \le 50000.
  • Each node value satisfies 1≤Node.val≤10001 \le \mathrm{Node.val} \le 1000.
  • The input is an acyclic singly linked list; rearrange the existing nodes and preserve their values.

Examples

Example 1

Input
head = [1,2,3,4]
Output
[1,4,2,3]

Take node 1 from the front and node 4 from the back, then take nodes 2 and 3. All four original nodes remain in the list and node 3 becomes the null-terminated tail.

Example 2

Input
head = [1,2,3,4,5]
Output
[1,5,2,4,3]

The pairs are 1 with 5 and 2 with 4. The middle node 3 has no partner, so it appears once at the tail; the first half must be at least as long as the second.

Example 3

Input
head = [7]
Output
[7]

There is only one original node. It is both head and tail, so its value and null next link stay unchanged.

Example 4

Input
head = [2,2,3,2]
Output
[2,2,2,3]

Repeated values do not identify nodes: the order uses original positions 0, 3, 1, 2. Swapping integer values would not satisfy the required node rearrangement.

Intuition

The target order needs access to both ends, but a singly linked list only walks forward. For Example 1, the front supplies 1 then 2 while the back supplies 4 then 3. An array of node references provides backward access directly; the constant-space alternative splits the list and reverses the back half, making the required back-to-front order available as a forward walk. In either method, save access to the remaining nodes before changing their links.

Approaches

Store Nodes in Array

Solution Details

Reveal Store Nodes in Array: intuition, complexity, and code

Hints

Hint 1
A singly linked list cannot step backwards, so the back-half nodes must be reached another way.
Hint 2
Reversing the second half makes its nodes arrive in exactly the order the weave needs.
Hint 3
Split at the slow pointer, reverse the back half, then alternate nodes from the two halves.

Edge Cases

  • A singleton stays the same node with a null next link; a two-node list already has the target order.
  • An odd-length list keeps one unpaired center node in the front half, which becomes the final tail.
  • An even-length list exhausts both halves together; never link the meeting array endpoint to itself.
  • Repeated values still represent distinct nodes; compare original node positions rather than deduplicating or swapping values.
  • At 50000 nodes, use iterative walks. The pointer-only solution avoids the linear reference array allocated by the first approach.

Common Mistakes and Interview Tips

  • Changing values or constructing a replacement list violates the in-place identity requirement even if the printed values match.
  • Failing to cut slow.next before reversing allows an old link between halves to create a cycle during the merge.
  • Overwriting either merge successor before saving it can lose the remaining nodes of that half.
  • Using a different midpoint convention without changing the merge logic may leave the back half longer and dereference a null first pointer.
  • For the array method, omitting the final null link leaves stale links and often a cycle. Trace odd and even lengths separately in an interview.
  • Clarify that the procedure returns nothing and keeps the caller head; it mutates the nodes themselves.

Key Takeaway

When both ends of a list are needed at once, split at the middle, reverse the back half, then walk the halves together.