← Questions
Intersection of two linked lists that may have cycles
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Airbnb's coding round, last asked Jan 2026. It tests linked lists, cycle detection and two pointers.
You get the heads of two singly linked lists. Either list may end normally or contain a cycle. Decide whether the lists share any node (by identity, not value) and return the first shared node, or None.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
first_shared_node(head_a, head_b) -> Node | None. If both lists enter the same cycle at different nodes, return either entry point.
Example
no cycles: A: 1 -> 2 -> 7 -> 8 B: 5 -> 7 -> 8 (the same 7)
-> the node 7
one cycle: A: 1 -> 2 -> 3 -> 2 (cycle) B: 4 -> 5 -> None
shared cycle, same entry:
A: 1 -> 6 -> 7 -> 8 -> 7 B: 2 -> 6 -> the node 6 (they merge before the cycle)
shared cycle, different entries:
cycle 7 -> 8 -> 9 -> 7; A enters at 7, B enters at 9
-> 7 or 9
Constraints
- Up to
10^5nodes in each list. - O(1) extra memory is the target; don't modify the lists.
Clarifying questions
- Is sharing by node identity?
- With different cycle entries, which node counts as "first"?
- May I use a hash set, or must memory be O(1)?
What a strong answer covers
- Floyd's algorithm on each list to detect a cycle and find its entry.
- Neither has a cycle: compare tails, then align by the length difference and walk together.
- Only one has a cycle: they can't share a node.
- Both have cycles: same entry means they merge before or at it (treat the entry as the tail and use the no-cycle method); different entries share a cycle only if walking around one cycle reaches the other entry.
- O(n + m) time, O(1) space; the hash-set version as a simpler O(n) space alternative.
Follow-ups
- Return the length of the shared part.
- Prove why Floyd's meeting point leads to the cycle entry.
- Lists of
kheads: find every pair that shares a node.
Where it's asked
- Airbnb · Coding · Last asked Jan 202645 min · Live coding, one or two rounds, from the same pool as the screen
What it tests
linked lists · cycle detection · two pointers
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
