Intersection of Two Linked Lists solution explained
To find where two linked lists intersect, run one pointer through list A then list B, and another through list B then list A. Both pointers travel the same total distance, so they arrive at the shared node together, or reach null together when the lists share nothing. The Intersection of Two Linked Lists solution runs in O(m + n) time with O(1) extra space, and it compares nodes by identity, never by value.
By Evin JohnPublished 6 min read
The task, restated
You have the heads of two singly linked lists. At some node they may join, after which they are the same chain to the end. Return that join node. If they never join, return null. Neither list contains a cycle, and both must look the same afterwards, so you may not leave any pointer changed.
| List A | List B | Answer |
|---|---|---|
| 2 → 6 → [11 → 15 → 20] | 9 → [11 → 15 → 20] | The node holding 11 (bracketed nodes are shared objects) |
| 1 → 3 | 1 → 3, separate nodes | null: equal values, no shared node |
| [4 → 8] | [4 → 8], the same head | The head itself |
| 5 → 7 | empty | null |
The second row is the trap. Two lists can hold the same values without sharing memory. The question is about the node objects, so compare references (is in Python, === in JavaScript), and say so before you write code.
How interviewers use it
This problem rewards one key observation: a singly linked node has only one next pointer, so once two lists meet they cannot split again. After that, the challenge is aligning two walks of different lengths without extra memory. Interviewers often ask for the hash set first, then push for constant space, then ask you to prove the two-pointer version always stops.
- Prove termination when there is no intersection. Both pointers reach null on the same step, after m + n + 1 moves.
- Lists might contain cycles. Then detect each cycle first; the problem changes shape completely.
- Return the length of the shared tail. Count from the intersection node to the end.
- Many lists, one common node. Hash the first list's nodes or compare tails pairwise.
Brute force: remember A's nodes
Comparing every node of A with every node of B is O(m·n). A set brings that down: store every node of A, then walk B and return the first node already in the set. Python objects hash by identity unless the class overrides equality, so a set of nodes is a set of references.
def intersection_with_set(head_a, head_b):
seen = set()
while head_a:
seen.add(head_a)
head_a = head_a.next
while head_b:
if head_b in seen:
return head_b
head_b = head_b.next
return NoneO(1) space: switch heads at the end
Say A has p nodes before the join, B has q, and the shared tail has c. A pointer that walks A and then continues at the head of B covers p + c + q nodes before reaching the join the second time around. A pointer that walks B then A covers q + c + p. Same number, so they land on the join node in the same step. With no join, c is 0 and both hit null at the same step, which ends the loop.
def get_intersection_node(head_a, head_b):
a, b = head_a, head_b
while a is not b:
a = a.next if a else head_b
b = b.next if b else head_a
return aThe other constant-space approach is more explicit: count both lengths, advance the longer list's pointer by the difference, then walk both together until they meet. It does the same amount of work and some interviewers find it easier to follow.
function getIntersectionNode(headA, headB) {
let a = headA, b = headB;
while (a !== b) {
a = a ? a.next : headB;
b = b ? b.next : headA;
}
return a;
}
function lengthOf(node) {
let n = 0;
for (; node; node = node.next) n++;
return n;
}
function intersectionByLength(headA, headB) {
let m = lengthOf(headA), n = lengthOf(headB);
let a = headA, b = headB;
for (; m > n; m--) a = a.next;
for (; n > m; n--) b = b.next;
while (a !== b) {
a = a.next;
b = b.next;
}
return a;
}Time and space
| Approach | Time | Extra space |
|---|---|---|
| Compare every pair | O(m·n) | O(1) |
| Hash set of A's nodes | O(m + n) | O(m) |
| Equalize lengths | O(m + n) | O(1) |
| Switch heads | O(m + n), at most m + n + 1 steps per pointer | O(1) |
Edge cases
- Either list empty: the answer is null, and the switch-heads loop still stops.
- Both heads are the same node: return it immediately.
- The join is the last node of both lists.
- One list is entirely the tail of the other.
- Equal values in unrelated nodes, which must not count as an intersection.
Mistakes to avoid
- Comparing val instead of the node reference.
- Switching with a = a.next ? a.next : headB, which skips the null step and loops forever when there is no intersection.
- Marking visited nodes by changing their values or pointers, which the problem forbids.
- Assuming the lists can cross like an X; a node has one next pointer, so they cannot split again.
- Reversing one list to find the join, then forgetting to restore it.
How to explain it out loud
Fictional example
- Candidate
- Once the lists share a node, everything after it is shared too, so it is a Y shape. I compare nodes by reference, not by value.
- Candidate
- A set of A's nodes would work in linear memory. To use constant memory, I walk A then B with one pointer, and B then A with the other.
- Candidate
- Each covers A's private part, B's private part and the shared tail, so they reach the join on the same step. If nothing is shared, they both reach null on the same step, after walking both lists, and the loop ends.
Related walkthroughs
Linked List Cycle II uses a similar distance argument to find where a cycle starts. Palindrome Linked List is the other two-pointer problem in this set. Two Sum is the classic case of trading memory for time with a hash set, the same trade as the brute force here.
Where Natively fits
While you practice on a Mac or Windows PC, Natively can read a diagram-heavy problem like this from a screenshot and write an approach and code, which you can hold up against your own termination argument. A transcript of the mock interview shows whether you said "by reference" out loud. The AI interview assistant page covers how it works.
Questions people ask
How do you find the intersection of two linked lists?
Run one pointer through list A then B and another through B then A. They travel the same distance and meet at the shared node, or both reach null if there is none.
Why do the two pointers meet at the intersection?
Each covers A's private part, B's private part and the shared tail, so both reach the join after the same number of steps.
What is the time complexity of Intersection of Two Linked Lists?
O(m + n) time and O(1) extra space for the two-pointer versions. The hash set version is also O(m + n) time but uses O(m) memory.
Should you compare node values or nodes?
Nodes. Two lists can hold equal values in separate nodes without sharing anything, so compare references.
Where to go next
Practice with the same assistant you would use on a call: it listens, transcribes and suggests answers from your own resume and the job description.
Facts on this page were last checked on . Product details come from the Natively app itself; if something here is out of date, tell us at hello@heynatively.com.
