Hello! Welcome to your next lesson in our linked lists module.
In our previous lessons, we've seen how pointer manipulation can lead to elegant solutions, like using slow and fast pointers to find the middle of a list or detect a cycle. Today, we'll tackle another classic problem that showcases a different, but equally clever, pointer-based technique.
Your learning outcome for this lesson is to find the intersection point of two linked lists. This is a frequently asked question in technical interviews, and understanding the optimal solution demonstrates a deep grasp of linked list mechanics.
1. Defining the Problem: What is an Intersection?
When we say two linked lists intersect, we don't just mean they contain nodes with the same value. We mean that at some point, they start sharing the exact same node objects, forming a 'Y' shape. From the intersection point onwards, the rest of the list is identical for both.

If the lists do not intersect at all, your function should return null.
2. Approach 1: The Hashing Method
As with many problems, a good first step in an interview is to find a working solution, even if it's not the most optimal one. A straightforward approach here involves using extra memory.
The idea is simple:
- Traverse the first linked list (say, List A).
- Store each node you visit in a
HashSet. AHashSetis a good choice here because it provides average time complexity for insertion and lookup. - Traverse the second linked list (List B). For each node, check if it already exists in the
HashSet. - The very first node from List B that you find in the set is the intersection point. If you reach the end of List B without finding any, there is no intersection.
This video from 'take U forward' explains this hashing approach clearly.
L12. Find the intersection point of Y LinkedList
This first part of the video explains the brute-force solution using a hash map (or hash set) to solve the problem.
Watch from the beginning (00:03) until 08:10. Focus on the logic of storing nodes from one list and checking them against the second list.
Complexity Analysis
- Time Complexity: , where and are the lengths of the two lists. You traverse each list once.
- Space Complexity: (or if you store the second list). This is because you need to store all nodes of one list in the hash set.
In an interview, the follow-up question would almost certainly be: "Can you solve this with space complexity?"
3. Approach 2: The Length Difference Method
To achieve space, we need to eliminate the HashSet. This requires a more creative use of pointers.
Consider why a simple parallel traversal (moving one pointer in each list simultaneously) doesn't work: if the lists have different lengths before the intersection, the pointers will be out of sync.

The key insight for this approach is to compensate for the length difference.
- Calculate Lengths: Traverse both lists to find their lengths, and .
- Find Difference: Calculate the absolute difference, .
- Align Pointers: Move the pointer of the longer list forward by nodes.
- Traverse Together: Now that both pointers are equidistant from the end of their respective lists, traverse both lists simultaneously. The node at which the two pointers become equal is the intersection point.
This method is also explained well in the 'take U forward' video.
L12. Find the intersection point of Y LinkedList
This next segment demonstrates the optimized approach that avoids extra space by calculating the length difference.
Continue watching from 08:10 to 15:56. Pay attention to how aligning the starting points of the traversal solves the problem.
Complexity Analysis
- Time Complexity: . You traverse both lists to find lengths, then traverse them again.
- Space Complexity: . We only use a few pointers and variables.
This is a great solution! It meets the space complexity requirement. However, there's an even more elegant and concise way to solve this, which is often considered the "gold standard" solution in interviews.
4. Approach 3: The Optimal Two-Pointer Technique
This final approach is brilliant because it cleverly equalizes the traversal paths of the two pointers without explicitly calculating the lengths first.
Here's the algorithm:
- Initialize two pointers,
ptrAatheadAandptrBatheadB. - Traverse both lists. In each step of a
whileloop, advanceptrAandptrBby one node. - Here's the trick: If
ptrAreaches the end of List A (null), redirect it toheadB. Similarly, ifptrBreaches the end of List B, redirect it toheadA. - The loop continues until
ptrAandptrBare equal. The node they meet at is the intersection. If they never intersect, they will both becomenullat the same time, and the loop will terminate, correctly returningnull.
Why does this work?
Imagine the total path ptrA travels: it's the length of List A plus the length of List B's unique part. The total path ptrB travels is the length of List B plus the length of List A's unique part.
- Path of
ptrA:(length of A's unique part) + (length of common part) + (length of B's unique part) - Path of
ptrB:(length of B's unique part) + (length of common part) + (length of A's unique part)
The paths are identical in composition and length! By having them traverse each other's paths after their own, they are guaranteed to meet at the intersection.
The following resource provides a fantastic explanation and a step-by-step walkthrough of this intuition.
160. Intersection of Two Linked Lists - In-Depth Explanation
The article '160. Intersection of Two Linked Lists - In-Depth Explanation' from AlgoMonster gives one of the best text-based explanations of this technique's intuition.
Please read the 'Intuition' and 'Example Walkthrough' sections. They do an excellent job of explaining why concatenating the traversal paths makes the pointers meet at the right spot.
Java Implementation
The implementation for this approach is surprisingly concise.
Intersection point of two Linked Lists
The GeeksforGeeks article provides a clean Java implementation of this two-pointer technique.
Scroll down to '[Expected Approach - 2] Using Two Pointer Technique'. Focus on the Java code snippet provided (it will be in chunk 37 of the full source). Notice how compact the logic is.
Here's the core of the Java code, which you'll find in the resources:
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode ptrA = headA;
ListNode ptrB = headB;
// The loop terminates when ptrA == ptrB.
// This will be the intersection node, or null if they don't intersect.
while (ptrA != ptrB) {
// If ptrA reaches the end, redirect it to headB. Otherwise, advance it.
ptrA = (ptrA == null) ? headB : ptrA.next;
// If ptrB reaches the end, redirect it to headA. Otherwise, advance it.
ptrB = (ptrB == null) ? headA : ptrB.next;
}
return ptrA; // or ptrB, they are the same
}
}
Complexity Analysis
- Time Complexity: . In the worst case, each pointer travels through both lists.
- Space Complexity: .
Test your understanding!
Consider two non-intersecting linked lists:List A: 1 -> 2 -> 3List B: 4 -> 5
Trace the movement of ptrA and ptrB using the optimal two-pointer algorithm. At what point does the while (ptrA != ptrB) loop terminate, and what value is returned?
Show answer
Let's trace it:
| Step | ptrA Value |
ptrB Value |
ptrA == ptrB? |
|---|---|---|---|
| 0 | 1 | 4 | No |
| 1 | 2 | 5 | No |
| 2 | 3 | null |
No |
| 3 | null |
1 (switched) | No |
| 4 | 4 (switched) | 2 | No |
| 5 | 5 | 3 | No |
| 6 | null |
null |
Yes! |
The pointers ptrA and ptrB become equal when both are null. The loop terminates, and the function returns ptrA (which is null), correctly indicating no intersection. This proves the algorithm works for non-intersecting lists too.
Conclusion
Excellent work! You have now explored three different ways to solve this problem, progressing from a simple, space-consuming method to a highly efficient and clever constant-space solution. This progression mimics the thought process expected in a technical interview.
Key Takeaways:
- Problem-Solving Trajectory: It's often best to start with a brute-force or simple solution (like hashing) and then optimize for time or space.
- Length-Difference Technique: Aligning pointers by accounting for path length differences is a valid space strategy.
- Optimal Two-Pointer Swap: The most elegant solution involves making the pointers traverse a combined path (
A + BandB + A), which cleverly handles length differences without explicit calculation. This is a pattern worth remembering.
Preview of the next lesson:
We will continue to hone our pointer manipulation skills. In the next lesson, you will learn to merge two sorted linked lists iteratively. This is another foundational problem that is a building block for more complex algorithms like merge sort on linked lists.