Algorithm Atlas · worked example
See the algorithm, step by step
Switch each pointer to the other head so both traverse equal combined distance.
This call is starting. Local values appear as they are initialized.
Data & variables
Changed Source indexExample input
a_prefix=[4,1]; b_prefix=[5,6,1]; shared=[8,4,5]Follow the code. Data appears here when it enters scope.
Call stack · 1 frame
maincall 1
Rust source
Line 34type SharedLink = Option<std::rc::Rc<SharedNode>>;
struct SharedNode {
value: i32,
next: SharedLink,
}
fn shared_list(prefix: &[i32], tail: SharedLink) -> SharedLink {
prefix.iter().rev().fold(tail, |next, &value| {
Some(std::rc::Rc::new(SharedNode { value, next }))
})
}
fn intersection(a: &SharedLink, b: &SharedLink) -> Option<i32> {
fn equal(a: &SharedLink, b: &SharedLink) -> bool {
match (a, b) {
(None, None) => true,
(Some(x), Some(y)) => std::rc::Rc::ptr_eq(x, y),
_ => false,
}
}
let (mut p, mut q) = (a.clone(), b.clone());
while !equal(&p, &q) {
p = match p {
Some(n) => n.next.clone(),
None => b.clone(),
};
q = match q {
Some(n) => n.next.clone(),
None => a.clone(),
};
}
p.map(|n| n.value)
}
fn main() {
let result = {
let tail = shared_list(&[8, 4, 5], None);
let a = shared_list(&[4, 1], tail.clone());
let b = shared_list(&[5, 6, 1], tail);
intersection(&a, &b).map_or("none".into(), |x| x.to_string())
};
println!("{result}");
}
Try your own version
Compare the visible input `a_prefix=[4,1]; b_prefix=[5,6,1]; shared=[8,4,5]` with `a_prefix=[]; b_prefix=[2]; shared=[7,8]`. Predict why their results are `8` and `7`. Then change one legal value and explain whether the same invariant, they are the same node allocation, not merely equal values, still applies.
Open personal copy in Playground ↗Start with this exact source and input, change a value, then use Run or Debug locally. This replay stays attached to the original example.
Recordings use a native Rust target. Playground runs on wasm32-wasip1; target-sized types such as usize can have different limits.

