Linked Lists · Linked List Fundamentals

Linked List Intersection

Finding the shared tail node of two lists of possibly different lengths by walking both with pointers that switch lists on reaching the end - which cancels out the length difference without ever counting it.

  • 7 min read
  • 11 practice questions
  • 1 graded problem
On this page
  1. Description
  2. The approach
  3. A second flavor: count the lengths, then align
  4. Where you'll see it
  5. Which tool for which job?
  6. Common mistakes
  7. Check yourself
  8. Key takeaways
  9. Quick revise
  10. Practice

Description

Two singly linked lists intersect when, from some node on, they share the very same nodes: a Y shape. Finding that first shared node is easy with a hash set. It's also possible in O(1) space with two pointers. Either measure both lengths and give the longer list a head start, or let each pointer jump to the other list's head when it runs out. Then both walk the same total distance and meet at the junction.

We'll go from simple to more technical:

  1. The pattern: two pointers that switch lists when they reach the end.
  2. A second flavor: count the lengths, then align the starts.
  3. Where finding the point two chains converge shows up in practice.
  4. The traps: comparing values instead of nodes, and assuming equal lengths.

The approach

The two lists usually have different lengths before they meet. So walking both from their heads, one step at a time, doesn't line up the shared part. Here's the fix. Walk pointer a along list A and pointer b along list B. When a pointer runs off the end, jump it to the head of the other list. Now each pointer walks both lists' private parts plus the shared part once. They cover the same total distance, so they reach the junction at the same moment.

List A is 3 -> 7 -> 2 -> 9 and list B is 5 -> 2 -> 9, where both lists share the same 2 node:

3

7

2

5

9

3

7

2

5

9

