86. Partition List
Problem Overview
Partition List (Medium) asks you to solve a structured algorithmic task. This is a common Linked List / Two Pointers pattern in coding interviews. Build two sublists using dummy head nodes: one for values < x and one for values >= x.
A full step-by-step explanation is being added. See the study guide for pattern-based practice.
Approach
Build two sublists using dummy head nodes: one for values < x and one for values >= x.
Iterate through the original list: append each node to the appropriate sublist.
After traversal, terminate the second sublist's tail (to avoid a cycle), then connect the first sublist's tail to the second sublist's head.
Return dummy1.next as the new head.
Dummy nodes eliminate special-casing for empty sublists.
Related patterns: Linked List, Two Pointers
// Approach: Build two sublists using dummy head nodes: one for values < x and one for values >= x.
// Iterate through the original list: append each node to the appropriate sublist.
// After traversal, terminate the second sublist's tail (to avoid a cycle), then connect the first sublist's tail to the second sublist's head.
// Return dummy1.next as the new head.
// Dummy nodes eliminate special-casing for empty sublists.
// Time: O(n) Space: O(1) — nodes are relinked, not copied.
public class ListNode
{
public int val;
public ListNode next;
public ListNode(int val = 0, ListNode next = null)
{
this.val = val;
this.next = next;
}
}
public class Solution
{
public ListNode Partition(ListNode head, int x)
{
ListNode beforeHead = new ListNode();
ListNode afterHead = new ListNode();
ListNode before = beforeHead;
ListNode after = afterHead;
while (head != null)
{
if (head.val < x)
{
before.next = head;
before = head;
}
else
{
after.next = head;
after = head;
}
head = head.next;
}
after.next = null;
before.next = afterHead.next;
return beforeHead.next;
}
}Related Problems
- 11. Container With Most Water(Medium)
- 15. 3Sum(Medium)
- 16. 3Sum Closest(Medium)
- 19. Remove Nth Node From End of List(Medium)
- 25. Reverse Nodes in k-Group(Hard)
- 26. Remove Duplicates from Sorted Array(Easy)