Please using the c++ language to solve this question, thank you Given the same N
ID: 3717689 • Letter: P
Question
Please using the c++ language to solve this question, thank you
Given the same Node above, implement a recursive function that merges two sorted singly linked lists into a single sorted linked list. The function should return the head of the new list. You may not create any new Nodes, this is known as an in-place merge. For example suppose we have two list: list2 And we call our merge function on these two lists: Node* newList - inPlaceMerge(list1, list2); The state of our program after that function call may look like: list1 newList list2 10 Node* inPlaceMerge(Node* list1, Node* list2) (Explanation / Answer
Code to merger two sorted list:
struct Node
{
int data;
struct Node *next;
};
// Merges two given lists in-place. This function
// mainly compares head nodes and calls mergeUtil()
Node *inPlaceMerge(Node *list1, Node *list2)
{
if (!list1)
return list2;
if (!list2)
return list1;
// start with the linked list
// whose head data is the least
if (list1->data < list2->data)
{
list1->next = merge(list1->next, list2);
return list1;
}
else
{
list2->next = merge(list1, list2->next);
return list2;
}
}