ok let me check and dry run
Merge linked list
I have dryrun it and every think is fine but why its giving fault pls tell me
@guptashubham210a you are not getting the point, you cant change any variable without passing it by reference.
can you send code ?so I can compare it with mine.
kcan you send code ?so I can compare it with mine.
pls you make the required changes and just put comment there,so I will get what I have done wrong
kpls you make the required changes and just put comment there,so I will get what I have done wrong
hi @guptashubham210a add &head, thats how you send a variable by referenceā¦what is so hard to get?
in all functions,I have to add&for head?
kin all functions,I have to add&for head?
@guptashubham210a only in the functions you are changing the value of head. You cant change its value without passing it by reference⦠please clear your basics this is a really important concept
@Ishitagambhir
I know this but I have done that also then also its giving fault pls check or you pls make the changes?
@ Ishitagambhir
I have tried that but not getting output same error,there is no need to change head na if we are inserting at tail na?
hi @guptashubham210a can you please share the latest code you wrote? I will make changes in it. Please share it via the CB IDE
hi @guptashubham210a you dont need to create an array to store elements, the MLE maybe because of that. Just take input for node and insert it in the list. You are also unnecessarily increasing the space complexity of your program this way.
make a separate function for insertion, that takes as input the head of the linked list. Remember, the point of functions is to generalise. If you made the function once, you can call it as many times for different linked lists. But now you made it for 2 linked lists, which defeats the purpose of using functions. It will not give you any errors or change your time complexity, but just a advice on how you write your code.
The merge function is almost correct, although you do not need to pass m and n, as these values are not being used. The one change you need to make is: use if-else pair instead of 2 conditions, else this code will not work if there are 2 same values in linked list (neither of the conditions will be satisfied and might give run time error). You can also add the condition head1->data == head2->data separately.

Try to make these changes, especially the insertion function. I am providing the insert at tail function for your reference. https://ide.codingblocks.com/s/310083 Please note that this is not the complete code, just for taking input of 1 test case and making linked lists.
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.