Skip to content
Decoder

Partition List

Medium

Problem

You get the head of a linked list and a value x. Rearrange the list so that every node with a value less than `x` comes before every node with a value greater than or equal to `x`. Within each of those two groups, keep the original relative order. Return the new head.

Example 1

Input
1
Output
4 -> 3 -> 2 -> 5 -> 2, x = 3 → 1 -> 2 -> 2 -> 4 -> 3 -> 5

Example 2

Input
2
Output
1, x = 2 → 1 -> 2

Constraints

  • 0 <= number of nodes <= 200
  • -100 <= Node.val <= 100
  • -200 <= x <= 200

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.