Linked List · Pattern 5 of 5
More linked list tricks: split, copy, intersect and sort
A few problems need their own small idea: build separate lists and join them, copy a node's value instead of deleting it, let two pointers swap lists to meet, or merge sort with a middle split.
Watch the lesson
Concepts first, then the problem and every approach, step by step.
Space to play or pause · ← → to jump a step · click or drag the bar to seek
When to use it
- Rearrange nodes into groups but keep their order (by value, by odd or even position)
- You hold only the node to delete, not the one before it
- Two lists that may share a tail
- Sort a list or deep-copy one with extra pointers
The template
// keep order, put nodes < x before the rest: two lists, then join
ListNode partition(ListNode head, int x) {
ListNode small = new ListNode(0), big = new ListNode(0), s = small, b = big;
for (ListNode p = head; p != null; p = p.next) {
if (p.val < x) { s.next = p; s = p; }
else { b.next = p; b = p; }
}
b.next = null; // cut the old link, or the list may loop
s.next = big.next;
return small.next;
}
// merge sort: split at the middle, sort both halves, merge
ListNode sortList(ListNode head) {
if (head == null || head.next == null) return head;
ListNode slow = head, fast = head.next; // slow stops at the end of the first half
while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; }
ListNode second = slow.next;
slow.next = null;
return merge(sortList(head), sortList(second));
}
ListNode merge(ListNode a, ListNode b) {
ListNode dummy = new ListNode(0), t = dummy;
while (a != null && b != null) {
if (a.val <= b.val) { t.next = a; a = a.next; } else { t.next = b; b = b.next; }
t = t.next;
}
t.next = (a != null) ? a : b;
return dummy.next;
} def partition(head, x): # keep order, nodes < x first
small = s = ListNode(0)
big = b = ListNode(0)
p = head
while p:
if p.val < x:
s.next = p
s = p
else:
b.next = p
b = p
p = p.next
b.next = None # cut the old link, or the list may loop
s.next = big.next
return small.next
def sort_list(head): # merge sort on a list
if not head or not head.next:
return head
slow, fast = head, head.next # slow stops at the end of the first half
while fast and fast.next:
slow, fast = slow.next, fast.next.next
second, slow.next = slow.next, None
return merge(sort_list(head), sort_list(second))
def merge(a, b):
dummy = t = ListNode(0)
while a and b:
if a.val <= b.val:
t.next, a = a, a.next
else:
t.next, b = b, b.next
t = t.next
t.next = a or b
return dummy.next // keep order, put nodes < x before the rest: two lists, then join
ListNode* partition(ListNode* head, int x) {
ListNode small(0), big(0);
ListNode *s = &small, *b = &big;
for (ListNode* p = head; p; p = p->next) {
if (p->val < x) { s->next = p; s = p; }
else { b->next = p; b = p; }
}
b->next = nullptr; // cut the old link, or the list may loop
s->next = big.next;
return small.next;
}
ListNode* merge(ListNode* a, ListNode* b) {
ListNode dummy(0);
ListNode* t = &dummy;
while (a && b) {
if (a->val <= b->val) { t->next = a; a = a->next; } else { t->next = b; b = b->next; }
t = t->next;
}
t->next = a ? a : b;
return dummy.next;
}
// merge sort: split at the middle, sort both halves, merge
ListNode* sortList(ListNode* head) {
if (!head || !head->next) return head;
ListNode *slow = head, *fast = head->next; // slow stops at the end of the first half
while (fast && fast->next) { slow = slow->next; fast = fast->next->next; }
ListNode* second = slow->next;
slow->next = nullptr;
return merge(sortList(head), sortList(second));
} // keep order, put nodes < x before the rest: two lists, then join
function partition(head, x) {
const small = new ListNode(0), big = new ListNode(0);
let s = small, b = big;
for (let p = head; p; p = p.next) {
if (p.val < x) { s.next = p; s = p; }
else { b.next = p; b = p; }
}
b.next = null; // cut the old link, or the list may loop
s.next = big.next;
return small.next;
}
// merge sort: split at the middle, sort both halves, merge
function sortList(head) {
if (!head || !head.next) return head;
let slow = head, fast = head.next; // slow stops at the end of the first half
while (fast && fast.next) { slow = slow.next; fast = fast.next.next; }
const second = slow.next;
slow.next = null;
return merge(sortList(head), sortList(second));
}
function merge(a, b) {
const dummy = new ListNode(0);
let t = dummy;
while (a && b) {
if (a.val <= b.val) { t.next = a; a = a.next; } else { t.next = b; b = b.next; }
t = t.next;
}
t.next = a ?? b;
return dummy.next;
} The variations
-
1Copy the value, skip the next node
You cannot reach the node before, so make this node become the next one: copy
next.valinto it, then unlinknext. It works because the node is never the tail. -
2Split into lists, then join
Walk once and hang each node on one of two dummy-headed lists (smaller and bigger, odd and even positions). Join them and set the last
nextto null. Splicing one list into another is the same: find the node before and the node after, then connect.Now you solve -
3Switch lists to line up
Walk both lists; when a pointer reaches the end, restart it at the other list's head. Both then travel the same total length, so they meet at the shared node (or both reach null together).
-
4Weave copies into the list
Insert each copy right after its original:
A -> A' -> B -> B'. Then a copy's random pointer isorig.random.next. Finally pull the two lists apart. A hash map from original to copy is the simpler O(n)-space version. -
5Merge sort a list
Split at the middle with fast and slow pointers, sort each half, merge them like two sorted lists. No random access is needed, which is why merge sort suits lists.
-
6More practice
More problems that use this pattern. Try them once the variations above feel easy.
Problems
Mistakes to avoid
- After splitting into two lists, leaving the last node's old
nextin place: the joined list loops. - Merge sort with
fast = head: on two nodes the split never shrinks and the recursion never ends. - Intersection: comparing values instead of node references.
Lesson done
Back to Linked List →Mark it done once you can write the template from memory and you have solved the worked examples yourself.