#include template class Node { public: T value; Node* next; Node* prev; // constructor Node(T val) : value(val), next(nullptr), prev(nullptr) {} }; // function to merge two sorted doubly linked lists // this function returns a pointer pointing to the head node of the merged list. template Node* mergeLists(Node* head_A, Node* head_B) { // If one list is empty, return the other if (!head_A) return head_B; if (!head_B) return head_A; // Decide the new head by comparing the first elements Node* head = nullptr; Node* tail = nullptr; // Pick whichever head is smaller as the new head if (head_A->value < head_B->value) { head = head_A; head_A = head_A->next; } else { head = head_B; head_B = head_B->next; } // At this point, 'head' is the first node of the merged list head->prev = nullptr; // new head has no previous tail = head; // Continue merging while both lists have nodes while (head_A && head_B) { if (head_A->value < head_B->value) { tail->next = head_A; head_A->prev = tail; tail = tail->next; head_A = head_A->next; } else { tail->next = head_B; head_B->prev = tail; tail = tail->next; head_B = head_B->next; } } // Append any remaining nodes from A or B if (head_A) { tail->next = head_A; head_A->prev = tail; } else if (head_B) { tail->next = head_B; head_B->prev = tail; } return head; } int main() { // create 5 nodes and link them to form a linked list, this is linked list A. Node* head_A = new Node(1); Node* second_A = new Node(3); Node* third_A = new Node(5); Node* fourth_A = new Node(7); Node* fifth_A = new Node(9); // link the nodes head_A->next = second_A; second_A->prev = head_A; second_A->next = third_A; third_A->prev = second_A; third_A->next = fourth_A; fourth_A->prev = third_A; fourth_A->next = fifth_A; fifth_A->prev = fourth_A; // traverse linked list A and print the values Node* current = head_A; while (current != nullptr) { std::cout << current->value << " "; current = current->next; } std::cout << std::endl; // create 5 nodes and link them to form a linked list, this is linked list B. Node* head_B = new Node(2); Node* second_B = new Node(4); Node* third_B = new Node(6); Node* fourth_B = new Node(8); Node* fifth_B = new Node(10); // link the nodes head_B->next = second_B; second_B->prev = head_B; second_B->next = third_B; third_B->prev = second_B; third_B->next = fourth_B; fourth_B->prev = third_B; fourth_B->next = fifth_B; fifth_B->prev = fourth_B; // traverse linked list B and print the values current = head_B; while (current != nullptr) { std::cout << current->value << " "; current = current->next; } std::cout << std::endl; Node* head_C; Node* tail_C; head_C = mergeLists(head_A, head_B); // traverse linked list C and print the values current = head_C; while (current != nullptr) { std::cout << current->value << " "; // keep tracking current and when current reaches nullptr, tail_C will be the tail node. tail_C = current; current = current->next; } std::cout << std::endl; // traverse linked list C backwards and print the values current = tail_C; while (current != nullptr) { std::cout << current->value << " "; current = current->prev; } std::cout << std::endl; return 0; }