Understand it
Build a_prefix=[4,1] and b_prefix=[5,6,1] as separate nodes, then attach both to shared=[8,4,5]. Pointer identities first agree at 8. Equal-valued private prefixes remain distinct; an empty shared tail means no intersection.
The worked program is a complete executable for this pattern. Its core function uses typed input so the state transitions remain visible. The challenge input contract is: Input is one of two forms. a_prefix, b_prefix and shared arrays: build separate prefix nodes and attach both to the same shared tail. Or values=[...] next=[...] a=i b=j: an arena of nodes where next holds each node's next index or none, and a and b are the two head indexes. Return the first shared node's value, or none when the lists share no node. Equal values do not identify shared nodes. The visible fixture is a_prefix=[4,1]; b_prefix=[5,6,1]; shared=[8,4,5] and its example result is 8. The implementation and additional semantic fixtures were checked against an independent reference calculation. Those bounded tests establish these examples; they are not a proof for all inputs or a measurement on an iPhone.
Return a canonical String from solve: lowercase true/false for booleans; decimal integers without extra signs, leading zeros or whitespace; comma-separated sequence values and semicolon-separated groups. Whole-number medians have no decimal suffix; half medians use .5. Do not append a newline to the returned value. Preserve the explicitly named textual sentinels such as none or #. Input fixtures are bounded so the demonstrated arithmetic fits its named Rust types; satisfy the stated input preconditions before applying the algorithm.
String and character-grid fixtures use ASCII unless the lesson explicitly specifies Unicode scalar values.
A problem is a good match for Pointer-Switch Intersection when it asks for shared-tail intersection without lengths. Here the concrete task is: Input is one of two forms. a_prefix, b_prefix and shared arrays: build separate prefix nodes and attach both to the same shared tail. Or values=[...] next=[...] a=i b=j: an arena of nodes where next holds each node's next index or none, and a and b are the two head indexes. Return the first shared node's value, or none when the lists share no node. Equal values do not identify shared nodes. The visible input a_prefix=[4,1]; b_prefix=[5,6,1]; shared=[8,4,5] is an example, not a definition of the whole problem. Ask what operations the solution must support and what the input guarantees. Look for tasks where node identity or one-way traversal matters. An indexed vector solution can hide the pointer constraint rather than solve it. The right choice follows from those guarantees, not from a familiar noun in the prompt.
The decisive invariant is: Switch each pointer to the other head so both traverse equal combined distance. Try to say why it applies to the task without quoting the implementation. Which fact becomes known after one update, and which possible answers remain? A list algorithm changes relationships between nodes, not just values. Draw the next pointers before and after each mutation. If that claim cannot be maintained on a legal input, the pattern is wrong or one of its preconditions is missing. A counterexample is more useful than a vague warning: change one property of the sample, trace the first two steps, and identify the exact claim that fails.
Compare costs before committing. The advertised bound is O(n+m) time · O(1) space; identify the operation responsible for it and the extra memory it needs. Then compare a straightforward alternative: a direct scan, full sort, or repeated recomputation may be easier but can pay for the same work many times. Do not choose the more elaborate method just because it has a famous name. For the sample, predict 8 and explain which part of the state lets the method reach that result.
When the prompt is ambiguous, ask about input size, ordering, duplicates, and required output format. Check an empty list, one node, a head or tail change, and cycles when the problem allows them. Rust ownership makes linked structures explicit: use the provided node type, take links carefully, and preserve access to the remaining chain. The Rust sketch can show a useful primitive without proving that the whole task is solved; connect each primitive to the requested output. You should be able to name a fitting case, a near miss, and the assumption separating them before writing the first line.
The pattern
Switch each pointer to the other head so both traverse equal combined distance.
When to use it: shared-tail intersection without lengths
Complexity: O(n+m) time · O(1) space
Input: a_prefix=[4,1]; b_prefix=[5,6,1]; shared=[8,4,5]
Read the code
type SharedLink = Option<std::rc::Rc<SharedNode>>;
struct SharedNode {
value: i32,
next: SharedLink,
}
fn shared_list(prefix: &[i32], tail: SharedLink) -> SharedLink {
prefix.iter().rev().fold(tail, |next, &value| {
Some(std::rc::Rc::new(SharedNode { value, next }))
})
}
fn intersection(a: &SharedLink, b: &SharedLink) -> Option<i32> {
fn equal(a: &SharedLink, b: &SharedLink) -> bool {
match (a, b) {
(None, None) => true,
(Some(x), Some(y)) => std::rc::Rc::ptr_eq(x, y),
_ => false,
}
}
let (mut p, mut q) = (a.clone(), b.clone());
while !equal(&p, &q) {
p = match p {
Some(n) => n.next.clone(),
None => b.clone(),
};
q = match q {
Some(n) => n.next.clone(),
None => a.clone(),
};
}
p.map(|n| n.value)
}
fn main() {
let result = {
let tail = shared_list(&[8, 4, 5], None);
let a = shared_list(&[4, 1], tail.clone());
let b = shared_list(&[5, 6, 1], tail);
intersection(&a, &b).map_or("none".into(), |x| x.to_string())
};
println!("{result}");
}
Complete std-only list-intersection program for its typed teaching fixture. Parsing and output construction are separate from the core algorithm.
Try it
Compare the visible and independent fixtures. Explain the input assumption that makes this algorithm applicable, then identify a change that would invalidate it.
What success looks like: You can justify the choice from constraints rather than from a remembered problem title.
Check your understanding
Trace Pointer-Switch Intersection on input a_prefix=[1]; b_prefix=[1]; shared=[]. Which result follows from the documented contract?
Reveal the answer
1none1,1
For this input the result is none. They are the same node allocation, not merely equal values. Compare this independent fixture with the worked trace: Build a_prefix=[4,1] and b_prefix=[5,6,1] as separate nodes, then attach both to shared=[8,4,5]. Pointer identities first agree at 8. Equal-valued private prefixes remain distinct; an empty shared tail means no intersection.
Go deeper
Common misconception: “Their integer values compare equal”
They are the same node allocation, not merely equal values. Build a_prefix=[4,1] and b_prefix=[5,6,1] as separate nodes, then attach both to shared=[8,4,5]. Pointer identities first agree at 8. Equal-valued private prefixes remain distinct; an empty shared tail means no intersection.
- Concrete transition: Build a_prefix=[4,1] and b_prefix=[5,6,1] as separate nodes, then attach both to shared=[8,4,5]. Pointer identities first agree at 8. Equal-valued private prefixes remain distinct; an empty shared tail means no intersection.
- Independent second fixture: Input
a_prefix=[1]; b_prefix=[1]; shared=[]producesnone. Execute the named function in the worked program with these typed arguments, then compare its actual result with this independently computed expectation. - Boundary and contract: Input
a_prefix=[8,4]; b_prefix=[8]; shared=[4,5]produces4. They are the same node allocation, not merely equal values. The stated work bound is O(n+m) time · O(1) space. Count actual state changes; a successful output test alone does not prove that bound.
Transfer challenge: Compare the visible input a_prefix=[4,1]; b_prefix=[5,6,1]; shared=[8,4,5] with a_prefix=[]; b_prefix=[2]; shared=[7,8]. Predict why their results are 8 and 7. Then change one legal value and explain whether the same invariant, they are the same node allocation, not merely equal values, still applies.
Connections
- Pointer-Switch Intersection: Mental Model: Switch each pointer to the other head so both traverse equal combined distance.
- Challenge: Pointer-Switch Intersection: Input is one of two forms. a_prefix, b_prefix and shared arrays: build separate prefix nodes and attach both to the same shared tail. Or values=[...] next=[...] a=i b=j: an arena of nodes where next holds each node's next index or none, and a and b are the two head indexes. Return the first shared node's value, or none when the lists share no node. Equal values do not identify shared nodes.
Reveal the Algorithm Atlas answer
8
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Pointer-Switch Intersection: When to Use It (courseId "algorithms", lessonId "algorithm.list-intersection.recognize") Link: https://crabrix.com/learn/algorithms/algorithm.list-intersection.recognize Goal: They are the same node allocation, not merely equal values. State the input preconditions before applying this invariant. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=algorithms/algorithm.list-intersection.recognize) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.