By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
(Complete Guide for FAANG-Level Interviews)
"This problem tests your ability to optimize space and time—nail it, and you prove you can handle real-world constraints like memory limits and edge cases in under 30 minutes."
current = current.next
(Repeatable mental checklist for every linked list intersection problem.)
null
Are cycles possible? (If yes, use Floyd’s first.)
Compute Lengths
lenA
lenB
If lengths differ, skip extra nodes in the longer list.
Align Pointers
|lenA - lenB|
Now both pointers are equidistant from the end.
Traverse in Sync
If either reaches null, return null (no intersection).
Edge Cases
No intersection (return null).
Optimize for Follow-Ups
Problem: Given two singly linked lists, return the node where they intersect (or null if no intersection).
Solution (Two-Pointer Technique):
def getIntersectionNode(headA, headB): # Step 1: Compute lengths lenA, lenB = 0, 0 currA, currB = headA, headB while currA: lenA += 1 currA = currA.next while currB: lenB += 1 currB = currB.next # Step 2: Align pointers currA, currB = headA, headB if lenA > lenB: for _ in range(lenA - lenB): currA = currA.next else: for _ in range(lenB - lenA): currB = currB.next # Step 3: Traverse in sync while currA and currB: if currA == currB: return currA currA = currA.next currB = currB.next return None
Time Complexity: O(n + m) Space Complexity: O(1)
Why This Works: - By aligning pointers, we ensure both traverse the same number of nodes before reaching the end. - If they intersect, they must meet at the intersection node.
Problem: Find the intersection node of two linked lists that may contain cycles.
Solution (Floyd’s + Two-Pointer):
def getIntersectionNodeWithCycle(headA, headB): # Step 1: Detect cycles (Floyd's algorithm) def detectCycle(head): slow = fast = head while fast and fast.next: slow = slow.next fast = fast.next.next if slow == fast: return slow # Cycle detected return None cycleA = detectCycle(headA) cycleB = detectCycle(headB) # Case 1: No cycles (use standard two-pointer) if not cycleA and not cycleB: return getIntersectionNode(headA, headB) # Case 2: One list has a cycle, the other doesn't (no intersection) if (cycleA and not cycleB) or (cycleB and not cycleA): return None # Case 3: Both have cycles (check if cycles are the same) # Traverse cycleA to see if cycleB is reachable temp = cycleA.next while temp != cycleA: if temp == cycleB: break temp = temp.next if temp != cycleB: return None # Different cycles, no intersection # Now find intersection (same as standard two-pointer) lenA, lenB = 0, 0 currA, currB = headA, headB while currA != cycleA: lenA += 1 currA = currA.next while currB != cycleB: lenB += 1 currB = currB.next currA, currB = headA, headB if lenA > lenB: for _ in range(lenA - lenB): currA = currA.next else: for _ in range(lenB - lenA): currB = currB.next while currA != cycleA and currB != cycleB: if currA == currB: return currA currA = currA.next currB = currB.next return cycleA # Intersection is the cycle start
Why This Works: - First detect cycles (if any). - If both lists have cycles, check if they share the same cycle. - If they do, find the intersection before the cycle starts.
Problem: Find the intersection node without computing lengths (optimized two-pointer).
Solution (Two-Pointer Swap):
def getIntersectionNodeOptimized(headA, headB): if not headA or not headB: return None ptrA, ptrB = headA, headB while ptrA != ptrB: ptrA = ptrA.next if ptrA else headB ptrB = ptrB.next if ptrB else headA return ptrA
Why This Works: - Pointers traverse both lists, swapping to the other list’s head when they reach the end. - They meet at the intersection after traversing lenA + lenB nodes.
lenA + lenB
if not headA or not headB: return None
while curr: len += 1; curr = curr.next
abs(lenA - lenB)
currA
currA.val
headA == headB
"Alright, let’s lock this in. For linked list intersection problems, here’s your battle plan:
For follow-ups, remember: - No lengths? Use the two-pointer swap trick. - Cycles? Detect them first with Floyd’s algorithm.
You’ve got this. Now go crush that interview!
This framework ensures you never panic when this problem appears. Good luck! ?
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.