ExamAdda Logo

Partition List

Easy

Given the head of a linked list and an integer x, partition the list so that all nodes with values less than x come before nodes with values greater than or equal to x.

The relative order of the nodes in each partition should be preserved.

Example 1

Input

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

Output

[1,2,2,4,3,5]

Explanation

Nodes less than 3 are 1,2,2, and nodes greater than or equal to 3 are 4,3,5. Their original relative order is preserved.

Example 2

Input

head = [2,1]
x = 2

Output

[1,2]

Explanation

The node 1 is less than 2, so it comes before the node 2.

Constraints

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

Hints:

Hint 1

Create two separate lists: one for nodes with values less than x and another for nodes with values greater than or equal to x.

Hint 2

After processing all nodes, connect the smaller-values list to the greater-or-equal-values list while preserving their original order.

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 18, 2026

Last updated

Aug 19, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output