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.

6 variations · 8 problems · 6 with animated walkthroughs

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

Miscellaneous
// 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;
}

The variations

  1. 1Copy the value, skip the next node

    You cannot reach the node before, so make this node become the next one: copy next.val into it, then unlink next. It works because the node is never the tail.

  2. 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 next to null. Splicing one list into another is the same: find the node before and the node after, then connect.

  3. 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).

  4. 4Weave copies into the list

    Insert each copy right after its original: A -> A' -> B -> B'. Then a copy's random pointer is orig.random.next. Finally pull the two lists apart. A hash map from original to copy is the simpler O(n)-space version.

  5. 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.

  6. 6More practice

    More problems that use this pattern. Try them once the variations above feel easy.

Mistakes to avoid

  • After splitting into two lists, leaving the last node's old next in 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.
Back to Linked List →

Mark it done once you can write the template from memory and you have solved the worked examples yourself.