86. Partition List
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to partition a linked list based on a value x. All nodes with values less than x must appear before nodes with values greater than or equal to x. Importantly, the relative order of nodes within each of the two partitions must be preserved. For instance, if node A appears before node B in the original list and both have values less than x, then node A must also appear before node B in the partitioned list.
Solution
Partition List in Python
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def partition(self, head: Optional[ListNode], x: int) -> Optional[ListNode]:
# Dummy nodes to start the two lists.
# `less_head` for nodes with val < x.
# `greater_head` for nodes with val >= x.
less_head = ListNode(0)
greater_head = ListNode(0)
# Pointers to the tails of the two lists.
less_tail = less_head
greater_tail = greater_head
# Iterate through the original list.
current = head
while current:
if current.val < x:
# Append the current node to the 'less' list.
less_tail.next = current
less_tail = less_tail.next
else:
# Append the current node to the 'greater' list.
greater_tail.next = current
greater_tail = greater_tail.next
current = current.next
# The last node of the 'greater' list must point to None to terminate the list.
# This is crucial to avoid cycles.
greater_tail.next = None
# Connect the 'less' list with the 'greater' list.
# The 'less' list's tail should point to the head of the 'greater' list.
less_tail.next = greater_head.next
# The head of the new partitioned list is the node after the 'less_head' dummy node.
return less_head.next
Partition List in Java
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode partition(ListNode head, int x) {
// Dummy nodes to start the two lists.
// `lessHead` for nodes with val < x.
// `greaterHead` for nodes with val >= x.
ListNode lessHead = new ListNode(0);
ListNode greaterHead = new ListNode(0);
// Pointers to the tails of the two lists.
ListNode lessTail = lessHead;
ListNode greaterTail = greaterHead;
// Iterate through the original list.
ListNode current = head;
while (current != null) {
if (current.val < x) {
// Append the current node to the 'less' list.
lessTail.next = current;
lessTail = lessTail.next;
} else {
// Append the current node to the 'greater' list.
greaterTail.next = current;
greaterTail = greaterTail.next;
}
current = current.next;
}
// The last node of the 'greater' list must point to null to terminate the list.
// This is crucial to avoid cycles.
greaterTail.next = null;
// Connect the 'less' list with the 'greater' list.
// The 'less' list's tail should point to the head of the 'greater' list.
lessTail.next = greaterHead.next;
// The head of the new partitioned list is the node after the 'lessHead' dummy node.
return lessHead.next;
}
}
Partition List in C++
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
// Dummy nodes to start the two lists.
// `less_head` for nodes with val < x.
// `greater_head` for nodes with val >= x.
ListNode less_head(0);
ListNode greater_head(0);
// Pointers to the tails of the two lists.
ListNode* less_tail = &less_head;
ListNode* greater_tail = &greater_head;
// Iterate through the original list.
ListNode* current = head;
while (current) {
if (current->val < x) {
// Append the current node to the 'less' list.
less_tail->next = current;
less_tail = less_tail->next;
} else {
// Append the current node to the 'greater' list.
greater_tail->next = current;
greater_tail = greater_tail->next;
}
current = current->next;
}
// The last node of the 'greater' list must point to null to terminate the list.
// This is crucial to avoid cycles if the original list's last node
// ends up in the 'less' list.
greater_tail->next = nullptr;
// Connect the 'less' list with the 'greater' list.
// The 'less' list's tail should point to the head of the 'greater' list.
less_tail->next = greater_head.next;
// The head of the new partitioned list is the node after the 'less_head' dummy node.
return less_head.next;
}
};
Partition List in TypeScript
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
function partition(head: ListNode | null, x: number): ListNode | null {
// Dummy nodes to start the two lists.
// `lessHead` for nodes with val < x.
// `greaterHead` for nodes with val >= x.
const lessHead = new ListNode(0);
const greaterHead = new ListNode(0);
// Pointers to the tails of the two lists.
let lessTail: ListNode = lessHead;
let greaterTail: ListNode = greaterHead;
// Iterate through the original list.
let current: ListNode | null = head;
while (current !== null) {
if (current.val < x) {
// Append the current node to the 'less' list.
lessTail.next = current;
lessTail = lessTail.next;
} else {
// Append the current node to the 'greater' list.
greaterTail.next = current;
greaterTail = greaterTail.next;
}
current = current.next;
}
// The last node of the 'greater' list must point to null to terminate the list.
// This is crucial to avoid cycles.
greaterTail.next = null;
// Connect the 'less' list with the 'greater' list.
// The 'less' list's tail should point to the head of the 'greater' list.
lessTail.next = greaterHead.next;
// The head of the new partitioned list is the node after the 'lessHead' dummy node.
return lessHead.next;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.