InterviewPrepKit

Home / Coding / Algorithm & Data Structure / Linked List

Reorder List

medium Original ↗ 00:00

Problem

Given the head of a singly linked list L0 → L1 → … → L(n-1), rearrange its nodes in place into the interleaved order:

L0 → L(n-1) → L1 → L(n-2) → L2 → L(n-3) → …

That is, alternate between the front of the list and the back of the list, working inward. Only node links may be changed — you may not rewrite node values. The function returns nothing; it mutates the list.

Examples

  • Input: head = [1, 2, 3, 4] → List becomes [1, 4, 2, 3] Front 1, back 4, front 2, back 3.
  • Input: head = [1, 2, 3, 4, 5] → List becomes [1, 5, 2, 4, 3] Front 1, back 5, front 2, back 4, and the middle 3 lands last.
  • Input: head = [7, 9] → List becomes [7, 9] Two nodes are already in reordered form.

Constraints

  • 1 <= n <= 5 * 10^4 where n is the number of nodes.
  • 1 <= Node.val <= 1000.

At 5·10^4 nodes, repeatedly walking to the current tail (O(n²)) is on the edge; the intended solution is O(n) time, and the classic follow-up is O(1) extra space.

Think about it first

Hint 1 The output alternates between two sequences: the first half in order, and the second half in reverse. Can you produce those two sequences?
Hint 2 With all nodes in an array, two indices (one at each end, moving inward) can relink everything. That costs O(n) space — what linked-list tools give you "second half, reversed" without an array?
Hint 3 Three classics chained together: (1) slow/fast pointers to find the middle, (2) reverse the second half in place, (3) merge the two halves alternately. Each step is a well-known routine; the problem is just their composition.

Write your solution, then hit Run tests to check it — or get a mock grade from the AI coach.

The coach remembers this session — revise your code and ask again, and it grades your progress. It gives hints, not the answer.
Report a bug