intermediate9 min read·Updated September 24, 2026
Intersection of Two Linked Lists Explained: Intersect at 8 Walkthrough
Master the intersection of two linked lists concept. Follow a step-by-step trace of A=4→1→8→4→5 and B=5→6→1→8→4→5 using the two-pointer bridge model.
By Learnisim AI·Published September 24, 2026
LEARNING ARCSetup → Model in the example → Full trace → Twist → Edge cases → Apply
PREREQUISITES
- Basic pointer manipulation
- Singly linked list traversal
Why
Why Two Linked Lists Can Share a Tail Without Copying
Phase 1: The Shape of Shared Memory
Imagine you are managing two separate text histories in memory where both timelines eventually merge into the exact same sequence of edits. In data structures, this is represented by two singly linked lists that share a common tail, such as our locked working example: list and list . They start with different prefix nodes ( versus ), but once they hit node 8, they point to the exact same memory objects all the way to the end.
Without a clever pointer strategy, finding where these two lists merge requires checking every node in list against every node in list using a nested loop, or storing references in a hash set with extra space. That brute-force approach ignores the structural gift of the merged tail: once two nodes point to the same next object, they can never split back apart. We need an approach that effortlessly bridges the gap between different prefix lengths without allocating extra memory.
Model
The Two-Pointer Bridge Model for Linked List Intersections
Phase 2: The Two-Pointer Bridge Model
When looking at our working example where list () and list () merge at node 8, the core hurdle is mismatched head lengths. List has a unique prefix of length 2, while list has a unique prefix of length 3 before hitting the shared node 8.
Instead of measuring lengths or using nested loops, we deploy a path-redirection model. We imagine two runners, pointer starting at the head of list and pointer starting at the head of list . When a pointer reaches the end of its list (hits
null), it instantly teleports to the head of the opposite list and continues walking.Let us visualize what happens to their total travel distance. Pointer travels list followed by list . Pointer travels list followed by list . Because addition is commutative (), both pointers travel the exact same total number of steps before they align at the intersection node 8.
The Anatomy of the Shared Suffix
Once both pointers cross into the shared tail starting at node 8, they are locked in sync. Every subsequent step they take together is identical: . The elegance of this model is that the distance mismatch is completely dissolved during the first pass of cross-traversal.
Worked example
Tracing the Intersect at 8 Example Step by Step
Phase 3: Walking the Dual Paths
Now let's trace our locked example from start to finish. We have list A starting at head 4 with length 5 (), and list B starting at head 5 with length 6 (). Both lists merge at node 8, sharing the exact same remaining tail nodes .
Given:
- Pointer
- Pointer
- Pointer
p starts at A.head (node 4).- Pointer
q starts at B.head (node 5).Steps:
1. First Pass (Individual Lists):
2. Absorbing the Offset: Because list B has one extra node at the front, pointer
3. Second Pass (The Convergence): Both pointers are now equidistant from the intersection node. Stepping forward in lockstep,
1. First Pass (Individual Lists):
p travels through and then jumps to B.head. Meanwhile, q travels through and then jumps to A.head.2. Absorbing the Offset: Because list B has one extra node at the front, pointer
p hits null earlier and switches to B.head while q is still finishing list B. When q hits null, it switches to A.head. At this exact moment, p has traveled the length of A plus the prefix of B, and q has traveled the length of B plus the prefix of A.3. Second Pass (The Convergence): Both pointers are now equidistant from the intersection node. Stepping forward in lockstep,
p and q march simultaneously through the shared tail until they land on node 8.Result:
The pointers collide at node 8 on the second pass, returning the correct shared intersection object.
The pointers collide at node 8 on the second pass, returning the correct shared intersection object.
Try this: Given list A = [4, 1, 8, 4, 5] and list B = [5, 6, 1, 8, 4, 5] intersecting at node 8:
1.Trace pointer p and pointer q step-by-step through their first pass.
2.Note the exact node where pointer p hits null and redirects to B.head.
3.Predict the exact iteration count on the second pass where p and q point to node 8 simultaneously.
Practice
Predicting Pointer Paths on Alternate Intersect Scenarios
Phase 4: Practice
Now that you have seen how the dual-pointer traversal equalizes the length difference in our working example ( and ), it is time to test your mental model on a structural variation. Consider what happens when one list is completely disjoint from the other, or when they intersect at their very first nodes.
Imagine you are running the exact same pointer-switching logic on two lists that never intersect: list has length 3 and list has length 4, and neither shares any memory nodes. Before writing any code, trace mentally: what will the pointers and point to after both lists have been fully traversed twice, and what should your loop condition return?
Keep the core invariant in mind: pointer traverses then , while pointer traverses then . If they never share a tail node, they must finish their second traversal at the exact same moment. Think about how their final step resolves to
null and why that prevents an infinite loop.Try this: Consider two non-intersecting lists where List A has 3 nodes and List B has 4 nodes. Trace pointers p (starting at
B) through their full double-traversal cycle. What are the exact values of p and q at the end of iteration step 7 (), and what value does the algorithm return?
a.and q (starting at
B) through their full double-traversal cycle. What are the exact values of p and q at the end of iteration step 7 (), and what value does the algorithm return?
Apply
Applying the Two-Pointer Bridge Pattern to Real-World Graph and Stream Problems
Phase 5: Apply
You have mastered how two runners traversing combined paths and will naturally synchronize at node 8 by absorbing length differences. This same structural trick—routing pointers through each other's full domain to eliminate length offsets—extends far beyond simple singly linked list intersections. Whenever two dependent sequences or streams must be aligned without using extra memory for hash sets, treating the traversal lengths as an additive identity () is your primary tool. Consider how this logic generalizes when pointers move through cyclic graphs or when memory constraints strictly forbid modifying the original node structures.
FAQ
How do lists A = 4→1→8→4→5 and B = 5→6→1→8→4→5 intersect at node 8?
Despite having different prefix lengths (2 vs 3 nodes before the intersection), both lists share the exact same tail nodes starting at 8 (8→4→5). Because nodes in linked lists are memory references, sharing a tail means they merge into the exact same objects.
Why does the two-pointer bridge model work when lists have different lengths?
By having pointer A traverse list A then jump to list B, and pointer B traverse list B then jump to list A, both pointers travel a combined distance equal to (Length A + Length B). This forces them to align perfectly at the intersection node on their second pass.
What is the time and space complexity of the two-pointer intersection approach?
The time complexity is O(N + M), where N and M are the lengths of the two lists, because each pointer traverses at most two lists. The space complexity is O(1) since we only use two pointer variables without extra data structures.
What happens if the two linked lists never intersect?
If there is no intersection, both pointers will reach the null terminator at the end of their second pass simultaneously. The loop terminates naturally, returning null or None.