Advanced Operations on Linked Lists Implement a function that merges (in other w
ID: 3857595 • Letter: A
Question
Advanced Operations on Linked Lists Implement a function that merges (in other words puts them together) two sorted linked lists so that the new list is also sorted. It takes as an argument the head of two lists that are already sorted from smallest to largest. Then, it returns the new head for the joint list that is also sorted from smallest to largest. From the new head it should be possible to reach all other nodes i in the combined list. typedef struct node { int data: struct node *next: } List: List * sorted_merge (List * a, List * b);Explanation / Answer
Hi,
Please find below the method-
List * sorted_merge(List * a, List * b){
List *resultHead, *resultTail, *temp;
resultHead = resultTail = NULL;
while(1){
/* */
if(a == NULL){
resultTail->next = b;
break;
}
if(b == NULL) {
resultTail->next = a;
break;
}
/* Check whether current node of
which Linked list is smaller*/
if(a->data <= b->data){
temp = a;
a = a->next;
} else {
temp = b;
b = b->next;
}
/*Add smaller node to result linked list */
if(resultHead == NULL){
resultHead = resultTail = temp;
} else {
resultTail->next = temp;
resultTail = temp;
}
resultTail->next = NULL;
}
return resultHead;
}
Regards,
Vinay Singh
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.