This question may be old, but I couldn\'t think of an answer.
Say, there are two lists of different lengths, merging at a point; how do we know wher
You can add the nodes of list1
to a hashset and the loop through the second and if any node of list2
is already present in the set .If yes, then thats the merge node
static int findMergeNode(SinglyLinkedListNode head1, SinglyLinkedListNode head2) {
HashSet set=new HashSet();
while(head1!=null)
{
set.add(head1);
head1=head1.next;
}
while(head2!=null){
if(set.contains(head2){
return head2.data;
}
}
return -1;
}