DSA Tracker

Easy

Intersection of Two Linked Lists

An easy Linked List problem included in Love Babbar 450, Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.

Topic
Linked List
Sheets
2
Core for
3 roles
Platform
LeetCode

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=[4,1,8,4,5], listB=[5,6,1,8,4,5]
Output
node with value 8

Example 2

Input
listA=[1,9,1,2,4], listB=[3,2,4]
Output
node with value 2

Example 3

Input
listA=[2,6,4], listB=[1,5]
Output
null

Constraints

  • 0 <= list1.length, list2.length <= 3*10^4

How to think about it

Updated 2026-09-09

The 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

  1. 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.

  2. 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.

  3. 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.

What each operation costs

OperationTime
insert or delete at the headO(1)
insert or delete after a known nodeO(1)
find an element by value or positionO(n)
What usually goes wrong with Linked List
  • Losing access to the remainder of the chain by overwriting a next reference before caching the downstream node address in a temporary variable.
  • Attempting to read properties of a null node reference after walking one step beyond the tail or advancing a fast runner without checking its next step.
  • Creating an accidental infinite cycle by pointing a trailing node back into earlier segments of the chain without severing old outgoing links.

Which roles need this problem

Linked List is a core topic for these 3 roles — if you're targeting one of them, this problem is early in your path, not optional.

Secondary for 5 more roles, including Full-Stack Developer, Android Developer, iOS Developer.

Track this in your role's order

Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.

Start free

More Linked List problems

Problem set and role mapping as of .