 All Problems
Intersection of Two Linked Lists
easy
linked list
hash table
two pointers
amazon
facebook
microsoft
google

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.

Example 1:

Input:
4 1 8 4 5
5 6 1 8 4 5
3
Output: 8

(Intersection at node with value 8; listA[3] and listB[3] are the same node)

Example 2:

Input:
2 6 4
1 5
-1
Output: null

Constraints:

  • 1 ≤ m, n ≤ 3 × 10⁴
  • intersectVal is 0 if there is no intersection

Input format: First line: listA values. Second line: listB values. Third line: skipA (index in A where intersection begins), or -1 for no intersection.

Output format: The intersection node's value, or null.

Run to check your code against the sample cases, or submit to run every case