Skip to main content
Back to problems
#160
Easy Algorithms

Intersection of two linked lists

Hash Table Linked List Two Pointers
63.2% acceptance
Jan 13, 2026
16512
1491
Given the heads of two singly linked-lists headA and headB, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null.

Solution

C++
Time O(n)
Space O(1)
LeetCode
solution.cpp
class Solution {
public:
  ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
    ListNode *a = headA, *b = headB;
    while (a != b) {
      a = a ? a->next : headB;
      b = b ? b->next : headA;
    }
    return a;
  }
};