Community Problem

Intersection of Two Linked Lists

Difficulty: Easy

Find the node where two singly-linked lists merge using the two-pointer rendezvous trick that runs in O(m + n) time and O(1) space.

Intersection of Two Linked Lists

Find the node where two singly-linked lists merge using the two-pointer rendezvous trick that runs in O(m + n) time and O(1) space.

EASY
Free
linked-list
two-pointers
hash-table
folakemansour

By @folakemansour

March 1, 2026

·

Updated May 18, 2026

709 views

4

Rate

I rotate this in for fast/slow + linked-list onsite practice because the two-pointer rendezvous trick is the kind of idea that's hard to derive from scratch in 30 minutes. The hash-set version (store all nodes from list A, scan B looking for the first hit) is the easy answer; the senior signal is the constant-space pointer-swap pattern.

Intersection of Two Linked Lists

Given the heads headA and headB of two singly-linked lists that may merge at some node, return the node at which the two lists first intersect. If the lists do not intersect, return null. Intersection is by reference, not by value: two nodes are the same intersection only if they are the same node object in memory.

After the intersection node, the two lists share the same tail (because each node has only one next reference). Before the intersection node, the two lists may have different lengths.

Examples

Example 1:

  • Input: list A = [4, 1, 8, 4, 5], list B = [5, 6, 1, 8, 4, 5], with the merge happening at the node with value 8.
  • Output: node with value 8
  • Explanation: After 4, 1 in A and 5, 6, 1 in B, both walk into the same 8 -> 4 -> 5 tail.

Example 2:

  • Input: list A = [1, 9, 1, 2, 4], list B = [3, 2, 4], merge at node value 2.
  • Output: node with value 2
  • Explanation: A's tail 2 -> 4 is the same as B's tail 2 -> 4.

Example 3:

  • Input: list A = [2, 6, 4], list B = [1, 5], no intersection.
  • Output: null
  • Explanation: Disjoint lists.

Example 4:

  • Input: list A = [1], list B = [1], no intersection (separate single nodes).
  • Output: null
  • Explanation: Two distinct single-node lists, even with equal values.

Constraints

  • The number of nodes in list A is in the range [1, 3 * 10^4].
  • The number of nodes in list B is in the range [1, 3 * 10^4].
  • 1 <= Node.val <= 10^5
  • The lists may or may not intersect, but if they do they share the same tail.

Follow-up

Can you do it in O(m + n) time and O(1) extra space? The pointer-swap trick is what gets you there.

Solution

Hints

0/4
Hint 1
Hint 2
Hint 3
Hint 4
All Problems