Getting TLE in merging the two linked lists

I’m getting TLE for this problem. I can’t find any other way to solve this problem. Please check how can it be optimized.

Hi @mansi_sharma9kF you have declared all the nodes, head1, head2, and head outside of the while loop. so for each test case, they are NOT reset to NULL or to new inputs (since you are inserting at tail anyway).
Just declare these nodes inside the WHILE LOOP and the code will run fine.