Intersection of Two Linked Lists

Given the heads of two singly linked-lists that may intersect, return the node at which the intersection begins. If the two lists have no intersection, return null. Example: Input: A = [4,1,8,4,5], B = [5,6,1,8,4,5] Output: 8 Explanation: The intersection starts at node with value 8. Pattern focus: Fast and Slow Pointers (two-pointer technique with switching).

Input Format

headA = ListNode, headB = ListNode

Output Format

return ListNode

Constraints

  • The number of nodes of list A is in [1, 10^4].
  • The number of nodes of list B is in [1, 10^4].
  • Values are in [-10^5, 10^5].

Examples

Example 1:

Input:

headA = [4,1,8,4,5]
headB = [5,6,1,8,4,5]

Output:

null

Explanation:

Intersection at node with value 8.

Example 2:

Input:

headA = [0,9,1,2,4]
headB = [3,2,4]

Output:

null

Explanation:

Intersection at node with value 2.

Loading...
Intersection of Two Linked Lists - Linked List