My this code is not working for this code, Please help me

My this code is not working for this code, Please help me.


hello @cbcao263

u r inserting in the same list .

create a NULL node and then bulid ur list using that.

check this , replaced (stack + queue) with deque ->

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.

I have even done that Sir, Declared head2 as NULL and then inserted and it shows 1->2->3->4

Okay You inserted in deque as we can follow both LIFO and FIFO properties there.

And Is there any method to use because It will take a lot of time.

@cbcao263

The basic idea is to split the list in half, then reverse the second half, and at last merge them. It is O(n) time, O(1) space. I was also wondering if there is a better solution.

1 Like

awesome approach sir.
Sir but I am not able to understand how you merge them.

code of inside while loop(for merging) is equivalent to ->

curNode = head1->next;
head1->next = head2;
head1 = head1->next;
head2 = curNode;

do a simple dry run , u will get its functioning

or check this if this make sense to u->

image