Intersection Point of Two Linked Lists in DSA Java - Time & Space Complexity
We want to know how long it takes to find where two linked lists meet.
How does the time grow as the lists get longer?
Analyze the time complexity of the following code snippet.
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode a = headA, b = headB;
while (a != b) {
a = (a == null) ? headB : a.next;
b = (b == null) ? headA : b.next;
}
return a;
}
This code finds the node where two linked lists intersect by switching heads when reaching the end.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Traversing nodes in both linked lists.
- How many times: Each pointer moves through each list at most twice.
As the lists get longer, the pointers travel through all nodes in both lists.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 steps (10 + 10) |
| 100 | About 200 steps (100 + 100) |
| 1000 | About 2000 steps (1000 + 1000) |
Pattern observation: The total steps grow roughly proportional to the sum of the lengths of both lists.
Time Complexity: O(m + n)
This means the time grows linearly with the total number of nodes in both lists combined.
[X] Wrong: "The loop runs forever or more than twice the list length."
[OK] Correct: Because each pointer switches lists once, they cover all nodes at most twice, so the loop ends.
Understanding this helps you explain how to efficiently find shared parts of linked lists, a common real-world problem.
"What if we used a hash set to store visited nodes instead? How would the time complexity change?"
Practice
intersection point of two linked lists mean?Solution
Step 1: Understand linked list nodes
Each node in a linked list has a value and a reference (memory address) to the next node.Step 2: Define intersection point
The intersection point is where two linked lists share the exact same node reference, meaning they merge at that node.Final Answer:
The node where both linked lists share the same reference (memory address). -> Option AQuick Check:
Intersection means shared node reference [OK]
- Confusing same value with same node reference
- Thinking intersection is about list length
- Assuming intersection is always at the last node
Solution
Step 1: Understand method return type
The method should return the intersecting node, so return type must be ListNode.Step 2: Check parameter types
Parameters must be the heads of the two linked lists, which are ListNode references.Final Answer:
public ListNode getIntersectionNode(ListNode headA, ListNode headB) -> Option DQuick Check:
Return node, input heads as ListNode [OK]
- Using void return type instead of ListNode
- Using primitive types instead of ListNode
- Incorrect parameter types
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode a = headA, b = headB;
while (a != b) {
a = (a == null) ? headB : a.next;
b = (b == null) ? headA : b.next;
}
return a;
}If the two linked lists intersect at node with value 8, what will be the output when printing
getIntersectionNode(headA, headB).val?Solution
Step 1: Understand the two-pointer approach
Two pointers traverse each list; when one reaches end, it switches to the other list's head. They meet at intersection.Step 2: Identify intersection value
Since lists intersect at node with value 8, pointers meet there and return that node.Final Answer:
8 -> Option BQuick Check:
Two-pointer meet at intersection node value 8 [OK]
- Assuming output is null if lists differ in length
- Confusing node value with node reference
- Not understanding pointer switching logic
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
while (headA != headB) {
headA = headA.next;
headB = headB.next;
}
return headA;
}What is the main problem with this code?
Solution
Step 1: Analyze pointer movement
Both pointers move forward without switching lists, so if lengths differ, one pointer reaches null first.Step 2: Identify error cause
When one pointer is null and the other is not, accessing next causes NullPointerException.Final Answer:
It does not handle different list lengths, causing NullPointerException. -> Option AQuick Check:
Different lengths cause null access error [OK]
- Ignoring list length difference
- Assuming nodes with same value are intersection
- Not checking for null before accessing next
Solution
Step 1: Calculate lengths and difference
Find lengths of both lists and compute difference (7 - 5 = 2).Step 2: Advance longer list pointer
Move pointer of longer list (List B) ahead by difference to align both pointers equally from intersection.Step 3: Move both pointers together to find intersection
Advance both pointers one step at a time until they meet at intersection node.Final Answer:
Calculate lengths, advance longer list by difference, then move both pointers together. -> Option CQuick Check:
Align pointers by length difference for O(n) solution [OK]
- Using extra space like hash sets (not O(1) space)
- Reversing lists which is inefficient and modifies data
- Not adjusting for length difference causing wrong results
Ready to prove it?
Pass the test and this pattern moves unknown
