Pattern this teaches:Fast and Slow Pointers
Intersection of Two Linked Lists
An easy Linked List interview guide. Task statement, worked examples, intuition, and step-by-step solutions.
- Topic
- Linked List
- Sheets
- 2
- Core for
- 4 roles
The problem
Given the heads of two singly linked lists, return the node where they intersect. If they do not intersect, return null.
Example 1
- Input
- listA=[3,7,2,9,5], listB=[6,2,9,5]
- Output
- node with value 2
Example 2
- Input
- listA=[4,8,6,1], listB=[5,9,3,6,1]
- Output
- node with value 6
Example 3
- Input
- listA=[3,8,5], listB=[2,7]
- Output
- null
Constraints
- 0 <= list1.length, list2.length <= 3*10^4
How to think about it
Updated 2026-09-29The two lists may have different prefix lengths before reaching the shared suffix. If two walkers switch to the other list's head upon hitting the end, each traverses length A + length B. Because both paths have identical total lengths, the runners align perfectly on their second lap.
Approaches, worst first
Visited node hash set
time O(m + n) · space O(m)
Traverse list A completely and record every node memory reference in a hash set. Then traverse list B, returning the first node encountered that already resides in the set. Correct but consumes linear extra space.
Length alignment offset
time O(m + n) · space O(1)
Compute lengths of both lists. Advance the pointer for the longer list by the difference in lengths so both runners are equidistant from the tail. Step both pointers forward together until they meet or hit null.
Two-pointer redirectWrite this one
time O(m + n) · space O(1)
Initialize pA at headA and pB at headB. Advance both by one step; when either reaches null, redirect it to the head of the opposite list. They will either collide at the intersection node or both hit null simultaneously after at most two passes.
Where people lose marks · 3
- Comparing node values rather than node references causes false intersection hits on distinct nodes that happen to store identical integer values.
- Switching pointers to the opposing head inside the loop without ever reaching null creates an infinite cycle when the lists do not intersect.
- Either head starting as null must immediately yield null without attempting pointer traversal.
The theory behind it
Linked List — the ground this problem stands on. All Linked List problems
What Linked List is
A linked list is a chain of separate cargo cars connected by coupling hooks, scattered anywhere across memory rather than sitting in a tidy contiguous row. Each car, called a node, holds a single piece of data and a pointer directing traffic to the address of the next car in line. Because nodes connect only by directional links, jumping straight to the tenth car is impossible without walking past the first nine.
When to reach for it
Choose a linked list when a problem requires frequent insertions and deletions at known positions without shifting whole blocks of surrounding memory. Problems mentioning pointer splicing, reversing subsequences in place, merging sorted streams, or detecting cycles in linear chains strongly point here. It is ideal when total capacity is unpredictable and memory allocation must happen one individual node at a time.
How the pattern works
Think in terms of pointer rewiring before dereferencing. Keep a dummy head node pointing to the start of the list so modifications to the initial item do not require separate edge logic. Always save references to neighboring nodes into temporary variables before cutting or redirecting forward links. When diagnosing loops or locating middle nodes, advance two references simultaneously at differing velocities so traversal completes without supplementary storage.
More Linked List problems
Target Roles
Core requirement for 4 roles:
Track in your role's order
Pick your target role and all 370 problems resequence to what that interview actually asks.
Start freeProblem set and role mapping as of .