LET'S TALK TECHNICAL

This blog is intended to help people prepare for the job interviews and improve their analytical skills. We have posted difficult datastructures and algorithm questions and puzzles. Interview experiences section is for the people to post their interview experiences.Views expressed here are of their personal and the blog author doesn't take any responsibility for the same.

-

Followers

Jobs

Saturday, February 2, 2008

Given two sorted linked lists, write a function to merge them into one?

list* merge(LIST* list1, LIST* list2){
LIST *it1, *it2, *head;
if(!list1) return list2;
if(!list2) return list1;
head = (list1->value <= list2->value) ? list1 : list2;
if(head == list1)
{ it1 = list1; it2 = list2;
}
else{
it1 = list2;
it2 = list1;
}
while(it1->next && it2){
if(it1->next->value >= it2->value){
list *temp = it1->next;
it1->next = it2;
it2 = it2->next;
it1->next->next = temp;
}
else{
it1 = it1->next;
}
}

if(it2)
it1->next = it2;
return head;
}

No comments:

Post a Comment

Popular Posts