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; };

0

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

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

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

Anonymous on Dec 19, 2015