3. Advanced Operations on Linked Lists (33 points) Implement a function that mer
ID: 3826871 • Letter: 3
Question
3. Advanced Operations on Linked Lists
(33 points) Implement a function that merges (in other words puts them together) two linked lists. It takes as an argument the head of two lists and returns the new head for the joint list. From the new head it should be possible to reach all other nodes in the combined list. The combined list does not need to retain the order of the original lists. Any ordering is allowed after the merge.
typedef int Item;
typedef struct node {
Item data;
struct node *next;
} List;
List * merge(List * a, List *b);
Explanation / Answer
typedef int Item;
typedef struct node {
Item data;
struct node *next;
} List;
List * merge(List * a, List *b)
{
// get head of current list
List *temp = a;
if(a == NULL)
return b;
// go to last node of the list
while(temp->next != NULL)
{
temp = temp->next;
}
// make b's head as next of last node
temp->next = b;
return a;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.