The Most Beautiful Solution on LeetCode
Intersection of Two Linked Lists (#160) has one of the most beautiful solutions in all of LeetCode. It is the kind of problem where the brute force approach is obvious, but the optimal solution feels like a magic trick until you understand the math behind it.
The problem asks you to find the node where two singly linked lists merge into one. Not a node with the same value — the exact same node object in memory. If the lists never intersect, return null.
What makes this problem special is the "switch heads" two-pointer technique. When a pointer reaches the end of its list, you redirect it to the head of the other list. After at most one redirect each, the two pointers are guaranteed to meet at the intersection node — or both reach null simultaneously if there is no intersection.
This approach runs in O(n + m) time and O(1) space. No hash sets, no length calculations, no modifying the lists. Just two pointers walking in lockstep until they collide.
Understanding the Intersection of Two Linked Lists Problem
You are given the heads of two singly linked lists, headA and headB. The two lists may converge at some node, after which they share every subsequent node. Your job is to return the node where the intersection begins, or null if the lists do not intersect.
A critical detail that trips people up: the intersection is defined by reference, not by value. Two nodes can both hold the value 8, but if they are different objects in memory, they are not the same intersection. The problem is asking you to find where the two lists physically share the same node.
Consider this example. ListA is 4 -> 1 -> 8 -> 4 -> 5, and ListB is 5 -> 6 -> 1 -> 8 -> 4 -> 5. The intersection begins at the node with value 8. From that node onward, both lists share the exact same nodes in memory.
The Elegant Two-Pointer Approach
The optimal approach uses two pointers, pA and pB, starting at headA and headB respectively. Both advance one node at a time. When pA reaches the end of ListA, redirect it to headB. When pB reaches the end of ListB, redirect it to headA.
After the redirect, something remarkable happens. Both pointers are now the exact same distance from the intersection node. They will walk in perfect lockstep until they collide at the intersection — or both reach null at the same time if the lists do not share any nodes.
The algorithm is shockingly simple. Initialize pA to headA and pB to headB. While pA is not equal to pB, advance each pointer. If pA is null, set it to headB; otherwise move to pA.next. Same logic for pB. When the loop exits, pA (or pB) is the intersection node, or null.
- Initialize pA = headA, pB = headB
- While pA !== pB: advance each pointer one step
- If a pointer hits null, redirect to the OTHER list head
- When pA === pB, you have found the intersection (or both are null)
The Key Insight
When pointer A reaches null, redirect to headB. When pointer B reaches null, redirect to headA. After one redirect each, both pointers are the same distance from the intersection.
Why the Switch Heads Technique Works
The math behind this approach is elegant. Let lenA be the length of ListA and lenB be the length of ListB. Let c be the length of the shared tail (from the intersection node to the end). The unique portion of ListA has length a = lenA - c, and the unique portion of ListB has length b = lenB - c.
Pointer A travels: a nodes (unique part of A) + c nodes (shared tail) + b nodes (unique part of B) = a + c + b. Pointer B travels: b nodes (unique part of B) + c nodes (shared tail) + a nodes (unique part of A) = b + c + a. Both pointers travel exactly a + b + c total nodes.
Since both pointers travel the same total distance and end up at the same point, they are guaranteed to arrive at the intersection simultaneously. The difference in path length before the intersection cancels out perfectly.
If there is no intersection, both pointers still travel lenA + lenB total steps and both land on null at the same time. The loop exits with pA === pB === null, which is the correct answer.
Visual Walkthrough of Intersection of Two Linked Lists
Let us trace through the example. ListA: 4 -> 1 -> 8 -> 4 -> 5. ListB: 5 -> 6 -> 1 -> 8 -> 4 -> 5. The intersection node has value 8. ListA has length 5 (unique part: 4, 1 = 2 nodes). ListB has length 6 (unique part: 5, 6, 1 = 3 nodes). Shared tail: 8, 4, 5 = 3 nodes.
Pointer A starts at 4 (ListA). Pointer B starts at 5 (ListB). They advance in lockstep. After 5 steps, pointer A reaches null and redirects to headB. After 6 steps, pointer B reaches null and redirects to headA.
Now pointer A is walking through ListB, and pointer B is walking through ListA. After the redirect, pointer A has traveled 5 + 3 = 8 steps to reach node 8. Pointer B has traveled 6 + 2 = 8 steps to reach node 8. They meet at the intersection.
- 1pA = 4, pB = 5 — both advance
- 2pA = 1, pB = 6 — both advance
- 3pA = 8, pB = 1 — both advance
- 4pA = 4, pB = 8 — both advance
- 5pA = 5, pB = 4 — both advance
- 6pA = null -> redirect to headB (5), pB = 5 — both advance
- 7pA = 6, pB = null -> redirect to headA (4) — both advance
- 8pA = 1, pB = 1 — both advance
- 9pA = 8, pB = 8 — pA === pB, intersection found!
Reference vs Value
Compare node REFERENCES, not values — two nodes with value 8 at different positions are NOT the same intersection. The intersection must be the exact same node object.
Edge Cases to Handle
The two-pointer approach handles every edge case naturally, but you should be aware of them for interviews. The most common edge case is when there is no intersection at all. Both pointers will travel lenA + lenB steps and both land on null simultaneously, returning null correctly.
Another edge case is when one list is entirely contained in the other. For example, if ListA is 1 -> 2 -> 3 and ListB is 2 -> 3 (where node 2 is the same object). The pointers still equalize path length and meet at node 2.
If both lists start at the same node, the first comparison pA === pB is true immediately, and the intersection is the head itself. Single-node lists also work correctly — if both heads point to the same node, they match instantly.
- No intersection: both pointers reach null simultaneously
- One list contained in the other: pointers still equalize and meet correctly
- Same starting node: pA === pB on first check, returns immediately
- Single node lists: works if both point to the same node, returns null otherwise
- Lists of very different lengths: the redirect trick compensates for the length difference
What Intersection of Two Linked Lists Teaches You
This problem teaches the powerful "equalize path length" trick that appears in multiple linked list problems. The idea that you can force two pointers to travel the same total distance by redirecting them is a technique worth internalizing.
The same mathematical insight applies to finding the linked list cycle start in LeetCode #142 and to problems where you need to align two sequences of different lengths. Once you recognize the pattern, it clicks across many problems.
The intersection of two linked lists two-pointer approach also demonstrates why O(1) space solutions are worth pursuing. A hash set solution works in O(n) time but requires O(n) space. The two-pointer approach matches the time complexity with zero extra memory.
YeetCode flashcards help you drill the linked list meeting point pattern through spaced repetition. Instead of re-deriving the math each time, you build the intuition so the "switch heads" technique becomes second nature when you see it in an interview.
Why This Problem Matters
Intersection of Two Linked Lists (#160) has one of the most elegant solutions on LeetCode — the math behind the "switch heads" technique is beautiful: both pointers travel lenA + lenB total distance.