Interview Questions

266. Find the intersection of two linked lists. Optimum space and time

Microsoft Interview Questions and Answers


(Continued from previous question...)

266. Find the intersection of two linked lists. Optimum space and time

Question:

Find the intersection of two linked lists. Optimum space and time


maybe an answer:


node* linkedListIntersection(node *first, node *second)
{
Hashtable<int> h1;
node *result=NULL;
//Insert all elements of firstlist in a hashtable
while(!first)
{
h1.Add(first->data);
first=first->next;
}

//Loop through the second list
while(!second)
{
if h1.contains(second->data)
{
result.Add(second);
h1.Remove(second->data);
}
second=second->next;
}
return result;
}

(Continued on next question...)

Other Interview Questions