Interview Questions

255.Merge two linked lists: Node* MergeList.....

Microsoft Interview Questions and Answers


(Continued from previous question...)

255.Merge two linked lists: Node* MergeList.....

Question:
Merge two linked lists: Node* MergeList(Node *list1, Node* list2). Do not create a node in the function while merging the lists.
Struct Node{
int data;
struct node* p;
}



maybe an answer:


Node Merge(Node L,Node a,Node b)
{
Node t=L;
do
{
if(a->element>b->element)
{
t->next=a;
t=a;
a=a->next;
}
else
{
t->next=b;
t=b;
b=b->next;
}
}
while(a!=null && b!=null)

if(a==null)
t->next=b;
if(b==null)
t->next=a;

return L;
}

(Continued on next question...)

Other Interview Questions