Intersection of Two Linked Lists

Pattern: Traversal and dummy nodes

easy

Walk a through A then B, and b through B then A. Both cover lenA + lenB nodes, so they arrive at the shared node at the same moment, or reach null together if there is none.

The problem

Two singly linked lists may join at some node and share every node after it. Given their heads, return the first shared node, or null if they never join. The lists have no cycles, and they must keep their shape afterwards. For example, 4 → 1 → 8 → 4 → 5 and 5 → 6 → 1 → 8 → 4 → 5 join at the node 8 (the same node, not just the same value).

The function name getIntersectionNode matches LeetCode. Use the link above for the exact statement and limits. This guide's tests build the two lists from a shared tail and check that the very node returned is its first node.

Think first

  • Why can't you compare values to find the join?
  • If both lists had the same length before the join, how would you find it?
  • How can two walkers make up the difference in length without measuring it?

Hints

    Brute force, and why it fails

    For every node of A, scan all of B: O(m × n). A hash set of A's nodes is O(m + n) time but O(m) memory. Measuring both lengths and starting the longer one ahead works in O(1) memory too.

    The key insight

    Swapping lists at the end makes both walkers travel the same total distance. Equal distances from the two heads mean they reach the join, or the end, at the same step.

    Dry run

    A = 4 1 | 8 4 50 
    B = 5 6 1 | 8 4 51 

    A is 4 → 1 → 8 → 4 → 5, B is 5 → 6 → 1 → 8 → 4 → 5, and both share the nodes from 8 on. A has 2 nodes before the join, B has 3.

    Step 1 of 4

    The plan

    a = headA, b = headB
    while a != b:
        a = headB if a is null else a.next
        b = headA if b is null else b.next
    return a
    

    Complexity

    Time
    O(m + n)
    Space
    O(1)

    Each walker covers at most m + n + 1 steps.

    Edge cases

    • No join: both become null after m + n steps and the loop ends.
    • One list is the shared part itself: the join is its head.
    • Same lengths: they meet in the first pass.
    I've built my logic, show the code

    Java

    class Solution {
        public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
            ListNode a = headA, b = headB;
            while (a != b) {                        // both walk lenA + lenB steps, so they meet at the join (or at null)
                a = a == null ? headB : a.next;
                b = b == null ? headA : b.next;
            }
            return a;
        }
    }

    C++

    class Solution {
    public:
        ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {
            ListNode *a = headA, *b = headB;
            while (a != b) {                        // both walk lenA + lenB steps, so they meet at the join (or at NULL)
                a = a == NULL ? headB : a->next;
                b = b == NULL ? headA : b->next;
            }
            return a;
        }
    };

    Python

    from lib import ListNode
    
    class Solution:
        def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
            a, b = headA, headB
            while a is not b:                       # both walk lenA + lenB steps, so they meet at the join (or at None)
                a = headB if a is None else a.next
                b = headA if b is None else b.next
            return a

    Common mistakes

    • Comparing a.val == b.val, which finds equal values, not shared nodes.
    • Switching to the other list only when a.next is null, which can loop forever when there is no join.
    • Changing the lists, for example by marking nodes.

    Similar problems