Stepab
Start35
172
229
39null
4null3 (jumped to A's head)
55 (jumped to B's head)7
622: the same node, so stop

Java

ListNode a = headA, b = headB;
while (a != b) {
    a = (a == null) ? headB : a.next;
    b = (b == null) ? headA : b.next;
}
return a;   // the intersection node, or null if the lists never intersect

Python

def get_intersection_node(head_a: ListNode | None, head_b: ListNode | None) -> ListNode | None:
    a, b = head_a, head_b
    while a is not b:
        a = head_b if a is None else a.next
        b = head_a if b is None else b.next
    return a   # the intersection node, or None if the lists never intersect

If the lists never meet, both pointers reach null at the same time after walking both lists, and the loop returns null.

A second flavor: count the lengths, then align

The same idea can be made explicit. Measure both lengths. Move the longer list's pointer ahead by the difference, so both pointers are the same distance from the end. Then walk them together until they meet:

Java

int lenA = length(headA), lenB = length(headB);
ListNode a = headA, b = headB;
// advance the longer list's pointer by the difference, so both have
// the same remaining distance left to the end
for (int i = 0; i < lenA - lenB; i++) a = a.next;
for (int i = 0; i < lenB - lenA; i++) b = b.next;
while (a != b) {
    a = a.next;
    b = b.next;
}
return a;

Python

def get_intersection_node(head_a: ListNode | None, head_b: ListNode | None) -> ListNode | None:
    len_a, len_b = length(head_a), length(head_b)
    a, b = head_a, head_b
    # advance the longer list's pointer by the difference, so both have
    # the same remaining distance left to the end
    for _ in range(len_a - len_b):
        a = a.next
    for _ in range(len_b - len_a):
        b = b.next
    while a is not b:
        a = a.next
        b = b.next
    return a

This walks each list once more to measure it, but some readers find the alignment easier to trust. Both versions rest on the same fact: two pointers the same distance from the end, moving together, reach the shared part at the same time.

Where you'll see it

Version histories work like this. Two branches of a repository each point back to their parent commit, and they meet at a shared ancestor. Finding where two chains of references converge, without knowing their lengths, is the same walk.

Which tool for which job?

You needUse
The junction, simplest codea hash set of one list's nodes
The junction in O(1) spaceswitch pointers, or count and align
The junction when lists may contain cyclesdetect cycles first (Floyd), then compare entrances
Whether two nodes are the same node== on references (Java), is (Python)
Whether two lists hold the same valuescompare values while walking

Common mistakes

MistakeWhat happensDo this instead
Comparing values (val ==)matches unrelated nodes with equal valuescompare references
equals on a record node / == on a dataclass nodecompares whole tails, deeply - and can overflow the stack== on references in Java, is in Python
Walking both lists in lockstep from the headsmisses the junction when lengths differswitch pointers, or align first
Switching more than oncean endless loop with no junctionswitch once; both reach null together
Running it on lists with cyclesnever terminatescheck for cycles first

Check yourself

Try to answer before opening each one - recalling beats re-reading.

1. Two nodes in two different lists hold the same value. Does a.val == b.val mean the lists intersect?

Show answer

No. Intersecting means sharing the same node object from some point on. Compare references, == in Java or is in Python. Two unrelated nodes can easily hold equal values.

2. Why does walking both lists from their heads, without switching, miss the junction?

Show answer

With different lengths, the pointers are always the same distance from their own heads, not from the shared part. So they compare unrelated nodes and pass the junction at different times. It only works when both lists happen to be the same length.

3. Why does switching to the other list's head make the pointers meet at the junction?

Show answer

Each pointer walks its own private part, then the other list's private part, then reaches the shared part. That's the same total distance for both. So they arrive at the first shared node on the same step, or both reach null if there's no shared part.

Key takeaways

  • Intersecting means sharing the same node. Compare references, never values.
  • Walking from both heads in step only works when the lengths match.
  • Jumping each pointer to the other list's head makes both walk the same distance, so they meet at the junction.
  • Counting both lengths and giving the longer list a head start works too, at the cost of an extra pass.

Quick revise

The whole skill on one page, for the night before an interview.

Everything from the lesson and the deep dive on one page. Skim it before practice or an interview, and open the full lesson for anything that doesn't ring a bell.

Pick the tool

You needUse
Simplesta hash set of nodes
O(1) spaceswitch pointers / count and align
Possible cyclesFloyd first
Same node?== (Java), is (Python)

Snippets worth knowing by heart

Java

ListNode p = headA, q = headB;
while (p != q) {                       // reference comparison
    p = (p == null) ? headB : p.next;  // switch once at the end
    q = (q == null) ? headA : q.next;
}
return p;                              // the junction, or null

Python

p, q = head_a, head_b
while p is not q:                      # identity, not ==
    p = p.next if p else head_b
    q = q.next if q else head_a
return p

The traps

  • Comparing values: unrelated nodes with equal values match. Compare references.
  • Record / dataclass ==: it compares whole tails and can overflow the stack. Use == on references in Java, is in Python.
  • Lockstep from the heads: lists of different lengths miss the junction. Switch pointers, or align first.
  • Switching twice: with no junction the loop never ends. Switch once; both reach null together.
  • Cycles: the walk never terminates. Check for cycles first.

Staff-level points, one line each

  • a + c + b = b + c + a: same distance, same node.
  • No junction: both reach null after a + b steps (12 for 7 and 4 nodes).
  • a = 1,000, b = 50, c = 300: switch 1,351 steps; count-and-align 2,650.
  • A Java record's equals said a copy was equal; on 100,000 nodes it overflowed the stack.
  • Cycles break the trick - detect them first.

Say it in an interview

  • "When a pointer runs out, it starts the other list, so both cover a + b + c."
  • "If there's no junction they both hit null together."
  • "I compare node references, not values."
  • "It's O(a + b) time and O(1) space."

Practice

This skill comes with 11 multiple-choice questions that tell you which misconception a wrong answer reveals and 1 graded problem. Your answers are graded and your mastery is tracked, so you'll know when the skill holds up. Running code and saving progress need a free account.

Practice Linked List Intersection free

Practice Linked List Intersection free

No card needed. Lessons, practice questions and easy problems are free forever.

Free plan, no card · 7-day Pro trial · Cancel anytime