Intersection Point of Two Linked Lists in DSA Python - Time & Space Complexity
We want to understand 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.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def get_intersection_node(headA, headB):
ptrA, ptrB = headA, headB
while ptrA != ptrB:
ptrA = ptrA.next if ptrA else headB
ptrB = ptrB.next if ptrB else headA
return ptrA
This code finds the node where two linked lists first meet or returns None if they don't intersect.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Moving pointers forward through each list node.
- How many times: Each pointer moves at most twice the length of the longer list.
As the lists get longer, the pointers move through each node, then switch to the other list once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 pointer moves |
| 100 | About 200 pointer moves |
| 1000 | About 2000 pointer moves |
Pattern observation: The number of steps grows roughly twice as fast as the length of the longer list.
Time Complexity: O(n + m)
This means the time grows linearly with the total length of both lists combined.
[X] Wrong: "The pointers only move once through each list, so itβs O(n)."
[OK] Correct: Each pointer can traverse both lists once, so the total steps add up to the sum of both list lengths, not just one.
Understanding this time complexity helps you explain how efficient your solution is when lists get large, showing you can reason about real-world data sizes.
"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
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, not just the same value.Final Answer:
The node where both linked lists share the same reference (memory address). -> 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 always at the start
headA and headB that intersect at node with value 8?Solution
Step 1: Understand intersection setup
To create intersection, both lists must share the same node object for the intersecting node.Step 2: Check code correctness
headA = Node(3) headB = Node(99) node = Node(8) headA.next = node headB.next = node creates node with value 8 once and assigns it as next for both lists, ensuring intersection.Final Answer:
headA and headB share the same node object for value 8. -> Option CQuick Check:
Shared node object creates intersection [OK]
- Creating separate nodes with same value instead of shared node
- Assigning next pointers incorrectly
- Not linking nodes properly
class Node:
def __init__(self, val):
self.val = val
self.next = None
def get_intersection(headA, headB):
a, b = headA, headB
while a != b:
a = a.next if a else headB
b = b.next if b else headA
return a.val if a else None
# Setup
common = Node(7)
common.next = Node(10)
headA = Node(3)
headA.next = Node(6)
headA.next.next = common
headB = Node(99)
headB.next = common
print(get_intersection(headA, headB))Solution
Step 1: Understand the intersection nodes
Both lists share node with value 7 and then 10. Intersection is at node with value 7.Step 2: Trace the get_intersection function
Pointers a and b traverse lists; when they meet at node 7, function returns 7.Final Answer:
7 -> Option AQuick Check:
First shared node value is 7 [OK]
- Returning value of second shared node
- Returning None if not careful with pointer resets
- Confusing node values with references
def get_intersection(headA, headB):
a, b = headA, headB
while a != b:
a = a.next
b = b.next
return a.val if a else NoneSolution
Step 1: Analyze pointer movement
The code moves pointers a and b forward but never resets them to the other list's head.Step 2: Understand consequence
Without resetting, pointers may reach None at different times and never meet, missing intersection.Final Answer:
It does not reset pointers to the other list when reaching the end. -> Option DQuick Check:
Resetting pointers is key for intersection detection [OK]
- Not resetting pointers causes infinite loop or no detection
- Comparing values instead of references
- Returning wrong node value
Options:
A) Use two pointers starting at heads; move each pointer to the other list's head when reaching end until they meet.
B) Use a hash set to store nodes of first list, then check nodes of second list for membership.
C) Compare every node of first list with every node of second list.
D) Reverse both lists and compare nodes from the start until mismatch.
Solution
Step 1: Analyze time and space complexity
Two-pointer switching heads method until pointers meet. uses two pointers and no extra space, running in O(m+n) time and O(1) space.Step 2: Compare other options
Hash set membership check for intersection. uses extra space (hash set), Nested loops comparing all nodes pairwise. is O(m*n), Reverse lists and compare nodes from start. modifies lists and is inefficient.Final Answer:
Two-pointer switching heads method until pointers meet. -> Option AQuick Check:
Two-pointer method is optimal for intersection [OK]
- Using extra space unnecessarily
- Using nested loops causing slow performance
- Modifying lists destructively
Ready to prove it?
Pass the test and this pattern moves unknown
