Find Where Two Linked Lists Merge
Updated
Problem statement
Two singly linked lists are stored in one node table. The nodes are numbered 0 to n - 1. next[i] is the number of the node that comes after node i, or -1 if node i is the last node of its list. headA and headB are the first nodes of list A and list B.
The two lists may merge: from some node on, they use the same nodes all the way to the end. Return the number of the first node that is on both lists. If the lists share no node, return -1.
Two nodes are the same only if they have the same number. Table entries for nodes that neither list reaches do not change the answer.
Function
findMergeNode(next: int[], headA: int, headB: int) → intExamples
Example 1
next = [1,2,3,-1,5,2]headA = 0headB = 4return = 2List A is 0 -> 1 -> 2 -> 3 and list B is 4 -> 5 -> 2 -> 3. Both lists reach node 2 and share every node after it, so the answer is 2.
Example 2
next = [1,-1,3,-1]headA = 0headB = 2return = -1List A is 0 -> 1 and list B is 2 -> 3. They have no node in common, so the answer is -1.
Example 3
next = [1,2,-1]headA = 0headB = 1return = 1List A is 0 -> 1 -> 2 and list B is 1 -> 2. List B starts on a node of list A, so the first shared node is B's own head, 1.
Constraints
1 <= n <= 2000, wherenis the length ofnext.-1 <= next[i] <= 19990 <= headA, headB <= 1999- Both heads, and every
next[i]that is not-1, are less thann. - Following
nextfromheadAor fromheadBreaches-1without visiting any node twice.
More Expedia Onsite Interview questions
- Scramble StringONSITE INTERVIEW · Seen Sep 2026
- Smallest Sufficient TeamONSITE INTERVIEW · Seen Sep 2026
- Check Anagram Pairs Index by IndexONSITE INTERVIEW · Seen Oct 2025
- Fresh Oranges Left After Rotting StopsONSITE INTERVIEW · Seen Oct 2025
- Longest Increasing SubsequenceONSITE INTERVIEW · Seen Oct 2025
- Lowest Common Ancestor with Repeated and Missing ValuesONSITE INTERVIEW · Seen Oct 2025
- Shortest Substring with Exactly K OnesONSITE INTERVIEW · Seen Oct 2025
- Middle Element of a Linked ListONSITE INTERVIEW · Seen Nov 2024
- Minimum in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Sep 2024
- Mirror Image Binary TreesONSITE INTERVIEW · Seen Sep 2024
Similar questions at other companies
- Detect a Linked-List CyclePaytm · ONSITE INTERVIEWPaytm interview questions
- Detect a Linked-List CycleCapgemini · ONSITE INTERVIEWCapgemini interview questions
- Detect a Linked-List CycleMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Detect and Remove a Linked List LoopBank of America · ONSITE INTERVIEWBank of America interview questions
- First Node Two Linked Lists ShareAdobe · PHONE SCREENAdobe interview questions
- Where Two Linked Lists MeetNutanix · PHONE SCREENNutanix interview questions
- Detect a Loop in a Linked ListEPAM · ONSITE INTERVIEWEPAM interview questions
- Merge Two Sorted Linked ListsZS · ONSITE INTERVIEWZS interview questions
- Middle Node Of A Linked ListNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
- Build and Check a Palindrome Linked ListOmnissa · ONSITE INTERVIEWOmnissa interview questions
Expedia interview experiences
- Expedia Intern Final Round in 2021: 28 Experiences (Part 4 of 6)
- Expedia Intern Finals Before 2020: 14 Experiences (Part 6 of 6)
- Expedia Intern Final Round in 2020: 24 Experiences (Part 5 of 6)
- Expedia Intern Final Round in 2022: 15 Experiences (Part 3 of 6)
- Expedia Intern Final Round in 2023: 17 Experiences (Part 2 of 6)
- Expedia SWE Intern Final Round: 126 Experiences (Part 1 of 6)