Riverbed Technology Interview Question: Given 2 linked lists sorted i... | Glassdoor

Interview Question

Member of Technical Staff Interview(Student Candidate) Sunnyvale, CA

Given 2 linked lists sorted in ascending order, write a

  function that will merge these lists into a single sorted list without copying the list contents. The node structure is of the form: struct Node { int value; struct Node *next; };
Answer

Interview Answer

1 Answer

0

Tough one... consider this for the kernel of the algorithm:

while (list_a->next && list_b->next)
{
   list_a_tmp=list_a->next;
   list_b_tmp=list_b->next;

   if (list_a->value value && list_a->next->value > list_b->value) { list_a->next=list_b; list_a=list_a_tmp; }
   else if (list_b->value value && list_b->next->value > list_a->value) { list_b->next=list_a; list_b=list_b_tmp; }
   else if (list_a->value value && list_a->next->value value) { list_a=list_a_tmp; }
   else { list_b=list_b_tmp; }
}

Anonymous on Dec 19, 2015

Add Answers or Comments

To comment on this, Sign In or Sign Up.