Partition List
Medium
Topics
Given the head of a linked list and a value k, partition it so that all nodes with value less than k come before nodes with value greater than or equal to k. Preserve the original relative order within each part. (LeetCode calls this parameter x.)
Example 1
Input: head = [1,4,3,2,5,2], k = 3 Output: [1,2,2,4,3,5]
Example 2
Input: head = [2,1], k = 2 Output: [1,2]
Constraints
- 0 <= number of nodes <= 200
- -100 <= Node.val <= 100
- -200 <= k <= 200
Editorial — intuition, approach, complexity and pitfalls
Intuition
Build two chains while walking the input once: one for values below k, one for the rest. Appending in input order means relative order is preserved for free — there is no reordering step to get wrong.
Two dummy heads remove every special case around the first node of each chain, including the case where one chain stays empty. At the end, join the two and return.
Approach
- Create dummy heads for the less and greater-or-equal chains, with tails pointing at them.
- Walk the input, appending each node to whichever chain its value selects.
- Terminate the greater chain by setting its tail's
nextto null. - Point the less chain's tail at the greater chain's first real node and return the less chain.
Dry Run
input 1 -> 4 -> 3 -> 2 -> 5 -> 2 k = 3
less chain 1 -> 2 -> 2
greater chain 4 -> 3 -> 5
join 1 -> 2 -> 2 -> 4 -> 3 -> 5
note that 4 -> 3 keeps its input order; the greater chain is not sortedComplexity
- Time — O(n): one pass, constant work per node.
- Space — O(1): two dummy nodes and four pointers. No node is copied; the original nodes are relinked in place.
Common Pitfalls
- Omitting the
g.next = Nonetermination. The last node of the greater chain still points wherever it pointed in the input, which closes a cycle: on[1,4,3,2,5,2]withk = 3the result walks 1, 2, 2, 4, 3, 5, 2, 4, 3, 5, 2, 4, and never ends. This is the defect that makes the problem worth asking. - Using
<=instead of<. Nodes equal tokbelong in the second part. - Building the chains from the real first nodes instead of dummies, which forces a null check on every append and a special case when a chain ends up empty.
- Swapping values to avoid relinking. It breaks stability within a part, which the problem requires.
- Sorting, or partitioning as in quicksort. Quicksort's partition is not stable, and stability is the requirement here.
- Forgetting that the less tail must be relinked too. If every node went to the greater chain, the less tail is still the dummy and the join is what makes the function return the right head.
Full Solution — all languages
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def partition(self, head, k):
less = ListNode(0)
greater = ListNode(0)
l, g = less, greater
cur = head
while cur:
if cur.val < k:
l.next = cur
l = cur
else:
g.next = cur
g = cur
cur = cur.next
g.next = None
l.next = greater.next
return less.nextimport java.util.*;
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
class Solution {
public ListNode partition(ListNode head, int k) {
ListNode less = new ListNode(0), greater = new ListNode(0);
ListNode l = less, g = greater, cur = head;
while (cur != null) {
if (cur.val < k) {
l.next = cur;
l = cur;
} else {
g.next = cur;
g = cur;
}
cur = cur.next;
}
g.next = null;
l.next = greater.next;
return less.next;
}
}#include <bits/stdc++.h>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x): val(x), next(nullptr) {
}
};
class Solution {
public: ListNode* partition(ListNode* head, int k) {
ListNode less(0), greater(0);
ListNode* l = &less;
ListNode* g = &greater;
ListNode* cur = head;
while (cur) {
if (cur->val < k) {
l->next = cur;
l = cur;
} else {
g->next = cur;
g = cur;
}
cur = cur->next;
}
g->next = nullptr;
l->next = greater.next;
return less.next;
}
};/** Node shape: { val, next }. Build/return plain objects or use this ctor. */ function ListNode(val, next) {
this.val = (val === undefined ? 0 : val);
this.next = (next === undefined ? null : next);
}
/**
* @param {ListNode} head
* @param {number} k
* @return {ListNode}
*/ var partition = function(head, k) {
const less = new ListNode(0), greater = new ListNode(0);
let l = less, g = greater, cur = head;
while (cur) {
if (cur.val < k) {
l.next = cur;
l = cur;
} else {
g.next = cur;
g = cur;
}
cur = cur.next;
}
g.next = null;
l.next = greater.next;
return less.next;
};