Skip to content
Decoder

Reorder List

Medium

Problem

Given the head of a linked list L0 -> L1 -> ... -> Ln, rearrange it in place into

L0 -> Ln -> L1 -> Ln-1 -> L2 -> Ln-2 -> ...

alternating one node from the front with one from the back. Change the next pointers, not the values. Return nothing; the tests look at the list starting from the original head.

Example 1

Input
1
Output
2 -> 3 -> 4 → 1 -> 4 -> 2 -> 3

Example 2

Input
1
Output
2 -> 3 -> 4 -> 5 → 1 -> 5 -> 2 -> 4 -> 3

Constraints

  • 0 <= number of nodes <= 5 * 10^4
  • 1 <= Node.val <= 1000

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.