Skip to content

Partition List

MediumLinked List
Decode it first. Clues, constraints, pattern and plan before any code.

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.

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

Constraints

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