# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
if not headA or not headB:
return None
a, b = headA, headB
while a != b:
a = a.next if a else headB
b = b.next if b else headA
return a
// Definition for singly-linked list.
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* a = headA;
ListNode* b = headB;
while (a != b) {
a = a ? a->next : headB;
b = b ? b->next : headA;
}
return a;
}
};
// Definition for singly-linked list.
public class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if (headA == null || headB == null) return null;
ListNode a = headA;
ListNode b = headB;
while (a != b) {
a = (a != null) ? a.next : headB;
b = (b != null) ? b.next : headA;
}
return a;
}
}
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} headA
* @param {ListNode} headB
* @return {ListNode}
*/
var getIntersectionNode = function(headA, headB) {
if (!headA || !headB) return null;
let a = headA, b = headB;
while (a !== b) {
a = a ? a.next : headB;
b = b ? b.next : headA;
}
return a;
};
Given two singly linked lists, headA
and headB
, return the node at which the two lists intersect. If the two linked lists have no intersection, return null
.
null
if there is no intersection.At first glance, the problem seems to require checking every node in one list against every node in the other. However, this brute-force approach is inefficient. We are looking for a node that is shared (by reference) between both lists. Since the lists may be of different lengths, a naive approach would not be optimal.
We want to find an efficient way to "align" the lists so that we can compare nodes in a single pass. A key observation is that after the intersection point, the two lists share all subsequent nodes. If we can traverse the same number of nodes from the end for both lists, we will eventually reach the intersection point if it exists.
The challenge is to handle lists of different lengths without using extra space. We can use two pointers and make them traverse both lists in such a way that they will either meet at the intersection node or both reach the end (null
) at the same time if there is no intersection.
The most efficient solution uses two pointers to traverse the two lists. Here's how it works:
a
and b
, at the heads of headA
and headB
respectively.null
at the same time.This approach works because:
null
) simultaneously.Suppose we have the following lists:
List A: 4 → 1 \ 8 → 4 → 5 / List B: 5 → 6 → 1
Walkthrough:
a
starts at 4, pointer b
at 5.The optimized approach is much more efficient, especially for long lists.
To find the intersection node of two singly linked lists, we use two pointers traversing the lists, switching heads when reaching the end. This ensures both pointers cover equal distances and meet at the intersection node if it exists, or at null
otherwise. This approach is elegant, efficient (O(m + n) time, O(1) space), and avoids the need for extra data structures or modifying the input lists.