Find the intersection point of Y LL

Linked-List FAQs (Medium) Medium
  • This problem, commonly known as the "Linked List Intersection" problem, serves as a fundamental concept in various real-world scenarios in the software industry
  • For instance, Git, a widely used version control system, utilizes this concept to identify the "merge base" of two branches - the point where the branches diverged
  • Finding this merge base is necessary for Git to efficiently and accurately merge the changes from two branches
  • To find the merge base, Git internally represents the commits as a linked list, with each node pointing to its parent commit(s), and essentially solves the Linked List Intersection problem

Given the heads of two linked lists A and B, containing positive integers. Find the node at which the two linked lists intersect. If they do intersect, return the node at which the intersection begins, otherwise return null.


The Linked List will not contain any cycles. The linked lists must retain their original structure, given as per the input, after the function returns.


Note: for custom input, the following parameters are required(your program is not provided with these parameters):

  • intersectVal - The value of the node where the intersection occurs. This is -1 if there is no intersected node.
  • skipA - The number of nodes to skip ahead in listA (starting from the head) to get to the intersected node(-1 if no intersection).
  • skipB - The number of nodes to skip ahead in listB (starting from the head) to get to the intersected node(-1 if no intersection).
  • listA - The first linked list.
  • listB - The second linked list.

Examples:

Input: listA: intersectVal = 4, skipA = 3, skipB = 2, head -> 1 -> 2 -> 3 -> 4 -> 5, listB: head -> 7 -> 8 -> 4 -> 5

Output(value at returned node is displayed): 4

Explanation: The two lists have nodes with values 4 and 5 as their tails.

Input: listA: intersectVal = -1, skipA = -1, skipB = -1, head -> 1 -> 2 -> 3, listB: head -> 8 -> 9

Output(value at returned node is displayed): null

Explanation: The two lists do not intersect.

Input: listA: intersectVal = 4, skipA = 0, skipB = 0, head -> 4 -> 5 -> 6, listB: head -> 4 -> 5 -> 6

Constraints

  • m == number of nodes in listA.
  • n == number of nodes in listB.
  • 1 <= m, n <= 5 * 104
  • 1 <= ListNode.val <= 104
  • 0 <= skipA < m
  • 0 <= skipB < n
  • intersectVal, skipA, skipB is -1 if listA and listB do not intersect.
  • intersectVal == listA[skipA] == listB[skipB] if listA and listB intersect.

Hints

  • Pointer A starts at headA, and Pointer B starts at headB. When Pointer A reaches the end of list A, move it to headB. When Pointer B reaches the end of list B, move it to headA.
  • If the lists intersect, the pointers will meet at the intersection. If they don’t, both will reach NULL at the same time.

Company Tags

Pinterest PayPal ARM Bain & Company Western Digital Texas Instruments Bloomberg Walmart Unity Technologies Morgan Stanley OYO Rooms Riot Games Splunk Bungie MongoDB Optum Alibaba Intel HCL Technologies Rakuten Cloudflare Dropbox Airbnb Wayfair Roche Google Microsoft Amazon Meta Apple Netflix Adobe