Intersection Point of Two Linked Lists in DSA C++ - Time & Space Complexity
We want to know how the time needed to find the intersection point of two linked lists changes as the lists get longer.
How does the number of steps grow when the lists have more nodes?
Analyze the time complexity of the following code snippet.
// Find intersection point of two singly linked lists
ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {
ListNode* a = headA;
ListNode* b = headB;
while (a != b) {
a = (a == nullptr) ? headB : a->next;
b = (b == nullptr) ? headA : b->next;
}
return a;
}
This code finds the node where two linked lists meet by traversing both lists with two pointers.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that moves pointers through the lists.
- How many times: Each pointer moves at most twice the length of the longer list.
As the lists get longer, the pointers walk through each node up to two times.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 steps |
| 100 | About 200 steps |
| 1000 | About 2000 steps |
Pattern observation: The steps grow roughly twice as fast as the list length, so the growth is linear.
Time Complexity: O(n + m)
This means the time grows linearly with the total number of nodes in both lists combined.
[X] Wrong: "The loop runs infinitely if there is no intersection."
[OK] Correct: The pointers switch lists once, so they will both reach the end (nullptr) and stop, ensuring the loop ends.
Understanding this time complexity helps you explain how efficient your solution is and shows you can analyze linked list problems clearly.
"What if we used hash sets to store visited nodes? 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 (address) to the next node.Step 2: Define intersection point
The intersection point is where two linked lists share the exact same node reference, not just the same value.Final Answer:
The node where both linked lists share the same reference (memory address). -> Option DQuick Check:
Intersection means shared node reference = A [OK]
- Confusing same value with same node reference
- Thinking intersection is the last node always
- Assuming intersection means equal length lists
Solution
Step 1: Understand pointer usage in C++ structs
A linked list node must have a pointer to the next node, soNode* next;is correct.Step 2: Analyze options
struct Node { int data; Node* next; }; usesNode* next;which is correct. Options A, B, and D misuse pointer or function syntax.Final Answer:
struct Node { int data; Node* next; }; -> Option AQuick Check:
Pointer to next node = C [OK]
- Forgetting the * pointer for next node
- Using function syntax instead of pointer
- Declaring next as object instead of pointer
Node* getIntersectionNode(Node* headA, Node* headB) {
Node* a = headA;
Node* b = headB;
while (a != b) {
a = a ? a->next : headB;
b = b ? b->next : headA;
}
return a;
}
// Lists:
// A: 3 -> 6 -> 9 -> 7 -> 10
// B: 1 -> 7 -> 10
Node* intersection = getIntersectionNode(headA, headB);
if (intersection) std::cout << intersection->data << std::endl;
else std::cout << -1 << std::endl;
Solution
Step 1: Understand the algorithm
This code uses two pointers traversing both lists; when one reaches end, it switches to the other list's head. They meet at intersection.Step 2: Trace the pointers
Both pointers meet at node with value 7, which is the intersection point.Final Answer:
7 -> Option CQuick Check:
Intersection node data = 7 [OK]
- Assuming intersection is node with value 10
- Printing -1 when intersection exists
- Confusing node value with node reference
Node* getIntersection(Node* headA, Node* headB) {
while (headA != headB) {
headA = headA->next;
headB = headB->next;
}
return headA;
}Solution
Step 1: Analyze pointer movement
The code moves both pointers forward simultaneously without adjusting for different list lengths.Step 2: Identify consequence
If lists differ in length, one pointer may reach null before the other, causing a null pointer dereference or infinite loop.Final Answer:
It does not handle different list lengths, causing null pointer errors. -> Option AQuick Check:
Unequal lengths cause errors = D [OK]
- Ignoring list length difference
- Dereferencing null pointers
- Assuming simultaneous traversal always works
Solution
Step 1: Understand length difference method
Calculate difference (8 - 5 = 3), advance longer list by 3 nodes to align both lists.Step 2: Traverse both lists together
Move both pointers one step at a time; the first node where they match is the intersection.Final Answer:
Calculate length difference, advance longer list by difference, then move both pointers together. -> Option BQuick Check:
Align by length difference then traverse = B [OK]
- Comparing node values instead of references
- Reversing lists unnecessarily
- Using extra space when not needed
Ready to prove it?
Pass the test and this pattern moves unknown
