Given two Link List on integers which are sorted, you have to merge those two link lists and create a new LinkList which is also sorted.
list* foo(list* l1, list* l2)
{
list* head=NULL;
while(l1&&l2)
{
if(l1->val>l2->val)
{
list* temp=l2->next;
l2->next=head;
head=l2;
l2=temp;
}
else
{
list* temp=l1->next;
l1->next=head;
head=l1;
l1=temp;
}
}
while(l1)
{
list* temp=l1->next;
l1->next=head;
head=l1;
l1=temp;
}
while(l2)
{
list* temp=l2->next;
l2->next=head;
head=l2;
l2=temp;
}
return head;
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment