Intersection of Two Linked Lists – The Smart Trick Most People Miss (LeetCode 160)
Intersection of Two Linked Lists – The Smart Trick Most People Miss (LeetCode 160)

Intersection of Two Linked Lists – The Smart Trick Most People Miss (LeetCode 160)

Understand Why Two Pointers Meet Exactly at the Intersection Without Extra Space (Simple + Powerful Intuition)

3 views
0
0

🚀 Try the Problem

Practice here:

https://leetcode.com/problems/intersection-of-two-linked-lists/


🤔 Let’s Start With a Thought

Imagine two roads:

Road A: 4 → 1 → 8 → 4 → 5
Road B: 5 → 6 → 1 → 8 → 4 → 5

At some point… both roads merge into one.

👉 That merging point is the intersection node


🧠 Problem in Simple Words

You are given:

  1. Head of List A
  2. Head of List B

👉 Find the node where both linked lists intersect

👉 If no intersection exists → return null


⚠️ Important Clarification

👉 Intersection means:

Same memory node (same reference)

NOT:

Same value ❌


📌 Example Insight

List A: 1 → 9 → 1 → 2 → 4
List B: 3 → 2 → 4

Even though both lists have 2 and 4:

👉 They intersect ONLY if they point to the same node in memory


📦 Constraints

1 <= m, n <= 3 * 10^4
1 <= Node.val <= 10^5
No cycles in the list


🔍 First Thought (Brute Force)

Idea

For every node in List A:

👉 Traverse entire List B and check if nodes match

Complexity

Time: O(m * n) ❌
Space: O(1)

Too slow.


🧩 Better Approach: Length Difference Method

Idea

  1. Find length of both lists
  2. Move the longer list ahead by the difference
  3. Now traverse both together

Why This Works

👉 After skipping extra nodes, both pointers are equally far from intersection

Complexity

Time: O(m + n)
Space: O(1)


🚀 Optimal Approach: Two Pointer Switching Trick

Now comes the most beautiful trick 🔥


🧠 Core Idea

We use two pointers:

pointerA → starts from headA
pointerB → starts from headB


🎯 The Magic Trick

👉 When a pointer reaches end → switch it to the other list


🔄 Visualization

Let’s say:

Length A = m
Length B = n

Pointer paths:

pointerA travels: A → B
pointerB travels: B → A

So both travel:

m + n distance


💡 Why They Meet

👉 If intersection exists → they meet at intersection

👉 If not → both reach null at same time


🔥 Step-by-Step Example

A: 4 → 1 → 8 → 4 → 5
B: 5 → 6 → 1 → 8 → 4 → 5

Iteration:

A pointer: 4 → 1 → 8 → 4 → 5 → null → 5 → 6 → 1 → 8
B pointer: 5 → 6 → 1 → 8 → 4 → 5 → null → 4 → 1 → 8

👉 They meet at 8


💻 Clean Java Code (Optimal Solution)

public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {

ListNode tempa = headA;
ListNode tempb = headB;

// Traverse until both pointers meet
while(tempa != tempb){

// Move pointer A
if(tempa != null) tempa = tempa.next;
else tempa = headB;

// Move pointer B
if(tempb != null) tempb = tempb.next;
else tempb = headA;
}

// Either intersection node OR null
return tempa;
}
}


⏱️ Complexity

Time Complexity

O(m + n)

Space Complexity

O(1)


⚖️ Approach Comparison

ApproachTimeSpaceNotes
Brute ForceO(m*n)O(1)Too slow
Length DifferenceO(m+n)O(1)Good
Two Pointer SwitchO(m+n)O(1)Best & elegant


❌ Common Mistakes

  1. Comparing values instead of nodes
  2. Forgetting to switch lists
  3. Not handling null correctly
  4. Assuming intersection always exists


🔥 Interview Insight

This is one of the most clever pointer problems.

It teaches:

👉 How to equalize paths without extra memory

👉 How pointer switching solves alignment problems


🧠 Final Thought

At first, this trick feels like magic…

But actually it’s just:

Equalizing path lengths


🚀 Conclusion

The Intersection of Two Linked Lists problem is a perfect example of:

  1. Smart thinking > brute force
  2. Clean logic > complex code
👉 Tip: Whenever two lists need alignment, think:
"Can I make both pointers travel equal distance?"

That’s where the magic begins ✨

Ai Assistant Kas