Write a function that has two linked-list head pointers as parameters. Assume th
ID: 3790223 • Letter: W
Question
Write a function that has two linked-list head pointers as parameters. Assume that the linked lists items are ordered by the < operator. On each list, every item is less than the next item on the same list. The function should create a new linked list that contains all the items on both lists, and the new linked list should also be ordered (so that every item is less than the next item on the list). Then new linked list should also eliminate duplicate items (i.e., if the same item appears on both input lists, then only one copy is placed in the newly constructed linked list). To eliminate duplicate items, you may assume that two items can be compared for equality using ==. The function should return a head pointer for the newly constructed linked list
Explanation / Answer
Hers is recusrvise solution for your question.
struct Node* MergeTwoList(struct Node* head1, struct Node* head2)
{
struct Node* res = NULL;
if (head1 == NULL)
return(head2);
else if (head2==NULL)
return(head1);
if (head1->data <= head2->data)
{
res = head1;
res->next = MergseTwoList(head1->next, head2);
}
else
{
res = head2;
res->next = MergseTwoList(head1, head2->next);
}
return res
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.