Partition List

Given the head of a linked list and an integer x, partition the list so that all nodes less than x come before nodes greater than or equal to x, preserving original order. Example: Input: head = [1,4,3,2,5,2], x = 3 Output: [1,2,2,4,3,5] Explanation: Nodes <3 (1,2,2) come before nodes ≥3 (4,3,5). Pattern focus: Dummy Head Node (build two lists).

Input Format

head = ListNode, x = int

Output Format

return ListNode

Constraints

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

Examples

Example 1:

Input:

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

Output:

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

Explanation:

Nodes less than 3 (1,2,2) come before >=3 (4,3,5).

Loading...
Partition List - Linked List DSA Problem