linked lists
intersecting node
data structures
algorithm
coding interview

Finding the intersecting node from two intersecting linked lists

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Two singly linked lists intersect only when they eventually point to the exact same node object in memory. That means intersection is about reference identity, not about two nodes holding the same value. The cleanest solution is the two-pointer technique, which finds the intersection in linear time and constant extra space.

Clarify What “Intersection” Means

These two lists intersect:

text
A: 1 -> 2 -> 3 -> 8 -> 9
B:      5 -> 8 -> 9

but only if the 8 node is literally the same node object reached from both heads.

They do not intersect if the lists merely contain equal values in separate nodes.

That distinction matters because many incorrect solutions compare node.value instead of comparing the node reference itself.

Best Solution: Two Pointers That Swap Heads

The standard elegant solution uses two pointers. Each pointer walks its list, then switches to the other list after reaching the end.

python
1class Node:
2    def __init__(self, value, next_node=None):
3        self.value = value
4        self.next = next_node
5
6
7def get_intersection_node(head_a, head_b):
8    p1 = head_a
9    p2 = head_b
10
11    while p1 is not p2:
12        p1 = head_b if p1 is None else p1.next
13        p2 = head_a if p2 is None else p2.next
14
15    return p1

Why it works:

  • pointer one walks A then B
  • pointer two walks B then A
  • both traverse the same total distance
  • if an intersection exists, they meet there
  • if no intersection exists, they both end at None

This gives O(m + n) time and O(1) extra space.

Example Setup in Python

python
1shared = Node(8, Node(9))
2head_a = Node(1, Node(2, Node(3, shared)))
3head_b = Node(5, shared)
4
5intersection = get_intersection_node(head_a, head_b)
6print(intersection.value if intersection else None)  # 8

This example works because both lists actually reuse the same shared node chain.

Alternative: Length Difference Method

Another valid approach is:

  1. compute both list lengths
  2. advance the longer list by the length difference
  3. walk both lists together until the references match
python
1def length(head):
2    count = 0
3    while head:
4        count += 1
5        head = head.next
6    return count
7
8
9def get_intersection_by_length(head_a, head_b):
10    len_a = length(head_a)
11    len_b = length(head_b)
12
13    while len_a > len_b:
14        head_a = head_a.next
15        len_a -= 1
16
17    while len_b > len_a:
18        head_b = head_b.next
19        len_b -= 1
20
21    while head_a is not head_b:
22        head_a = head_a.next if head_a else None
23        head_b = head_b.next if head_b else None
24
25    return head_a

This also runs in linear time and constant space. It is slightly more verbose than the pointer-switching solution, but still solid.

Alternative: Hash Set

A simpler but less space-efficient approach stores all nodes from one list in a set, then scans the other list.

python
1def get_intersection_with_set(head_a, head_b):
2    seen = set()
3
4    while head_a:
5        seen.add(head_a)
6        head_a = head_a.next
7
8    while head_b:
9        if head_b in seen:
10            return head_b
11        head_b = head_b.next
12
13    return None

This is easy to explain, but it uses O(m) extra space.

When There Is No Intersection

The good algorithms handle the no-intersection case naturally.

  • the two-pointer method returns None
  • the length-difference method returns None
  • the set-based method returns None

There is no need for a separate special-case algorithm.

Why the Two-Pointer Method Is Preferred

The pointer-switching method is preferred because it is:

  • short
  • constant-space
  • easy to test
  • independent of precomputed lengths

Once understood, it is the best combination of elegance and efficiency for interview and production use.

Common Pitfalls

A common mistake is comparing node values instead of node identities. Equal values do not imply intersecting lists.

Another issue is building the test data incorrectly. If you create two separate nodes with value 8, the lists do not intersect even if the printed shape looks the same.

Developers also sometimes forget the no-intersection case and write loops that never terminate. The two-pointer method avoids that if implemented exactly.

Finally, if the lists may contain cycles, that is a different problem. These standard intersection solutions assume ordinary acyclic singly linked lists.

Summary

  • Linked-list intersection means shared node identity, not shared node value.
  • The best standard solution uses two pointers that switch heads after reaching the end.
  • That method runs in O(m + n) time with O(1) extra space.
  • Length-difference and hash-set approaches also work, with different tradeoffs.
  • Test with truly shared node objects when verifying the algorithm.

Course illustration
Course illustration

All Rights Reserved.