Intersection of Two Lists

Given two (non-cyclical) linked lists, determine if the two lists intersect and return the intersecting node. Otherwise, return null. Example: Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5] Output: Reference to node with value 8 Explanation: Lists intersect at the node with value 8. Pattern focus: Fast and Slow Pointers (two-pointer technique with switching heads).

Input Format

headA = ListNode, headB = ListNode

Output Format

return ListNode

Constraints

  • The number of nodes is in the range [0, 10^4].
  • -10^5 <= Node.val <= 10^5

Examples

Example 1:

Input:

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

Output:

null

Explanation:

Intersection starts at node with value 8.

Loading...
Intersection of Two Lists - Linked List