This function merge two sorted linked list and create new sorted list. Without changing other lists
linkedlist linkedlist::merge(linkedlist l1,linkedlist l2)
{
linkedlist l3;
if (l1.isEmpty()&&l2.isEmpty())
{
return l3;
}
else if (l1.isEmpty())
{
return l2;
}
else if (l2.isEmpty())
{
return l1;
}
else
{
Node *ptr1=l1.first,*ptr2=l2.first;
while (ptr1!=NULL && ptr2!=NULL)
{
if (ptr1->data<ptr2->data)
{
l3.append(ptr1->data);
ptr1=ptr1->next;
}
else
{
l3.append(ptr2->data);
ptr2=ptr2->next;
}
}
if (ptr1!=NULL)
{
while (ptr1!=NULL)
{
l3.append(ptr1->data);
ptr1=ptr1->next;
}
return l3;
}
else if (ptr2!=NULL)
{
while (ptr2!=NULL)
{
l3.append(ptr2->data);
ptr2=ptr2->next;
}
return l3;
}
}
}
2 comments:
Excellent work ahmad just keep it up.😃
chaa gea hai Chaudhry (Y)
Post a Comment