Table of Contents
966 words
5 minutes
Detecting Intersection of Two Singly Linked Lists and Finding the Intersection Node: Two-Pointer Alternating Traversal

Problem Description#

Given the head nodes headA and headB of two singly linked lists, determine whether they intersect (i.e., some node is shared by both linked lists). If they intersect, return the intersection node; otherwise, return null.

Input: headA = 4→1→8→4→5
headB = 5→6→1→8→4→5
↑
intersection
Output: node 8

The two linked lists meet at node 8, after which they share the 4→5 portion.

Solution Approach#

Problems with the brute-force approach#

  • Hash table approach: Traverse linked list A and store all nodes in a hash table; then traverse linked list B and check whether each node is in the hash table. Space O(n).
  • Two-pointer alternating traversal approach: Space O(1), completed in a single traversal.

Two-pointer alternating traversal approach#

Key observation: If the two linked lists have an intersection, then the nodes after the intersection are exactly the same. The two lists may have different lengths, but the total path length from each respective head node to the intersection and then to the end is fixed.

Core idea#

Let two pointers start from A and B respectively:

  • Pointer pA: starts from the head of A, and after reaching the end, jumps to the head of B and continues
  • Pointer pB: starts from the head of B, and after reaching the end, jumps to the head of A and continues

If an intersection exists, the total distance traveled by both pointers is equal (both are lenA + lenB), and they will meet at the intersection. If no intersection exists, both pointers will eventually reach null at the same time.

Mathematical proof#

Let the length of linked list A be lenA = a + c (a is the part unique to A, c is the shared part), and the length of linked list B be lenB = b + c.

PointerFirst segmentSecond segmentTotal distanceReaches intersection
pAac + ba + c + bYes
pBbc + ab + c + aYes

The total distance traveled by both pointers is a + b + c, so they will reach the intersection at the same time (or reach null at the same time).

Algorithm steps#

  1. pA = headA, pB = headB
  2. Loop condition: while (pA != pB)
  3. pA moves one step; if pA == null, jump to headB
  4. pB moves one step; if pB == null, jump to headA
  5. When the loop ends, return pA (the intersection or null)

Code Implementation#

C
#include <stdio.h>
#include <stdlib.h>
typedef struct ListNode {
int val;
struct ListNode *next;
} ListNode;
ListNode* getIntersectionNode(ListNode *headA, ListNode *headB) {
if (!headA || !headB) return NULL;
ListNode *pA = headA;
ListNode *pB = headB;
// Two pointers traverse alternately
while (pA != pB) {
pA = pA ? pA->next : headB; // pA finishes A then goes to B
pB = pB ? pB->next : headA; // pB finishes B then goes to A
}
// Meeting point: intersection or null
return pA;
}
C++
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(nullptr) {}
};
class Solution {
public:
ListNode* getIntersectionNode(ListNode *headA, ListNode *headB) {
if (!headA || !headB) return nullptr;
ListNode *pA = headA;
ListNode *pB = headB;
// Two pointers traverse alternately
while (pA != pB) {
pA = pA ? pA->next : headB; // pA finishes A then goes to B
pB = pB ? pB->next : headA; // pB finishes B then goes to A
}
// Meeting point: intersection or nullptr
return pA;
}
};
JavaScript
class ListNode {
constructor(val) {
this.val = val;
this.next = null;
}
}
function getIntersectionNode(headA, headB) {
if (!headA || !headB) return null;
let pA = headA;
let pB = headB;
// Two pointers traverse alternately
while (pA !== pB) {
pA = pA ? pA.next : headB; // pA finishes A then goes to B
pB = pB ? pB.next : headA; // pB finishes B then goes to A
}
// Meeting point: intersection or null
return pA;
}

Illustrated Process#

Case with an intersection#

Linked list A: a1 → a2
↘
c1 → c2 → c3
↗
Linked list B: b1 → b2 → b3
pA path: a1 → a2 → c1 → c2 → c3 → b1 → b2 → b3 → ★c1
pB path: b1 → b2 → b3 → c1 → c2 → c3 → a1 → a2 → ★c1
At ★ the two pointers meet, so c1 is the intersection point ✓

Case without an intersection#

Linked list A: 1 → 2 → 3
Linked list B: 4 → 5
pA path: 1 → 2 → 3 → 4 → 5 → null
pB path: 4 → 5 → 1 → 2 → 3 → null
★ Both pointers reach null at the same time ✓

Complexity Analysis#

DimensionAnalysis
Time complexityO(m + n) — each pointer walks m + n steps
Space complexityO(1) — only two pointers are used

Key Points#

  1. “Walk the path you walked”: pA finishes its own list and then walks B, while pB finishes its own list and then walks A—this design lets the two pointers eliminate the length difference and reach the intersection at the same time
  2. Reach null at the same time: If there is no intersection, the two pointers will also reach null at the same time after traversing the same total length, and the loop ends naturally
  3. Cannot skip null midway: Some implementations say “jump when reaching null,” which is fine, because acyclic linked lists all end in null
  4. Difference from cycle detection: This problem assumes the linked lists are acyclic. If cycles are possible, cycle detection must be done first

Extended Thinking#

  • If the linked lists may contain cycles, how do we determine intersection? — This requires case-by-case discussion: both acyclic, one cyclic and one acyclic, both cyclic
  • Is there a more intuitive solution? — You can first compute the length difference between the two linked lists, let the pointer of the longer list walk the “difference” steps first, and then walk in sync

Related articles:

Detecting Intersection of Two Singly Linked Lists and Finding the Intersection Node: Two-Pointer Alternating Traversal
https://www.hehonglei.com/technology/linked-list-intersection/
Author
HHLBlog
Published at
2026-08-10
License
CC BY-NC-SA 4.0