Intersection of Two Linked Lists

Easy Linked List Miscellaneous Original on LeetCode

Intersection of Two Linked Lists is a easy linked list problem solved with the miscellaneous pattern. The best approach, optimal (two pointers that switch lists), runs in O(m + n) time and O(1) space. Below are 2 approaches in Java, from hash set up.

Problem

Two singly linked lists may merge at some node and share every node after it. Return the first shared node, or null if they never merge. Compare nodes by identity, not by value.

Examples

Example 1

Input
listA = [4, 1, 8, 4, 5], listB = [5, 6, 1, 8, 4, 5], shared tail starts at 8
Output
the node with value 8
Why
Equal values are not enough; it must be the same node object.

Example 2

Input
listA = [2, 6, 4], listB = [1, 5], no shared nodes
Output
null

Constraints

  • Lengths from 1 to 3 * 10^4.
  • No cycles. Do not modify the lists. Follow-up: O(1) memory.

Animated walkthrough

A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.

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

Solutions

Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the Java code.

ApproachTimeSpace
Hash setO(m + n)O(m)
Optimal (two pointers that switch lists)O(m + n)O(1)

1Hash set

TimeO(m + n)
SpaceO(m)

Store every node of list A; the first node of list B found in the set is the intersection.

  1. Add all of A's nodes to a set.
  2. Walk B and return the first node contained in the set.
Java
class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        Set<ListNode> seen = new HashSet<>();
        for (ListNode p = headA; p != null; p = p.next) seen.add(p);
        for (ListNode p = headB; p != null; p = p.next) if (seen.contains(p)) return p;
        return null;
    }
}

2Optimal (two pointers that switch lists)

TimeO(m + n)
SpaceO(1)

Pointer a walks A then B; pointer b walks B then A. Both cover lenA + lenB nodes, so they line up at the intersection, or both reach null together if there is none.

  1. a = headA, b = headB.
  2. While a != b: a = a == null ? headB : a.next; b = b == null ? headA : b.next.
  3. Return a.
Java
class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode a = headA, b = headB;
        while (a != b) {
            a = a == null ? headB : a.next;
            b = b == null ? headA : b.next;
        }
        return a;
    }
}

Edge cases to test

  • No intersection
  • Different lengths before the shared part
  • Intersection at the head of one list

Hints

Hint 1

If each pointer walks its own list and then switches to the other list, both travel lenA + lenB.

FAQ

What is the best time complexity for Intersection of Two Linked Lists?

Optimal (two pointers that switch lists) runs in O(m + n) time and O(1) extra space.

Which pattern does Intersection of Two Linked Lists use?

It is a linked list problem that uses the miscellaneous pattern. Other problems with the same pattern: Delete Node in a Linked List, Copy List with Random Pointer, Partition List.

Is there a brute force solution for Intersection of Two Linked Lists?

Yes. Hash set takes O(m + n) time and O(m) space. Store every node of list A; the first node of list B found in the set is the intersection.

Which edge cases should I test for Intersection of Two Linked Lists?

No intersection; Different lengths before the shared part; Intersection at the head of one list.