C++ Program To Merge Two Sorted Lists (In-Place) Last Updated : 27 Aug, 2024 Comments Improve Suggest changes Like Article Like Report Write a C++ program to merge two sorted linked lists into a single sorted linked list in place (i.e. without using extra space).Examples:Input: List1: 5 -> 7 -> 9, list2: 4 -> 6 -> 8 Output: 4->5->6->7->8->9Input: List1: 1 -> 3 -> 5 -> 7, List2: 2 -> 4Output: 1 -> 2 -> 3 -> 4 -> 5 -> 7Algorithm to Merge Two Sorted Lists (In-Place) in C++Merging two sorted linked lists using recursion involves combining them into one list while maintaining the order of the elements. The approach to merge two sorted linked list in place is as follows:Start from the beginning of the two linked listIf both the elements are NULL return null. Otherwise,Compare the current elements of both linked lists.Set the smaller element's next pointer to the element that is return by the recursive call where we pass same linked lists except omitting the smaller element. This call will return the next smaller element between the linked lists.Return the smaller element.If one list ends before another, return the other list.To know more methods for merging, refer to the article - Merge two sorted lists (in-place)Implementation C++ // C++ program to merge two sorted linked lists in-place. #include <bits/stdc++.h> using namespace std; class Node { public: int data; Node* next; Node(int key) { this->data = key; this->next = NULL; } }; Node* mergeInPlace(Node* h1, Node* h2) { // Return NULL when both of the linked list are empty if (!h1 && !h2) return NULL; // If one list ends, returns the remaining one. if (!h1) return h2; if (!h2) return h1; // Return and set the next pointer of the smaller node if (h1->data < h2->data) { h1->next = mergeInPlace(h1->next, h2); return h1; } else { h2->next = mergeInPlace(h1, h2->next); return h2; } } int main() { // First Linked List: 1 -> 3 -> 5 Node* list1 = new Node(1); list1->next = new Node(3); list1->next->next = new Node(5); // Second Linked List: 0 ->2 -> 4 Node* list2 = new Node(0); list2->next = new Node(2); list2->next->next = new Node(4); Node* result = mergeInPlace(list1, list2); // Printing the resultant list Node* temp = result; while (temp != NULL) { printf("%d ", temp->data); temp = temp->next; } return 0; } Output0 1 2 3 4 5 Time Complexity: O (N + M), where N is the size of first list and M is the size of second list.Auxiliary Space: O (N + M), needed by recursion call stack.We can implement this using iteration too which saves the stack space consumed by this implementation. But that approach is complex to understand. Refer to this article for it - Merge two sorted lists (in-place) Comment More infoAdvertise with us Next Article C++ Program To Merge Two Sorted Lists (In-Place) kartik Follow Improve Article Tags : Linked List C++ Programs C++ DSA Microsoft Amazon Oracle Flipkart Samsung Belzabar Accolite MakeMyTrip Synopsys Brocade FactSet OATS Systems CPP-DSA +13 More Practice Tags : AccoliteAmazonBelzabarBrocadeFactSetFlipkartMakeMyTripMicrosoftOATS SystemsOracleSamsungSynopsysCPPLinked List +10 More Similar Reads C++ Program To Merge K Sorted Linked Lists - Set 1 Given K sorted linked lists of size N each, merge them and print the sorted output. Examples: Input: k = 3, n = 4 list1 = 1->3->5->7->NULL list2 = 2->4->6->8->NULL list3 = 0->9->10->11->NULL Output: 0->1->2->3->4->5->6->7->8->9->10- 6 min read C++ Program to Merge Two Sorted Arrays Given two sorted arrays, the task is to merge them in a sorted manner.Examples:Â Input: arr1[] = { 1, 3, 4, 5}, arr2[] = {2, 4, 6, 8}Â Output: arr3[] = {1, 2, 3, 4, 4, 5, 6, 8} Input: arr1[] = { 5, 8, 9}, arr2[] = {4, 7, 8}Â Output: arr3[] = {4, 5, 7, 8, 8, 9}Â Recommended: Please solve it on âPRACTIC 5 min read C++ Program To Merge K Sorted Linked Lists Using Min Heap - Set 2 Given k linked lists each of size n and each list is sorted in non-decreasing order, merge them into a single sorted (non-decreasing order) linked list and print the sorted linked list as output.Examples:Â Input: k = 3, n = 4 list1 = 1->3->5->7->NULL list2 = 2->4->6->8->NULL list3 = 0->9->10->11->NU 5 min read C++ Program to Merge 3 Sorted Arrays Given 3 arrays (A, B, C) which are sorted in ascending order, we are required to merge them together in ascending order and output the array D. Examples: Input : A = [1, 2, 3, 4, 5] B = [2, 3, 4] C = [4, 5, 6, 7] Output : D = [1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 6, 7] Input : A = [1, 2, 3, 5] B = [6, 7, 8 9 min read C++ Program For Merge Sort For Doubly Linked List Given a doubly linked list, write a function to sort the doubly linked list in increasing order using merge sort.For example, the following doubly linked list should be changed to 24810 Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Merge sort for singly linked l 3 min read Like