Skip to content

Practice Intersection of Two Linked Lists

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#160
LeetCode Problem

Intersection of Two Linked Lists

Problem Statement

Return the first node shared by the linked lists headed by headA and headB, or null when they do not intersect. Sharing means node identity, not equal values. The lists are acyclic and their structure must remain unchanged. Judge inputs listA and listB describe values; skipA and skipB count nodes before the shared node, and intersectVal is its value or 0 when absent. Your function receives only the two heads. Follow-up: achieve O(m + n) time with O(1) extra memory.

Source: Intersection of Two Linked Lists on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
Output
Intersected at '8'

# Example 2

Input
intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
Output
Intersected at '2'

# Example 3

Input
intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
Output
No intersection

Constraints

The number of nodes of listA is in the m.
The number of nodes of listB is in the n.
1 <= m, n <= 3 * 10^4
1 <= Node.val <= 10^5
0 <= skipA <= m
0 <= skipB <= n
intersectVal is 0 if listA and listB do not intersect.
intersectVal == listA[skipA] == listB[skipB] if listA and listB intersect.

Related LeetCode Problems

Practice more problems similar to Intersection of Two Linked Lists.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.