Amazon Interview Question
Software Engineer / DevelopersL1: 1->2->3->4->5
L2: 6->7->8->9->10
P is pointer on L1 and q is a pointer on L2. r is temp pointer .
Resultant list L: 1->6->2->7->3->8->4->9->5->10
=====================================
Hello Sir, can you merge two lists without using third pointer ?
Just imagine.. u declared a pointer *r,
p->next->next = r; and u r using r without declaring it...
This is a recursive implementation of alternateMerge.. the final result is available in the list pointer l1. So the function does not return anything.
The implementation assumes the lists are of equal size, so if any of the list ends, the alternateMerge ends, ignoring the rest of the elements in the other list.
void alternateMerge(node *l1,node *l2)
{
if(l1==NULL || l2==NULL) return;
node *temp;
temp=l1->next;
l1->next=l2;
l2=l2->next;
l1->next->next=temp;
alternateMerge(temp,l2);
}
- siva.sai.2020 December 15, 2010