Partition List

Given the head of a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.

You should preserve the original relative order of the nodes in each of the two partitions.

Example 1
[1] -> [4] -> [3] -> [2] -> [5] -> [2] -> null
---
[1] -> [2] -> [2] -> [4] -> [3] -> [5] -> null
Inputhead = [1,4,3,2,5,2], x = 3
Output[1,2,2,4,3,5]
Nodes with values less than 3, [1, 2, 2], come before the remaining nodes while preserving relative order within each partition.
Example 2
[2] -> [1] -> null
---
[1] -> [2] -> null
Inputhead = [2,1], x = 2
Output[1,2]
The node with value 1 is less than 2, so it is moved before the node with value 2 while preserving partition order.

Constraints

  • The number of nodes in the list is in the range [0, 200].
  • -100 <= Node.val <= 100
  • -200 <= x <= 200

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate