Find where two linked lists intersect. Compare nested loops, HashSet, length difference and the pointer-switch trick.
Problem
Two singly linked lists may merge at some node (by reference, not by value). Return that node or null.
Pattern
Two pointers that equalise path lengths: pointer A walks list A then list B; pointer B walks list B then list A. Both travel a + b steps, so they meet at the intersection (or both reach null together).
final class LNode {
final int val;
LNode next;
LNode(int val) { this.val = val; }
}Approach 1: Nested loops (feasible, O(m·n))
class IntersectBrute {
static LNode find(LNode a, LNode b) {
for (LNode x = a; x != null; x = x.next)
for (LNode y = b; y != null; y = y.next)
if (x == y) return x;
return null;
}
}Approach 2: HashSet of nodes from A (O(m+n) time, O(m) space)
Uses identity: LNode does not override equals, so the set compares references.
import java.util.HashSet;
import java.util.Set;
class IntersectHashSet {
static LNode find(LNode a, LNode b) {
Set<LNode> seen = new HashSet<>();
for (LNode x = a; x != null; x = x.next) seen.add(x);
for (LNode y = b; y != null; y = y.next) if (seen.contains(y)) return y;
return null;
}
}Approach 3: Length difference (O(m+n), O(1))
class IntersectLengths {
static int length(LNode n) { int len = 0; for (; n != null; n = n.next) len++; return len; }
static LNode find(LNode a, LNode b) {
int la = length(a), lb = length(b);
while (la > lb) { a = a.next; la--; }
while (lb > la) { b = b.next; lb--; }
while (a != b) { a = a.next; b = b.next; }
return a;
}
}Approach 4: Pointer switching (O(m+n), O(1), no length pass) — optimal
class IntersectSwitch {
static LNode find(LNode headA, LNode headB) {
LNode a = headA, b = headB;
while (a != b) {
a = a == null ? headB : a.next;
b = b == null ? headA : b.next;
}
return a; // intersection or null
}
}
class IntersectDemo {
public static void main(String[] args) {
LNode common = new LNode(8); common.next = new LNode(4); common.next.next = new LNode(5);
LNode a = new LNode(4); a.next = new LNode(1); a.next.next = common;
LNode b = new LNode(5); b.next = new LNode(6); b.next.next = new LNode(1); b.next.next.next = common;
System.out.println(IntersectBrute.find(a, b).val + " " + IntersectHashSet.find(a, b).val + " "
+ IntersectLengths.find(a, b).val + " " + IntersectSwitch.find(a, b).val); // 8 8 8 8
System.out.println(IntersectSwitch.find(new LNode(1), new LNode(2))); // null
}
}Feasible → optimal
O(m·n) → O(m+n) with O(m) memory (HashSet) → O(m+n) with O(1) memory (length difference, or the pointer-switch trick, which is the shortest code).
Follow-up questions interviewers ask
Try answering each one before you open it.
Related (same pattern): detect a cycle and find where it starts.
Floyd's tortoise and hare: when slow and fast meet, reset one pointer to the head and move both one step at a time; they meet at the cycle start. O(n), O(1) (see Reverse a linked list and detect a cycle).
Related (same pattern): copy a list with random pointers.
HashMap of original → copy (O(n) space), or interleave copies (A → A' → B → B'), set A'.random = A.random.next, then unweave: O(n) time, O(1) extra space.