FastPrepPartition List

Partition List

Bloomberg LP logoBloomberg LP● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

Given the head of a singly linked list and an integer x, reorder the existing nodes so that every node with value less than x appears before every node with value greater than or equal to x.

Preserve the original relative order inside both partitions and return the reordered head.

Function

partitionList(head: ListNode, x: int) → ListNode

Examples

Example 1

head = [1,4,3,2,5,2]x = 3return = [1,2,2,4,3,5]

The nodes below 3 keep order 1,2,2; the remaining nodes keep order 4,3,5.

Example 2

head = [2,1]x = 2return = [1,2]

The node 1 moves into the less-than partition while relative order remains stable.

Example 3

head = []x = 0return = []

An empty list remains empty.

Constraints

  • The list contains at most 200 nodes.
  • -100 <= Node.val <= 100.
  • -100 <= x <= 100.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public ListNode partitionList(ListNode head, int x) {
    // Write your code here.
}
head[1,4,3,2,5,2]
x3
expected[1,2,2,4,3,5]
Checking account…