363. Partition List Around a Value X

MediumLinked ListLinked ListTwo Pointers

Given the head of a linked list and a value x, partition it so that all nodes with value less than x come before nodes with value greater than or equal to x. Preserve the original relative order within each partition. The list is given as an array; return the partitioned array. Input: '[list], x'.

Input: '[list], x'.

Output: Array — the partitioned list.

Examples

Example 1
Input: [1,4,3,2,5,2], 3
Output: [1,2,2,4,3,5]
Explanation: Values <3 first, order preserved.
Example 2
Input: [2,1], 2
Output: [1,2]
Explanation: 1 before 2.
Example 3
Input: [1], 0
Output: [1]
Explanation: Single node.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →