Editorial
Core insight
Intersection of Two Linked Lists · Linked List Manipulation Patterns
Core Insight for Intersection of Two Linked Lists
The fundamental challenge in LeetCode 160 is that the two lists may have different lengths before the intersection point. A pointer traversing the shorter list will reach the intersection sooner than a pointer traversing the longer list.
To solve this, we must synchronize the pointers so they arrive at the intersection simultaneously. The core insight is to virtually "concatenate" the lists.
If a pointer traverses List A and then immediately switches to the head of List B, the total distance it covers is Length(A) + Length(B). Conversely, a pointer traversing List B then List A covers Length(B) + Length(A). Since addition is commutative, both pointers traverse exactly the same total distance.
Visual Description:
Imagine two pointers, pA and pB.
pAmoves through List A. When it hits the end (null), it jumps to the head of List B.pBmoves through List B. When it hits the end (null), it jumps to the head of List A.- By effectively swapping starting lanes after the first pass, the pointer that started on the longer list has now been delayed exactly enough to align with the pointer that started on the shorter list.
- They will meet at the intersection node. If there is no intersection, they will both reach
nullat the exact same step after the swap.

Deep dive
Full breakdown
Full reasoning, pitfalls, and implementation flow
LeetCode 160: Intersection of Two Linked Lists Solution & Explanation
Problem Overview
TL;DR: The optimal solution uses two pointers traversing the concatenated length of both lists to neutralize length differences and locate the intersection node in time with space.
The Intersection of Two Linked Lists problem asks us to identify the specific node where two singly linked lists merge. If the lists intersect, they form a Y-shape, sharing all nodes after the intersection point. If they do not intersect, we must return null. Importantly, the solution must retain the original structure of the lists, and the intersection is defined by reference (memory address), not by node value. This is a classic LeetCode 160 interview question that tests pointer manipulation skills.
Brute Force Approach for Intersection of Two Linked Lists
The naive approach involves checking every node in the first list against every node in the second list to see if they point to the same memory location.
- Iterate through
headAwith a pointerptrA. - For every node visited by
ptrA, iterate through the entire listheadBusingptrB. - Check if
ptrA == ptrB(reference equality). - If a match is found, return that node.
- If loops complete without a match, return
null.
Pseudo-code:
for nodeA in listA:
for nodeB in listB:
if nodeA is nodeB:
return nodeA
return nullComplexity Analysis: The time complexity is , where and are the lengths of the two lists. For large inputs (up to nodes each), this results in approximately operations, which is highly inefficient and may lead to a Time Limit Exceeded (TLE) error or simply fail the interview constraint of linear time complexity.
Algorithm Strategy: Linked List Manipulation Patterns
Using the Linked List - Intersection Detection strategy, we maintain two pointers initialized at the heads of the respective lists.
- Initialization: Create two pointers,
ptrAandptrB, pointing toheadAandheadB. - Traversal Loop: Enter a loop that continues as long as
ptrAis not equal toptrB. - Pointer Movement & Switching:
- Advance
ptrAtoptrA.next. IfptrAbecomesnull, redirect it toheadB. - Advance
ptrBtoptrB.next. IfptrBbecomesnull, redirect it toheadA.
- Advance
- Termination: The loop terminates when
ptrA == ptrB.- Case 1 (Intersection): Both pointers point to the intersecting node.
- Case 2 (No Intersection): Both pointers become
nullsimultaneously (after one full swap cycle).
- Return: Return either
ptrAorptrB(since they are equal).
Execution Flow
- Start:
ptrApoints to the first node of List A;ptrBpoints to the first node of List B. - Compare: Check if
ptrAandptrBrefer to the same object. If yes, returnptrA. - Advance:
- Move
ptrAforward. IfptrAwas at the last node of List A, move it to the first node of List B. - Move
ptrBforward. IfptrBwas at the last node of List B, move it to the first node of List A.
- Move
- Repeat: Continue this process.
- If the lists intersect, the pointers will eventually align at the intersection node because the "head start" of the shorter list is canceled out by the length of the longer list during the second pass.
- If the lists do not intersect, both pointers will traverse
len(A) + len(B)nodes and reachnullat the same time. The conditionptrA == ptrB(where both arenull) satisfies the loop termination.
- Output: Return the node found (or
null).
Proof of Correctness
Let be the length of List A, be the length of List B, and be the length of the shared suffix (intersection).
- The non-shared part of A is .
- The non-shared part of B is .
When ptrA traverses A then B, it travels nodes (unique A), then nodes (shared), then switches to B and travels nodes (unique B) before reaching the intersection again.
Total distance to intersection: .
When ptrB traverses B then A, it travels nodes (unique B), then nodes (shared), then switches to A and travels nodes (unique A) before reaching the intersection again.
Total distance to intersection: .
Since the distance to the intersection is identical for both pointers, and they move at the same speed (1 node/step), they are guaranteed to meet at the intersection node. If no intersection exists (), they both travel and meet at null.
Pattern Reuse Notes
The Linked List - Intersection Detection pattern and the two-pointer synchronization technique are applicable to other problems:
- LeetCode 599: Minimum Index Sum of Two Lists - While this problem typically involves arrays/strings and hashmaps, the conceptual subpattern of finding the "meeting point" or commonality between two linear structures is similar.
- Cycle Detection (LeetCode 141): The logic of pointers traversing at different relative speeds or distances to detect a structural property (intersection vs. cycle) shares the same underlying intuition of invariant-based traversal.
Feynman coach · Before you peek at the code
Can you explain the core insight in your own words?
If you can explain it before reading code, you’re far more likely to recall it under interview pressure.
Reference Implementation
C++ Solution for LeetCode 160
1/**
2 * Definition for singly-linked list.
3 * struct ListNode {
4 * int val;
5 * ListNode *next;
6 * ListNode(int x) : val(x), next(NULL) {}
7 * };
8 */
9class Solution {
10public:
11 ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
12 // If either head is null, there is no intersection
13 if (!headA || !headB) return nullptr;
14
15 ListNode *ptrA = headA;
16 ListNode *ptrB = headB;
17
18 // Traverse until the pointers meet (either at the intersection node or at null)
19 while (ptrA != ptrB) {
20 // If ptrA reaches the end of listA, switch to headB
21 // Otherwise, move to the next node
22 ptrA = (ptrA == nullptr) ? headB : ptrA->next;
23
24 // If ptrB reaches the end of listB, switch to headA
25 // Otherwise, move to the next node
26 ptrB = (ptrB == nullptr) ? headA : ptrB->next;
27 }
28
29 // Return the intersection node or null
30 return ptrA;
31 }
32};Java Solution for LeetCode 160
1/**
2 * Definition for singly-linked list.
3 * public class ListNode {
4 * int val;
5 * ListNode next;
6 * ListNode(int x) {
7 * val = x;
8 * next = null;
9 * }
10 * }
11 */
12public class Solution {
13 public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
14 // Boundary check
15 if (headA == null || headB == null) return null;
16
17 ListNode ptrA = headA;
18 ListNode ptrB = headB;
19
20 // Continue until they collide or both hit null
21 while (ptrA != ptrB) {
22 // Switch tracks at the end of the list
23 ptrA = (ptrA == null) ? headB : ptrA.next;
24 ptrB = (ptrB == null) ? headA : ptrB.next;
25 }
26
27 return ptrA;
28 }
29}Python Solution for LeetCode 160
1# Definition for singly-linked list.
2# class ListNode:
3# def __init__(self, x):
4# self.val = x
5# self.next = None
6
7class Solution:
8 def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]:
9 if not headA or not headB:
10 return None
11
12 ptrA = headA
13 ptrB = headB
14
15 # Loop until pointers meet or both become None
16 while ptrA != ptrB:
17 # If ptrA reaches end of list A, redirect to head B
18 ptrA = ptrA.next if ptrA else headB
19 # If ptrB reaches end of list B, redirect to head A
20 ptrB = ptrB.next if ptrB else headA
21
22 return ptrA