Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

75. Intersection of Two Linked Lists

EasyDay 22 of 60Linked List

Given the heads of two singly linked lists, headA and headB, return the node where the two lists merge (the first node they share), or null if they never meet. The lists have no cycles and must keep their structure. In the tests, intersectVal is the value of the shared node (0 if none) and skipA / skipB are how many nodes of each list come before it; these are only used to build the lists. The judge shows the value of the node you return.

Example 1
Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
Output: 8
Both lists share the tail [8,4,5]. The two 1s are different nodes.
Example 2
Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
Output: null
Constraints
  • Each list has 1 to 3 * 10^4 nodes
  • 1 <= Node.val <= 10^5
  • There are no cycles
Hints
Frequently asked at: Amazon, Microsoft, Bloomberg, Apple
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min