0
0
DSA Pythonprogramming~20 mins

Intersection Point of Two Linked Lists in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Intersection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Find the intersection node value
Given two linked lists that intersect, what is the value of the intersection node printed by the code?
DSA Python
class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

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

# Create nodes
common = Node(8)
common.next = Node(10)

headA = Node(3)
headA.next = Node(7)
headA.next.next = common

headB = Node(99)
headB.next = Node(1)
headB.next.next = common

intersection = get_intersection_node(headA, headB)
print(intersection.val if intersection else None)
A7
B10
CNone
D8
Attempts:
2 left
💡 Hint
Trace the pointers moving through each list and switching heads when reaching the end.
Predict Output
intermediate
2:00remaining
Output when no intersection exists
What is the output of the code when two linked lists do not intersect?
DSA Python
class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

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

headA = Node(1)
headA.next = Node(2)
headA.next.next = Node(3)

headB = Node(4)
headB.next = Node(5)
headB.next.next = Node(6)

intersection = get_intersection_node(headA, headB)
print(intersection.val if intersection else None)
ANone
B1
C6
D0
Attempts:
2 left
💡 Hint
If no intersection exists, the pointers will both become None at the same time.
🔧 Debug
advanced
2:00remaining
Identify the error in intersection detection code
What error does the following code produce when trying to find the intersection point of two linked lists?
DSA Python
def get_intersection_node(headA, headB):
    ptrA, ptrB = headA, headB
    while ptrA != ptrB:
        ptrA = ptrA.next
        ptrB = ptrB.next
        if ptrA is None and ptrB is None:
            return None
    return ptrA
ASyntaxError due to missing colon
BReturns None correctly when no intersection
CAttributeError when ptrA or ptrB is None
DInfinite loop when lists intersect
Attempts:
2 left
💡 Hint
Check what happens when ptrA or ptrB reaches the end of the list.
🧠 Conceptual
advanced
2:00remaining
Why does switching heads help find intersection?
Why does switching the pointers to the other list's head after reaching the end help find the intersection point?
AIt skips nodes to speed up search
BIt equalizes the path lengths so pointers meet at intersection
CIt prevents infinite loops by breaking early
DIt resets pointers to start over from beginning
Attempts:
2 left
💡 Hint
Think about how different list lengths affect pointer alignment.
Predict Output
expert
3:00remaining
Output of intersection detection with cycle in one list
What is the output of the code when one linked list has a cycle and the other intersects it at a node inside the cycle?
DSA Python
class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

def get_intersection_node(headA, headB):
    ptrA, ptrB = headA, headB
    visited = set()
    while ptrA and ptrB:
        if ptrA == ptrB:
            return ptrA
        if ptrA in visited or ptrB in visited:
            return None
        visited.add(ptrA)
        visited.add(ptrB)
        ptrA = ptrA.next if ptrA else None
        ptrB = ptrB.next if ptrB else None
    return None

# Create cycle list
headA = Node(1)
node2 = Node(2)
node3 = Node(3)
node4 = Node(4)
headA.next = node2
node2.next = node3
node3.next = node4
node4.next = node2  # cycle here

# Create second list intersecting inside cycle
headB = Node(9)
headB.next = node3

intersection = get_intersection_node(headA, headB)
print(intersection.val if intersection else None)
A3
B2
CNone
D4
Attempts:
2 left
💡 Hint
The intersection is at node with value 3 inside the cycle.