
Intersection of Two Linked Lists β The Smart Trick Most People Miss (LeetCode 160)
π Try the ProblemPractice here:https://leetcode.com/problems/intersection-of-two-linked-lists/π€ Letβs Start With a ThoughtImagine two roads:Road A: 4 β 1 β 8 β 4 β 5 Road B: 5 β 6 β 1 β 8 β 4 β 5At some pointβ¦ both roads merge into one.π That merging point is the intersection nodeπ§ Problem in Simple WordsYou are given:Head of List AHead 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 InsightList A: 1 β 9 β 1 β 2 β 4 List B: 3 β 2 β 4Even though both lists have 2 and 4:π They intersect ONLY if they point to the same node in memoryπ¦ Constraints1 <= m, n <= 3 * 10^41 <= Node.val <= 10^5No cycles in the listπ First Thought (Brute Force)IdeaFor every node in List A:π Traverse entire List B and check if nodes matchComplexityTime: O(m * n) βSpace: O(1)Too slow.π§© Better Approach: Length Difference MethodIdeaFind length of both listsMove the longer list ahead by the differenceNow traverse both togetherWhy This Worksπ After skipping extra nodes, both pointers are equally far from intersectionComplexityTime: O(m + n)Space: O(1)π Optimal Approach: Two Pointer Switching TrickNow comes the most beautiful trick π₯π§ Core IdeaWe use two pointers:pointerA β starts from headApointerB β starts from headBπ― The Magic Trickπ When a pointer reaches end β switch it to the other listπ VisualizationLetβs say:Length A = mLength B = nPointer paths:pointerA travels: A β BpointerB travels: B β ASo 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 ExampleA: 4 β 1 β 8 β 4 β 5 B: 5 β 6 β 1 β 8 β 4 β 5Iteration: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; }}β±οΈ ComplexityTime ComplexityO(m + n)Space ComplexityO(1)βοΈ Approach ComparisonApproachTimeSpaceNotesBrute ForceO(m*n)O(1)Too slowLength DifferenceO(m+n)O(1)GoodTwo Pointer SwitchO(m+n)O(1)Best & elegantβ Common MistakesComparing values instead of nodes βForgetting to switch listsNot handling null correctlyAssuming intersection always existsπ₯ Interview InsightThis is one of the most clever pointer problems.It teaches:π How to equalize paths without extra memoryπ How pointer switching solves alignment problemsπ§ Final ThoughtAt first, this trick feels like magicβ¦But actually itβs just:Equalizing path lengthsπ ConclusionThe Intersection of Two Linked Lists problem is a perfect example of:Smart thinking > brute forceClean logic > complex codeπ Tip: Whenever two lists need alignment, think:"Can I make both pointers travel equal distance?"Thatβs where the magic begins β¨

