Given 2 sorted linked lists - merge them .....
Microsoft Interview Questions and Answers
(Continued from previous question...)
96. Given 2 sorted linked lists - merge them .....
Question:
Given 2 sorted linked lists - merge them. Make sure you don't have duplicates in the merged list. The input lists could have duplicates within them or across the 2 lists.
maybe an answer:
node *merge(node *p,node *q)
{
if(p==NULL) return q;
if(q==NULL) return p;
if(p->data==q->data)
{
p->next=merge(p->next,q->next);
delete q;
return p;
}
if(p->data>q->data)
{
q->next=merge(p,q->next);
return q;
}
p->next=merge(p->next,q);
return p;
}
(Continued on next question...)
Other Interview Questions
|