2

I have the following code to merge two sorted linked lists:

struct node* merge(struct node* a, struct node* b)
{
        struct node dummy;     

        struct node* tail = &dummy; 

        dummy.next = NULL;
        while(1)
        {
                if(a == NULL)
                {
                        tail->next = b;
                        break;
                }
                else if (b == NULL)
                {
                        tail->next = a;
                        break;
                }
                if (a->data <= b->data)
                {
                        MoveNode(&(tail->next), &a);
                }
                else
                {
                        MoveNode(&(tail->next), &b);
                }
                tail = tail->next;
        }
        return(dummy.next);
} 

void MoveNode(struct node** destRef, struct node** sourceRef)
{
        struct node* newNode = *sourceRef;

        *sourceRef = newNode->next;

        newNode->next = *destRef;

        *destRef = newNode;
}

And it worked fine. I was trying to make it into a recursive method and this is what I got:

struct node* Merge(struct node* a, struct  node* b)
{
        struct node* result;

        if (a == NULL)
                return(b);
        else if (b==NULL)
                return(a);

        if (a->data <= b->data)
        {                
                result = Merge(a->next, b);
        }
        else
        {                
                result = Merge(a, b->next);
        }
        return(result);
}

But it has many of the nodes missing in the result. What is wrong?

1
  • 1
    I think you forgot to actually build an output list in your recursive function. In your inductive case, why don't you add something to the list you got from the recursive invocation? Commented Feb 4, 2011 at 4:32

1 Answer 1

3

Your base conditions are correct. But there is problem with your recursive condition.

When you compare a's data with b's data you are not copying node a or node b into result.

Try:

struct node* result; 

if (a == NULL)         
        return(b);                     
else if (b==NULL)                              
        return(a);                                             

if (a->data <= b->data)                                                
{          
        // make result point to node a.                                        
        result = a;      
        // recursively merge the remaining nodes in list a & entire list b
        // and append the resultant list to result.
        result->next = Merge(a->next, b);
}
else                                    
{                
        result = b;
        result->next = Merge(a, b->next);               
}
return(result);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.