Intersection of Two Linked Lists
Problem Statement
You are given the heads of two singly linked lists, headA and headB. At some node the two lists may join: from that node on, they share the exact same nodes (the same objects in memory) until the end.
Return the first shared node. If the lists never join, return null.
The lists have no cycles, and you must not change their structure. Follow-up: can you do it in O(m + n) time and O(1) extra memory?
Two list heads headA (m nodes) and headB (n nodes). The judge builds them from listA, listB, skipA, skipB and intersectVal, but your function only gets the two heads.
The node where the two lists join, or null if they don't.
A's own part is [4, 1], B's own part is [5, 6, 1], and both then continue with the same nodes 8 → 4 → 5. Note that the 1 in A and the 1 in B have the same value but are different nodes.
After [1, 9, 1] in A and [3] in B, both lists continue with the same nodes 2 → 4.
The lists never share a node.
- • 1 ≤ m, n ≤ 3 · 10^4
- • 1 ≤ Node.val ≤ 10^5
- • 0 ≤ skipA ≤ m, 0 ≤ skipB ≤ n
- • The lists contain no cycles.
- • The list structure must be the same after the function returns.
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
For each node of A, scan all of B for the same node
Store every node address of A, then find the first node of B in the set
a walks A then B, b walks B then A; equal totals make them meet at the join
Take each node of A in order and scan the whole of B looking for that same node (same address). The first node of A found in B is the join, because everything before the join belongs only to A.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Linked List — Fast & Slow translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.