Merge is one of those nice recursive problems where the recursive solution code is much cleaner than the iterative code
Node *Merge(Node * head1, Node *head2)
{
// Base conditions
if(! head1 || !head2)
{
if(!head1)
return head2;
else
return head1;
}
else
{
if(Node1.data <= Node2.data)
{
Node1.next = Merge(Node1.next, Node2)
return Node1;
}
else
{
Node2.next= Merge(Node1, Node2.next);
return Node2;
}
}
}
No comments:
Post a Comment