Q. Let X = (X1, X2, X3,....Xn) and Y= (Y1, Y2, Y3,....Xm) be the two linked lists respectively. Write down an algorithm to merge the lists together to get the linked list Z such that
Z = (X1, Y1, X2, Y2,....Xm, Ym,Xm+1....Xn) if m<=n or
Z = (X1, Y1,X2,Y2....Xn,Yn,Yn+1....Ym) if m>n.
Ans.
void near (*head1,*head2,*head3)
{
p1 = head1;
p2 = head2;
p3 = head3;
if (p1 != NULL)
{
p3 = head3 = p1 ;
p1=p1 next ;
}
while (p1!= NULL && p2 != NULL)
{
p3 next = p2 ; p2=p2 next; p3=p3 next;
p3 next = p1; p1 = p1 next ; p3 = p3 next ;
}
for ( ; p1 != NULL; p1 = p1 ? next)
{
p3 next = p1;
p3 = p3 next ;
}
for ( ; p2 != NULL ; p2 = p2 ? next)
{
P2 next = p2 ;
p2 = p2 next ;
}