268. Given two sorted linked list......
Microsoft Interview Questions and Answers
(Continued from previous question...)
268. Given two sorted linked list......
Question:
Given two sorted linked list.
Write a recursive code to find the intersection of two list.
the resulted list must be in sorted order.
all unused node should be deleted using "free()".
method Signature was given..
Node *SortedIntersection(Node *l1, Node *l2)
maybe an answer:
struct Node *SortedIntersection(struct Node *l1, struct Node *l2)
{
struct Node *n = NULL;
if(l1 == NULL || l2 == NULL)
return NULL;
else if(l1->data < l2->data) {
struct Node *t = l1; l1 = l1->next; free(t);
n = SortedIntersection(l1, l2); }
else if(l1->data > l2->data) {
struct Node *t = l2; l2 = l2->next; free(t);
n = SortedIntersection(l1, l2); }
else if(l1->data == l2->data) {
struct Node *t = l1; l1 = l1->next; free(t);
n = l2;
n->next = SortedIntersection(l1, l2->next); }
return n;
}
(Continued on next question...)
Other Interview Questions
|