Intersection Point of Two Linked Lists in DSA Typescript - 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.
function getIntersectionNode(headA: ListNode | null, headB: ListNode | null): ListNode | null {
if (!headA || !headB) return null;
let a = headA;
let b = headB;
while (a !== b) {
a = a ? a.next : headB;
b = b ? b.next : headA;
}
return a;
}
This code finds the node where two linked lists first meet or returns null if they don't meet.
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 lists combined.
As the lists get longer, the pointers walk through each node at most twice.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 pointer moves |
| 100 | About 200 pointer moves |
| 1000 | About 2000 pointer moves |
Pattern observation: The operations grow roughly in direct proportion to the total length of both lists.
Time Complexity: O(m + n)
This means the time to find the intersection grows linearly with the sum of the lengths of the two lists.
[X] Wrong: "The loop runs forever or depends on the length of only one list."
[OK] Correct: Each pointer switches to the other list after reaching the end, so they both traverse the combined length, ensuring the loop ends.
Understanding this approach shows you can handle linked list problems efficiently and think about pointer movement clearly.
What if we changed the pointers to move only once through each list without switching? How would the time complexity change?
Practice
Solution
Step 1: Understand the problem goal
The problem asks to find the node where two linked lists join or share the same reference, not just the value.Step 2: Clarify what intersection means
Intersection means the exact same node object, not just nodes with equal values.Final Answer:
The node where two linked lists merge or intersect -> Option AQuick Check:
Intersection point = node shared by both lists [OK]
- Confusing intersection with nodes having same values
- Thinking intersection means equal length lists
- Looking for sum or length instead of node
Solution
Step 1: Check for class declaration with constructor
class ListNode { val: number; next: ListNode | null; constructor(val: number) { this.val = val; this.next = null; } } correctly defines a class with a constructor initializing val and next properties.Step 2: Verify type safety and nullability
class ListNode { val: number; next: ListNode | null; constructor(val: number) { this.val = val; this.next = null; } } uses 'next: ListNode | null' which is correct for linked list end.Final Answer:
class ListNode { val: number; next: ListNode | null; constructor(val: number) { this.val = val; this.next = null; } } -> Option DQuick Check:
Proper class with constructor and nullable next [OK]
- Using interface without constructor
- Missing null for next pointer
- Using function syntax without types
class ListNode {
val: number;
next: ListNode | null;
constructor(val: number) {
this.val = val;
this.next = null;
}
}
function getIntersectionNode(headA: ListNode | null, headB: ListNode | null): ListNode | null {
let pA = headA;
let pB = headB;
while (pA !== pB) {
pA = pA ? pA.next : headB;
pB = pB ? pB.next : headA;
}
return pA;
}
// Create nodes
const common = new ListNode(8);
common.next = new ListNode(10);
const listA = new ListNode(3);
listA.next = new ListNode(7);
listA.next.next = common;
const listB = new ListNode(99);
listB.next = common;
const intersection = getIntersectionNode(listA, listB);
console.log(intersection ? intersection.val : null);Solution
Step 1: Trace the linked lists and intersection node
List A: 3 -> 7 -> 8 -> 10; List B: 99 -> 8 -> 10; common intersection starts at node with value 8.Step 2: Understand the two-pointer approach
Pointers pA and pB traverse each list; when one reaches end, it switches to the other list's head. They meet at intersection node 8.Final Answer:
8 -> Option CQuick Check:
Intersection node value = 8 [OK]
- Returning node value 7 or 10 instead of 8
- Confusing node values with references
- Not switching pointers to other list head
function getIntersectionNode(headA: ListNode | null, headB: ListNode | null): ListNode | null {
let pA = headA;
let pB = headB;
while (pA !== pB) {
pA = pA.next;
pB = pB.next;
if (!pA && !pB) return null;
}
return pA;
}Solution
Step 1: Analyze pointer movement in the loop
Both pointers move forward but do not switch to the other list's head when reaching null, so they may never meet.Step 2: Understand the standard approach
To find intersection, pointers must switch to the other list's head after reaching null to align lengths.Final Answer:
Pointers do not switch to the other list's head after reaching null -> Option AQuick Check:
Switch pointers to other list head to find intersection [OK]
- Not switching pointers causes infinite loop or wrong result
- Returning null too early without full traversal
- Incorrect loop condition
Solution
Step 1: Calculate lengths and difference
List A length = 7, List B length = 5, difference = 2.Step 2: Advance longer list pointer by difference
Move pointer in List A forward by 2 nodes to align with List B pointer.Step 3: Move both pointers together to find intersection
Advance both pointers step-by-step until they meet at the intersection node.Final Answer:
Calculate lengths, advance longer list by difference, then move both pointers together -> Option BQuick Check:
Align pointers by length difference to find intersection [OK]
- Comparing node values instead of references
- Reversing lists wastes time and breaks structure
- Using hash set wastes extra space unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
