160. Intersection of Two Linked Lists
EasySolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to find the intersection node of two singly linked lists. The intersection is defined by reference, not value. This means if the lists intersect, they share a common sub-list from the intersection point to the end. The solution must be efficient, running in O(m + n) time and using only O(1) extra memory, as hinted by the follow-up question.
Solution
Intersection of Two Linked Lists in Python
# 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') -> 'Optional[ListNode]':
# Initialize two pointers, one for each list head.
p1, p2 = headA, headB
# Loop until the two pointers meet.
while p1 != p2:
# If p1 reaches the end of list A, redirect it to the head of list B.
# Otherwise, move it to the next node.
p1 = p1.next if p1 else headB
# If p2 reaches the end of list B, redirect it to the head of list A.
# Otherwise, move it to the next node.
p2 = p2.next if p2 else headA
# The pointers have met. This meeting point is either the intersection node or null.
# If there's an intersection, they meet at the intersection node.
# If there's no intersection, they both traverse both lists and meet at null.
return p1
Intersection of Two Linked Lists in Java
/**
* 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) {
// Initialize two pointers, one for each list head.
ListNode p1 = headA;
ListNode p2 = headB;
// Loop until the two pointers meet.
while (p1 != p2) {
// If p1 reaches the end of list A, redirect it to the head of list B.
// Otherwise, move it to the next node.
p1 = (p1 == null) ? headB : p1.next;
// If p2 reaches the end of list B, redirect it to the head of list A.
// Otherwise, move it to the next node.
p2 = (p2 == null) ? headA : p2.next;
}
// The pointers have met. This meeting point is either the intersection node or null.
// If there's an intersection, they meet at the intersection node.
// If there's no intersection, they both traverse both lists and meet at null.
return p1;
}
}
Intersection of Two Linked Lists in C++
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
// Initialize two pointers, one for each list head.
ListNode *p1 = headA;
ListNode *p2 = headB;
// Loop until the two pointers meet.
while (p1 != p2) {
// If p1 reaches the end of list A, redirect it to the head of list B.
// Otherwise, move it to the next node.
p1 = p1 ? p1->next : headB;
// If p2 reaches the end of list B, redirect it to the head of list A.
// Otherwise, move it to the next node.
p2 = p2 ? p2->next : headA;
}
// The pointers have met. This meeting point is either the intersection node or null.
// If there's an intersection, they meet at the intersection node.
// If there's no intersection, they both traverse both lists and meet at null.
return p1;
}
};
Intersection of Two Linked Lists in TypeScript
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
function getIntersectionNode(headA: ListNode | null, headB: ListNode | null): ListNode | null {
// Initialize two pointers, one for each list head.
let p1: ListNode | null = headA;
let p2: ListNode | null = headB;
// Loop until the two pointers meet.
while (p1 !== p2) {
// If p1 reaches the end of list A, redirect it to the head of list B.
// Otherwise, move it to the next node.
p1 = p1 ? p1.next : headB;
// If p2 reaches the end of list B, redirect it to the head of list A.
// Otherwise, move it to the next node.
p2 = p2 ? p2.next : headA;
}
// The pointers have met. This meeting point is either the intersection node or null.
// If there's an intersection, they meet at the intersection node.
// If there's no intersection, they both traverse both lists and meet at null.
return p1;
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.