Partition List

Medium Topics Company Tags

Rearrange the nodes so that every value strictly less than x appears before every value greater than or equal to x. Preserve the original relative order within each of these two groups, and return the new head. Values equal to x belong to the second group. Arrays in examples and test inputs show node values from head to tail; [] represents an empty list.

Example 1:

Partition List, example 1: illustration of the example

Input: head = [3,1,4,2,3,0]
x = 3

Output: [1,2,0,3,4,3]

Explanation: Values less than 3 are 1, 2, and 0 in that order. The remaining values are 3, 4, and 3 in their original order. Join these groups to obtain 1 -> 2 -> 0 -> 3 -> 4 -> 3.

Example 2:

Partition List, example 2: illustration of the example

Input: head = [0,-2,3,-1]
x = 0

Output: [-2,-1,0,3]

Explanation: The values below 0 are -2 and -1. Place them before 0 and 3, preserving the order within both groups.

Example 3:

Partition List, example 3: illustration of the example

Input: head = []
x = 7

Output: []

Explanation: There are no nodes to partition, so the result is an empty list.

Constraints:

  • The list contains between 0 and 200 nodes.
  • -100 <= Node.val <= 100
  • -200 <= x <= 200


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.