Intersection of Two Chains
Every problem on this page hands you two starting points and a single way to move: follow one pointer forward. Once two such walks touch the same node they never separate again, because every node has only one "next". So the two paths always form a Y: two private prefixes and one shared tail.
The answer is always the first node of that shared tail, and there are two ways to find it.
- Set. Walk the first chain to its end and remember every node. Then walk
the second chain; the first node you have already seen is the answer. Costs
O(m + n)time andO(m)memory. - Two-pointer switch. Walk both chains at once. When a pointer falls off the
end, restart it at the other chain's head. Both pointers then cover
a + c + bnodes, so they reach the shared node on the same step. CostsO(m + n)time andO(1)memory.
What changes from problem to problem is only what counts as "next".
The hub: .next
The plain version. Two singly linked lists that may share a tail, and the
pointer to follow is .next.
All four solutions are here: brute force, the set, the length-difference
alignment, and the two-pointer switch. The length-difference solution is the
switch with its trick made explicit: measure both lengths, give the longer list
a head start equal to the difference, then walk both together. The switch gets
the same alignment for free by making each pointer walk both prefixes. Learn
the None stop in the switch's explanation well, because the next two problems
use it unchanged.
160. Intersection of Two Linked Lists
.parent instead of .next
160 on a tree. You get two tree nodes p and q, each with a .parent
pointer, and no root.
Walking up from a node traces a linked list that ends at the root. Walking up
from p and from q gives two lists that merge where the paths meet, and that
merge point is their lowest common ancestor. Replace .next with .parent and
the Ancestor Set and Two-Pointer Switch solutions are 160's set and switch.
The first solution on this page climbs to the root and then counts; it is the
Lowest Common Ancestor family's approach
and is included so you can compare the two.
1650. Lowest Common Ancestor of a Binary Tree III
Build the pointers first
1650 with no pointers given. The input is a list of rows, each one a region followed by the regions directly inside it. The problem never mentions a tree or a list.
Invert every row into parents[child] = parent and you have 1650 again, with
parents[x] as the "next" step. Both solutions carry over: the ancestor set
and the two-pointer switch. The one new step is building the map. The lesson
is to recognize this family from the Y shape, even when the problem never
names it.
1257. Smallest Common Region
The three problems side by side:
| Problem | Title | Chain step | Pointers given? | Set space | Switch space |
|---|---|---|---|---|---|
| 160the hub | Intersection of Two Linked Lists | node.next | O(m) | O(1) | |
| 1650from 160 | LCA of a Binary Tree III | node.parent | O(h)ancestors of p | O(1) | |
| 1257from 1650 | Smallest Common Region | parents[x] | invert the rows into a map first | O(r + h)the map plus ancestors | O(r)the map itself |