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 Merge two sorted linked lists K kartik Follow Improve Article Tags : C++ Microsoft Amazon Oracle Flipkart Samsung Belzabar Accolite MakeMyTrip Synopsys Brocade FactSet OATS Systems CPP-DSA +10 More Practice Tags : AccoliteAmazonBelzabarBrocadeFactSetFlipkartMakeMyTripMicrosoftOATS SystemsOracleSamsungSynopsysCPP +9 More Similar Reads Javascript Program To Merge Two Sorted Lists (In-Place) Given two sorted lists, merge them so as to produce a combined sorted list (without using extra space).Examples:Input: head1: 5->7->9 head2: 4->6->8 Output: 4->5->6->7->8->9Explanation: The output list is in sorted order.Input: head1: 1->3->5->7 head2: 2->4Outp 5 min read Merge two sorted linked lists Given two sorted linked lists consisting of n and m nodes respectively. The task is to merge both of the lists and return the head of the merged list.Examples:Input: Output: Input:Output: Table of Content[Naive Approach] By Using Array - O((n+m)*log(n+m)) Time and O(n+m) Space[Better Approach] Using 12 min read Merge K sorted linked lists Given k sorted linked lists of different sizes, the task is to merge them all maintaining their sorted order.Examples: Input: Output: Merged lists in a sorted order where every element is greater than the previous element.Input: Output: Merged lists in a sorted order where every element is greater t 15+ min read C++ Program For Merging Two Sorted Linked Lists Such That Merged List Is In Reverse Order Given two linked lists sorted in increasing order. Merge them such a way that the result list is in decreasing order (reverse order). Examples:Â Input: a: 5->10->15->40 b: 2->3->20 Output: res: 40->20->15->10->5->3->2 Input: a: NULL b: 2->3->20 Output: res: 20- 4 min read Merge K Sorted Linked Lists using Min Heap Given k sorted linked lists of different sizes, the task is to merge them all maintaining their sorted order.Examples:Input: K = 3, N = 4list1 = 1->3->5->7->NULLlist2 = 2->4->6->8->NULLlist3 = 0->9->10->11->NULLOutput: 0->1->2->3->4->5->6->7- 9 min read Merge two sorted linked lists such that merged list is in reverse order Given two linked lists sorted in increasing order. Merge them such a way that the result list is in decreasing order (reverse order). Examples: Input: a: 5->10->15->40 b: 2->3->20 Output: res: 40->20->15->10->5->3->2Input: a: NULL b: 2->3->20 Output: res: 20- 15+ min read Like