What is my mistake in this question


Here is the code

hi @hhiteshbansal_40d12e2460a1738e
refer this -->

which thing did i did wrong in my code

hi @hhiteshbansal_40d12e2460a1738e
give a newline after every test case…

OKAY but what was my mistake why testcases are showing RUN ERROR

hi @hhiteshbansal_40d12e2460a1738e
so ur code was failing on test cases where elements from both linked list had same value…
consider this test case–>

1
2
2 4
2
1 2

this happened bcoz while merging the linked lists u were not checking the condition if elements from both lists are equal…
what u did was -->

if(head1->data<head2->data){
    head3=head1;
   head3->next= merge(head1->next,head2);
}
 if(head2->data<head1->data){
    head3=head2;
    head3->next=merge(head1,head2->next);
}  

correction -->

if(head1->data<head2->data){
    head3=head1;
   head3->next= merge(head1->next,head2);
}
 if(head2->data<=head1->data){ //here make <= instead of only <
    head3=head2;
    head3->next=merge(head1,head2->next);
}  

rest give a newline after each test case… ur code will pass all test cases…

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.