Merge Point of Two Linked Lists
Updated
Problem statement
Two singly linked lists are stored together in one array next. The nodes are numbered from 0 to next.length - 1. Node i points to node next[i], and next[i] = -1 means node i is the last node of its list. The first list starts at node headA and the second list starts at node headB.
The two lists may merge: from some node on, both lists run through exactly the same nodes to the end. Return the index of the merge point, which is the first node that is on both lists. If no node is on both lists, return -1.
Nodes are told apart by their index. Every node in the array is on at least one of the two lists.
Function
findMergePoint(next: int[], headA: int, headB: int) → intExamples
Example 1
next = [5,5,-1,1,0,2]headA = 4headB = 3return = 5The first list visits nodes 4, 0, 5, 2 and the second list visits nodes 3, 1, 5, 2. Node 5 is the first node on both lists, and from there both lists go on to node 2.
Example 2
next = [2,3,4,-1,-1]headA = 0headB = 1return = -1The first list is 0, 2, 4 and the second list is 1, 3. No node is on both lists, so the answer is -1.
Example 3
next = [3,-1,0,1]headA = 2headB = 3return = 3The first list is 2, 0, 3, 1. The second list starts at node 3, which is already the third node of the first list, so the whole second list 3, 1 is shared and the merge point is 3.
Constraints
1 <= next.length <= 10^4-1 <= next[i] < next.length0 <= headA, headB < next.length- Following
nextfromheadAor fromheadBalways ends at-1, so neither list has a cycle. - Every node is on at least one of the two lists.
More Bank of America Online Assessment questions
- Return Exact Cash Register ChangeOA · Seen Aug 2026
- Sum Skipping Multiples of 7OA · Seen Nov 2025
- Wildcard Substring MatchOA · Seen Nov 2025
- Maximum String BeautyOA · Seen Sep 2024
- Remove Duplicates from a ListOA · Seen Oct 2023
- Pairs with Target Sum in a Sorted ArrayOA · Seen Oct 2023
- Count Level-Order Traversals of a TreeOA · Seen Sep 2023
- Count Set BitsOA · Seen Sep 2023
- Count Pairs with Sum KOA · Seen Jun 2023
- Letter Pattern SumOA · Seen Sep 2022
Similar questions at other companies
- First Node Two Linked Lists ShareAdobe · PHONE SCREENAdobe interview questions
- Find Where Two Linked Lists MergeExpedia · ONSITE INTERVIEWExpedia interview questions
- Where Two Linked Lists MeetNutanix · PHONE SCREENNutanix interview questions
- 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
- Find the Intersection Node of Two Linked ListsConcentric AI · OAConcentric AI interview questions
- Get Distinct Pairs (Also apply to AS intern)Amazon · OA
- Count Cross-Array Target-Sum PairsVisa · OAVisa interview questions
- Merge Two Sorted ListsThe Walt Disney Company · OAThe Walt Disney Company interview questions