2Brute2Best
🔥0

Intersection of Two Linked Lists

Easy·LeetCode #160 Pro OnlyBasic Reinforcement
LeetCode
01

Problem Statement

You are given the heads of two singly linked lists, headA and headB. At some node the two lists may join: from that node on, they share the exact same nodes (the same objects in memory) until the end.

Return the first shared node. If the lists never join, return null.

The lists have no cycles, and you must not change their structure. Follow-up: can you do it in O(m + n) time and O(1) extra memory?

Input

Two list heads headA (m nodes) and headB (n nodes). The judge builds them from listA, listB, skipA, skipB and intersectVal, but your function only gets the two heads.

Output

The node where the two lists join, or null if they don't.

Example 1
Input: listA = [4, 1, 8, 4, 5], listB = [5, 6, 1, 8, 4, 5], skipA = 2, skipB = 3
Output: The node with value 8

A's own part is [4, 1], B's own part is [5, 6, 1], and both then continue with the same nodes 8 → 4 → 5. Note that the 1 in A and the 1 in B have the same value but are different nodes.

Example 2
Input: listA = [1, 9, 1, 2, 4], listB = [3, 2, 4], skipA = 3, skipB = 1
Output: The node with value 2

After [1, 9, 1] in A and [3] in B, both lists continue with the same nodes 2 → 4.

Example 3
Input: listA = [2, 6, 4], listB = [1, 5], skipA = 3, skipB = 2
Output: null

The lists never share a node.

Constraints
  • • 1 ≤ m, n ≤ 3 · 10^4
  • • 1 ≤ Node.val ≤ 10^5
  • • 0 ≤ skipA ≤ m, 0 ≤ skipB ≤ n
  • • The lists contain no cycles.
  • • The list structure must be the same after the function returns.
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“two lists that may join”
→ After the join they share a whole tail. Only the head parts differ in length.
“first shared node”
→ Line both pointers up so they are the same distance from the join.
“O(1) memory”
→ No hash set. Fix the length difference with a head start or by switching lists.
“same node, not same value”
→ Compare addresses: `a == b`.
03

Approach Complexity Ladder

Brute Force — check every pair of nodesO(m · n)O(1)

For each node of A, scan all of B for the same node

Better — hash set of A's nodesO(m + n)O(m)

Store every node address of A, then find the first node of B in the set

Optimal — two pointers that switch listsO(m + n)O(1)

a walks A then B, b walks B then A; equal totals make them meet at the join

Brute Force Intuition

Take each node of A in order and scan the whole of B looking for that same node (same address). The first node of A found in B is the join, because everything before the join belongs only to A.

Recognition rule for this sub-pattern:

Give one pointer a head start (n steps, or the other list's length) so both reach the target together.
Remember this · Linked List — Fast & Slow
Trigger
Cycle detection, middle of list, "n-th from end", two lists meeting, repeating sequences.
Think
If one pointer moves faster or starts earlier, where will they meet?
Key question
What speed or gap makes the meeting point tell me the answer?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Linked List — Fast & Slow translate into production architectures and low-level memory mechanics.