Merge sorted linked list

getting wrong output
please check my code

I have edited your code a bit, the approach you are using for building the list in your code is not correct, So I have added two functions, one is buildlist function and other as insertAtTail function in your code… Also, the merge function you have used in the code is not correct… So you can refer to the code snippet below for the changes you need to make in tht function as,

node *merge(node *a,node *b)
{
if(a==NULL)
{
return b;
}
if(b==NULL)
{
return a;
}
node *c;
if(a->datadata)
{
c=a;
c->next=merge(a->next,b);
}
else
{
c=b;
c->next=merge(a,b->next);
}
return c;
}

1 Like

but the same take input function has worked in the k reverse linked list


you can check here .
this is running in o(n)
but the way pratik bhaiya has discussed to build lined list is running in o(n^2).

please dry run my code .
i am doing somewhere mistake .
which i am not able to find out .
i have dry run a lot of time .

The approach which I told you is correct, you can dry run on the same, the approach you are following is a little bit complicated…

1 Like