Intersection Point of Two Linked Lists in DSA C - Time & Space Complexity
We want to find where two linked lists meet, if at all. Understanding how long this takes helps us write efficient code.
How does the time needed grow as the lists get longer?
Analyze the time complexity of the following code snippet.
// Find intersection node of two singly linked lists
struct Node* getIntersectionNode(struct Node* headA, struct Node* headB) {
struct Node *a = headA, *b = headB;
while (a != b) {
a = (a == NULL) ? headB : a->next;
b = (b == NULL) ? headA : b->next;
}
return a;
}
This code tries to find the node where two linked lists intersect by traversing both lists with two pointers.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Traversing nodes of both linked lists using two pointers.
- How many times: Each pointer moves through its list and then switches to the other list once, repeating until they meet or both reach the end.
As the lists get longer, the pointers travel through each node at most twice before meeting or ending.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 steps (each pointer traverses both lists) |
| 100 | About 200 steps |
| 1000 | About 2000 steps |
Pattern observation: The total steps grow roughly linearly with 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 pointers only traverse one list each, so time is O(max(m, n))"
[OK] Correct: Each pointer may traverse both lists once, so total steps depend on the sum of lengths, not just the longer list.
Understanding this approach shows you can handle linked list problems efficiently by clever pointer use, a skill valued in many coding challenges.
"What if we used a hash set to store visited nodes from one list? How would the time complexity change?"
Practice
intersection point of two linked lists mean?Solution
Step 1: Understand linked list nodes and references
Each node in a linked list has a reference (address) to the next node. Two lists intersect if they share the same node reference, not just the same value.Step 2: Define intersection point
The intersection point is the first node where both lists point to the exact same node object in memory.Final Answer:
The node where both linked lists share the same reference -> Option BQuick Check:
Intersection means shared node reference [OK]
- Confusing same value with same node reference
- Thinking intersection is based on node values
- Assuming intersection is at list ends
Solution
Step 1: Understand correct struct declaration
In C, to declare a linked list node, you define a struct with data and a pointer to the same struct type.Step 2: Check each option
typedef struct Node { int data; struct Node* next; } Node; correctly uses typedef and struct Node with a pointer to struct Node. struct Node { int data; Node* next; }; misses typedef and uses Node* without struct keyword. typedef struct { int data; struct Node* next; } Node; misses struct name. struct Node { int data; struct Node next; }; incorrectly uses struct Node next (not a pointer).Final Answer:
typedef struct Node { int data; struct Node* next; } Node; -> Option DQuick Check:
Correct struct with pointer to same struct [OK]
- Forgetting 'struct' keyword before pointer type
- Using struct variable instead of pointer for next
- Missing typedef or struct name
Node* getIntersectionNode(Node* headA, Node* headB) {
Node *a = headA, *b = headB;
while (a != b) {
a = (a == NULL) ? headB : a->next;
b = (b == NULL) ? headA : b->next;
}
return a;
}
// Lists:
// List A: 1 -> 2 -> 3 \
// 6 -> 7 -> NULL
// List B: 4 -> 5 /
// Intersection at node with value 6
Node* result = getIntersectionNode(headA, headB);
printf("%d", result->data);Solution
Step 1: Understand the two-pointer approach
Two pointers traverse each list; when reaching end, switch to other list head. They meet at intersection or NULL if none.Step 2: Trace pointers on given lists
Lists intersect at node with value 6. Pointers will meet there after switching lists.Final Answer:
6 -> Option CQuick Check:
Two-pointer meet at intersection node value 6 [OK]
- Assuming pointers meet at first node
- Not switching pointers to other list head
- Confusing node value with pointer address
Node* getIntersectionNode(Node* headA, Node* headB) {
Node *a = headA, *b = headB;
while (a != b) {
if (a == NULL || b == NULL) {
a = headB;
b = headA;
} else {
a = a->next;
b = b->next;
}
}
return a;
}Solution
Step 1: Analyze loop termination condition
The loop ends when pointers a and b are equal (same node or NULL).Step 2: Check pointer switching logic
When a or b is NULL, they switch to other list head. But if no intersection, pointers cycle indefinitely without becoming equal.Final Answer:
The loop may run infinitely if no intersection exists -> Option AQuick Check:
Loop lacks proper NULL check, causing infinite loop [OK]
- Assuming pointers always meet
- Comparing node data instead of node pointers
- Not handling empty list inputs
Solution
Step 1: Evaluate time and space complexity of each approach
Two-pointer switching approach; O(n) time, O(1) space uses two pointers switching lists, runs in linear time and constant space. Nested loops; O(n*m) time, O(1) space is nested loops, very slow. Hash set approach; O(n+m) time, O(n) space uses extra space for hash set. Reverse lists; modifies input and O(n) time modifies lists, which is not ideal.Step 2: Choose best approach for efficiency and safety
Two-pointer switching approach; O(n) time, O(1) space is best because it is efficient (O(n)) and does not use extra space or modify lists.Final Answer:
Two-pointer switching approach; O(n) time, O(1) space -> Option AQuick Check:
Two-pointer method is optimal for intersection detection [OK]
- Choosing nested loops causing slow performance
- Using extra memory unnecessarily
- Modifying input lists causing side effects
Ready to prove it?
Pass the test and this pattern moves unknown
