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:
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.
Why it works:
- pointer one walks
AthenB - pointer two walks
BthenA - 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
This example works because both lists actually reuse the same shared node chain.
Alternative: Length Difference Method
Another valid approach is:
- compute both list lengths
- advance the longer list by the length difference
- walk both lists together until the references match
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.
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 withO(1)extra space. - Length-difference and hash-set approaches also work, with different tradeoffs.
- Test with truly shared node objects when verifying the algorithm.

