Difference of two Linked Lists using Merge sort Last Updated : 04 Dec, 2023 Comments Improve Suggest changes Like Article Like Report Given two Linked List, the task is to create a Linked List to store the difference of Linked List 1 with Linked List 2, i.e. the elements present in List 1 but not in List 2.Examples: Input: List1: 10 -> 15 -> 4 ->20, List2: 8 -> 4 -> 2 -> 10 Output: 15 -> 20 Explanation: In the given linked list elements 15 and 20 are present in the list 1 but not in list 2.Input: List1: 2 -> 4 -> 8 -> 10, List2: 8 -> 10 Output: 2 -> 4 Explanation: In the given linked list 1 elements 2 and 4 are present in the list 1 but not in list 2. Approach: Sort both Linked Lists using merge sort.Linearly scan both sorted lists to get the difference with two pointers on it p1 and p2 and compare the data of the nodes in the linked list and perform following in below three cases - If p1.data == p2.data then, p1.data cannot be in the difference list, So move the pointers p1 and p2 ahead.If p1.data > p2.data then, p1.data may be present in list 2 in further nodes, So move the pointer p2 ahead.If p1.data > p2.data then, p1.data cannot be present in list 2 now, So add the data of p1 into difference list and move pointer p1 ahead.If the end of list 2 is reached, insert all the remaining elements of list 1 into the difference list.Below is the implementation of the above approach. C++ //C++ program to implement above approach #include <iostream> class Node { public: int data; Node* next; Node(int val) : data(val), next(nullptr) {} }; class LinkedList { public: Node* head; LinkedList() : head(nullptr) {} // Function to insert a node at the end of the linked list void append(int data) { Node* temp = new Node(data); if (head == nullptr) { head = temp; } else { Node* p = head; while (p->next != nullptr) { p = p->next; } p->next = temp; } } // Function to find the middle node of the linked list Node* get_mid(Node* head) { if (head == nullptr) { return head; } Node* slow = head; Node* fast = head; while (fast->next != nullptr && fast->next->next != nullptr) { slow = slow->next; fast = fast->next->next; } return slow; } // Recursive method to merge two halves after sorting Node* merge(Node* l, Node* r) { if (l == nullptr) return r; if (r == nullptr) return l; Node* result; if (l->data <= r->data) { result = l; result->next = merge(l->next, r); } else { result = r; result->next = merge(l, r->next); } return result; } // Recursive method to divide the list into two halves until 1 node left Node* merge_sort(Node* head) { if (head == nullptr || head->next == nullptr) { return head; } Node* mid = get_mid(head); Node* next_to_mid = mid->next; mid->next = nullptr; Node* left = merge_sort(head); Node* right = merge_sort(next_to_mid); Node* sorted_merge = merge(left, right); return sorted_merge; } // Function to print the list elements void display() { Node* p = head; while (p != nullptr) { std::cout << p->data << " "; p = p->next; } std::cout << std::endl; } }; // Function to get the difference list LinkedList get_difference(Node* p1, Node* p2) { LinkedList difference_list; // Scan the lists while (p1 != nullptr && p2 != nullptr) { // Condition to check if the data of both pointers are the same, then move ahead if (p2->data == p1->data) { p1 = p1->next; p2 = p2->next; } else if (p2->data < p1->data) { // Condition to check if the data of the second pointer is smaller than the first, then move the second pointer ahead p2 = p2->next; } else { // Condition when the data of the first pointer is greater than the second, then append into the difference list and move difference_list.append(p1->data); p1 = p1->next; } } // If the end of list2 is reached, there may be some nodes in List 1 left to be scanned, // they all will be inserted in the difference list while (p1 != nullptr) { difference_list.append(p1->data); p1 = p1->next; } return difference_list; } int main() { // Linked List 1 LinkedList list1; list1.append(2); list1.append(6); list1.append(8); list1.append(1); // Linked List 2 LinkedList list2; list2.append(4); list2.append(1); list2.append(9); // Sort both linked lists list1.head = list1.merge_sort(list1.head); list2.head = list2.merge_sort(list2.head); // Get the difference list LinkedList result = get_difference(list1.head, list2.head); if (result.head) { result.display(); } else { std::cout << "Lists are equal" << std::endl; } return 0; } Java class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } } class LinkedList { Node head; LinkedList() { head = null; } // Function to insert a node at the end of Linked List void append(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; } else { Node current = head; while (current.next != null) { current = current.next; } current.next = newNode; } } // Function to find the middle node of the Linked List Node getMid(Node head) { if (head == null) { return head; } Node slow = head; Node fast = head; while (fast.next != null && fast.next.next != null) { slow = slow.next; fast = fast.next.next; } return slow; } // Recursive method to merge two halves after sorting Node merge(Node left, Node right) { if (left == null) { return right; } if (right == null) { return left; } Node result; if (left.data <= right.data) { result = left; result.next = merge(left.next, right); } else { result = right; result.next = merge(left, right.next); } return result; } // Recursive method to divide the list into two halves until 1 node left Node mergeSort(Node head) { if (head == null || head.next == null) { return head; } Node mid = getMid(head); Node nextToMid = mid.next; mid.next = null; Node left = mergeSort(head); Node right = mergeSort(nextToMid); return merge(left, right); } // Function to print the list elements void display() { Node current = head; while (current != null) { System.out.print(current.data + " "); current = current.next; } System.out.println(); } // Function to get the difference list static LinkedList getDifference(Node p1, Node p2) { LinkedList differenceList = new LinkedList(); // Scan the lists while (p1 != null && p2 != null) { // Condition to check if the data of both pointers are the same, then move ahead if (p2.data == p1.data) { p1 = p1.next; p2 = p2.next; } else if (p2.data < p1.data) { // Condition to check if the data of the second pointer is smaller than the first, then move the second pointer ahead p2 = p2.next; } else { // Condition when the data of the first pointer is greater than the second, then append into the difference list and move differenceList.append(p1.data); p1 = p1.next; } } // If the end of list2 is reached, there may be some nodes in List 1 left to be scanned, they all will be inserted in the difference list while (p1 != null) { differenceList.append(p1.data); p1 = p1.next; } return differenceList; } } public class Main { public static void main(String[] args) { // Linked List 1 LinkedList list1 = new LinkedList(); list1.append(2); list1.append(6); list1.append(8); list1.append(1); // Linked List 2 LinkedList list2 = new LinkedList(); list2.append(4); list2.append(1); list2.append(9); // Sort both the linked lists list1.head = list1.mergeSort(list1.head); list2.head = list2.mergeSort(list2.head); // Get the difference list LinkedList result = LinkedList.getDifference(list1.head, list2.head); if (result.head != null) { result.display(); } else { System.out.println("Lists are equal"); } } } Python3 # Python implementation to create # a difference Linked List of # two Linked Lists # Node of the Linked List class Node: def __init__(self, data): self.data = data self.next = None # Linked List class linked_list: def __init__(self): self.head = None # Function to insert a node # at the end of Linked List def append(self, data): temp = Node(data) if self.head == None: self.head = temp else: p = self.head while p.next != None: p = p.next p.next = temp # Function to find the middle # node of the Linked List def get_mid(self, head): if head == None: return head slow = fast = head while fast.next != None \ and fast.next.next != None: slow = slow.next fast = fast.next.next return slow # Recursive method to merge the # two half after sorting def merge(self, l, r): if l == None:return r if r == None:return l if l.data<= r.data: result = l result.next = \ self.merge(l.next, r) else: result = r result.next = \ self.merge(l, r.next) return result # Recursive method to divide the # list into two half until 1 node left def merge_sort(self, head): if head == None or head.next == None: return head mid = self.get_mid(head) next_to_mid = mid.next mid.next = None left = self.merge_sort(head) right = self.merge_sort(next_to_mid) sorted_merge = self.merge(left, right) return sorted_merge # Function to print the list elements def display(self): p = self.head while p != None: print(p.data, end =' ') p = p.next print() # Function to get the difference list def get_difference(p1, p2): difference_list = linked_list() # Scan the lists while p1 != None and p2 != None: # Condition to check if the # Data of the both pointer are # same then move ahead if p2.data == p1.data: p1 = p1.next p2 = p2.next # Condition to check if the # Data of the first pointer is # greater than second then # move second pointer ahead elif p2.data<p1.data: p2 = p2.next # Condition when first pointer # data is greater than the # second pointer then append # into the difference list and move else: difference_list.append(p1.data) p1 = p1.next # If end of list2 is reached, # there may be some nodes in # List 1 left to be scanned, # they all will be inserted # in the difference list if p2 == None: while p1: difference_list.append(p1.data) p1 = p1.next return difference_list # Driver Code if __name__ == '__main__': # Linked List 1 list1 = linked_list() list1.append(2) list1.append(6) list1.append(8) list1.append(1) # Linked List 2 list2 = linked_list() list2.append(4) list2.append(1) list2.append(9) # Sort both the linkedlists list1.head = list1.merge_sort( list1.head ) list2.head = list2.merge_sort( list2.head ) # Get difference list result = get_difference( list1.head, list2.head ) if result.head: result.display() # if difference list is empty, # then lists are equal else: print('Lists are equal') C# using System; class Node { public int data; public Node next; public Node(int data) { this.data = data; this.next = null; } } class LinkedList { public Node head; public LinkedList() { head = null; } // Function to insert a node at the end of Linked List public void Append(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; } else { Node current = head; while (current.next != null) { current = current.next; } current.next = newNode; } } // Function to find the middle node of the Linked List public Node GetMid(Node head) { if (head == null) { return head; } Node slow = head; Node fast = head; while (fast.next != null && fast.next.next != null) { slow = slow.next; fast = fast.next.next; } return slow; } // Recursive method to merge two halves after sorting public Node Merge(Node left, Node right) { if (left == null) { return right; } if (right == null) { return left; } Node result; if (left.data <= right.data) { result = left; result.next = Merge(left.next, right); } else { result = right; result.next = Merge(left, right.next); } return result; } // Recursive method to divide the list into two halves // until 1 node left public Node MergeSort(Node head) { if (head == null || head.next == null) { return head; } Node mid = GetMid(head); Node nextToMid = mid.next; mid.next = null; Node left = MergeSort(head); Node right = MergeSort(nextToMid); return Merge(left, right); } // Function to print the list elements public void Display() { Node current = head; while (current != null) { Console.Write(current.data + " "); current = current.next; } Console.WriteLine(); } // Function to get the difference list public static LinkedList GetDifference(Node p1, Node p2) { LinkedList differenceList = new LinkedList(); // Scan the lists while (p1 != null && p2 != null) { // Condition to check if the data of both // pointers are the same, then move ahead if (p2.data == p1.data) { p1 = p1.next; p2 = p2.next; } else if (p2.data < p1.data) // Condition to check if the // data of the second // pointer is smaller than // the first, then move the // second pointer ahead { p2 = p2.next; } else { // Condition when the data of the first // pointer is greater than the second, // then append into the difference list // and move differenceList.Append(p1.data); p1 = p1.next; } } // If the end of list2 is reached, there may be some // nodes in List 1 left to be scanned, they all will // be inserted in the difference list while (p1 != null) { differenceList.Append(p1.data); p1 = p1.next; } return differenceList; } } class Program { public static void Main(string[] args) { // Linked List 1 LinkedList list1 = new LinkedList(); list1.Append(2); list1.Append(6); list1.Append(8); list1.Append(1); // Linked List 2 LinkedList list2 = new LinkedList(); list2.Append(4); list2.Append(1); list2.Append(9); // Sort both the linked lists list1.head = list1.MergeSort(list1.head); list2.head = list2.MergeSort(list2.head); // Get the difference list LinkedList result = LinkedList.GetDifference( list1.head, list2.head); if (result.head != null) { result.Display(); } else { Console.WriteLine("Lists are equal"); } } } JavaScript class Node { constructor(data) { this.data = data; this.next = null; } } class LinkedList { constructor() { this.head = null; } // Function to insert a node at the end of Linked List append(data) { const newNode = new Node(data); if (this.head === null) { this.head = newNode; } else { let current = this.head; while (current.next !== null) { current = current.next; } current.next = newNode; } } // Function to find the middle node of the Linked List getMid(head) { if (head === null) { return head; } let slow = head; let fast = head; while (fast.next !== null && fast.next.next !== null) { slow = slow.next; fast = fast.next.next; } return slow; } // Recursive method to merge two halves after sorting merge(left, right) { if (left === null) { return right; } if (right === null) { return left; } let result; if (left.data <= right.data) { result = left; result.next = this.merge(left.next, right); } else { result = right; result.next = this.merge(left, right.next); } return result; } // Recursive method to divide the list into two halves until 1 node left mergeSort(head) { if (head === null || head.next === null) { return head; } const mid = this.getMid(head); const nextToMid = mid.next; mid.next = null; const left = this.mergeSort(head); const right = this.mergeSort(nextToMid); return this.merge(left, right); } // Function to print the list elements display() { let current = this.head; let result = ''; while (current !== null) { result += current.data + ' '; current = current.next; } console.log(result.trim()); } // Function to get the difference list static getDifference(p1, p2) { const differenceList = new LinkedList(); // Scan the lists while (p1 !== null && p2 !== null) { // Condition to check if the data of both pointers are the same, then move ahead if (p2.data === p1.data) { p1 = p1.next; p2 = p2.next; } else if (p2.data < p1.data) { // Condition to check if the data of the second pointer is smaller than the first, then move the second pointer ahead p2 = p2.next; } else { // Condition when the data of the first pointer is greater than the second, then append into the difference list and move differenceList.append(p1.data); p1 = p1.next; } } // If the end of list2 is reached, there may be some nodes in List 1 left to be scanned, they all will be inserted in the difference list while (p1 !== null) { differenceList.append(p1.data); p1 = p1.next; } return differenceList; } } // Linked List 1 const list1 = new LinkedList(); list1.append(2); list1.append(6); list1.append(8); list1.append(1); // Linked List 2 const list2 = new LinkedList(); list2.append(4); list2.append(1); list2.append(9); // Sort both the linked lists list1.head = list1.mergeSort(list1.head); list2.head = list2.mergeSort(list2.head); // Get the difference list const result = LinkedList.getDifference(list1.head, list2.head); if (result.head !== null) { result.display(); } else { console.log("Lists are equal"); } Output2 6 8Time complexity: O(M Log M + N Log N). Comment More infoAdvertise with us N naina024 Follow Improve Article Tags : Linked List Python Data Structures DSA Practice Tags : Data StructuresLinked Listpython Similar Reads Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge 14 min read Merge sort in different languagesC Program for Merge SortMerge Sort is a comparison-based sorting algorithm that works by dividing the input array into two halves, then calling itself for these two halves, and finally it merges the two sorted halves. In this article, we will learn how to implement merge sort in C language.What is Merge Sort Algorithm?Merg 3 min read C++ Program For Merge SortMerge Sort is a comparison-based sorting algorithm that uses divide and conquer paradigm to sort the given dataset. It divides the dataset into two halves, calls itself for these two halves, and then it merges the two sorted halves.In this article, we will learn how to implement merge sort in a C++ 4 min read Java Program for Merge SortMerge Sort is a divide-and-conquer algorithm. It divides the input array into two halves, calls itself the two halves, and then merges the two sorted halves. The merge() function is used for merging two halves. The merge(arr, l, m, r) is a key process that assumes that arr[l..m] and arr[m+1..r] are 3 min read Merge Sort in PythonMerge Sort is a Divide and Conquer algorithm. It divides input array in two halves, calls itself for the two halves and then merges the two sorted halves. The merge() function is used for merging two halves. The merge(arr, l, m, r) is key process that assumes that arr[l..m] and arr[m+1..r] are sorte 4 min read Merge Sort using Multi-threadingMerge Sort is a popular sorting technique which divides an array or list into two halves and then start merging them when sufficient depth is reached. Time complexity of merge sort is O(nlogn).Threads are lightweight processes and threads shares with other threads their code section, data section an 14 min read Variations of Merge Sort3-way Merge SortMerge Sort is a divide-and-conquer algorithm that recursively splits an array into two halves, sorts each half, and then merges them. A variation of this is 3-way Merge Sort, where instead of splitting the array into two parts, we divide it into three equal parts. In traditional Merge Sort, the arra 13 min read Iterative Merge SortGiven an array of size n, the task is to sort the given array using iterative merge sort.Examples:Input: arr[] = [4, 1, 3, 9, 7]Output: [1, 3, 4, 7, 9]Explanation: The output array is sorted.Input: arr[] = [1, 3 , 2]Output: [1, 2, 3]Explanation: The output array is sorted.You can refer to Merge Sort 9 min read In-Place Merge SortImplement Merge Sort i.e. standard implementation keeping the sorting algorithm as in-place. In-place means it does not occupy extra memory for merge operation as in the standard case. Examples: Input: arr[] = {2, 3, 4, 1} Output: 1 2 3 4 Input: arr[] = {56, 2, 45} Output: 2 45 56 Approach 1: Mainta 15+ min read In-Place Merge Sort | Set 2Given an array A[] of size N, the task is to sort the array in increasing order using In-Place Merge Sort. Examples: Input: A = {5, 6, 3, 2, 1, 6, 7}Output: {1, 2, 3, 5, 6, 6, 7} Input: A = {2, 3, 4, 1}Output: {1, 2, 3, 4} Approach: The idea is to use the inplace_merge() function to merge the sorted 7 min read Merge Sort with O(1) extra space merge and O(n log n) time [Unsigned Integers Only]We have discussed Merge sort. How to modify the algorithm so that merge works in O(1) extra space and algorithm still works in O(n Log n) time. We may assume that the input values are integers only. Examples: Input : 5 4 3 2 1 Output : 1 2 3 4 5 Input : 999 612 589 856 56 945 243 Output : 56 243 589 10 min read Merge Sort in Linked ListMerge Sort for Linked ListsGiven a singly linked list, The task is to sort the linked list in non-decreasing order using merge sort.Examples: Input: 40 -> 20 -> 60 -> 10 -> 50 -> 30 -> NULLOutput: 10 -> 20 -> 30 -> 40 -> 50 -> 60 -> NULLInput: 9 -> 5 -> 2 -> 8 -> NULLOutput: 2 - 12 min read Merge Sort for Doubly Linked ListGiven a doubly linked list, The task is to sort the doubly linked list in non-decreasing order using merge sort.Examples:Input: 10 <-> 8 <-> 4 <-> 2Output: 2 <-> 4 <-> 8 <-> 10Input: 5 <-> 3 <-> 2Output: 2 <-> 3 <-> 5 Note: Merge sort for a 13 min read Iterative Merge Sort for Linked ListGiven a singly linked list of integers, the task is to sort it using iterative merge sort.Examples:Input: 40 -> 20 -> 60 -> 10 -> 50 -> 30 -> NULLOutput: 10 -> 20 -> 30 -> 40 -> 50 -> 60 -> NULLInput: 9 -> 5 -> 2 -> 8 -> NULLOutput: 2 -> 5 -> 8 - 13 min read Merge two sorted lists (in-place)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.Example:Input: Output: Input: Output: Approach:The idea is to iteratively merge two sorted linked lists using a dummy node to simplify the process. A 9 min read Merge K sorted Doubly Linked List in Sorted OrderGiven K sorted doubly linked list. The task is to merge all sorted doubly linked list in single sorted doubly linked list means final list must be sorted.Examples: Input: List 1 : 2 <-> 7 <-> 8 <-> 12 <-> 15 <-> NULL List 2 : 4 <-> 9 <-> 10 <-> NULL Li 15+ min read Merge a linked list into another linked list at alternate positionsGiven two singly linked lists, The task is to insert nodes of the second list into the first list at alternate positions of the first list and leave the remaining nodes of the second list if it is longer.Example:Input: head1: 1->2->3 , head2: 4->5->6->7->8Output: head1: 1->4- 8 min read Find a permutation that causes worst case of Merge Sort Given a set of elements, find which permutation of these elements would result in worst case of Merge Sort.Asymptotically, merge sort always takes O(n Log n) time, but the cases that require more comparisons generally take more time in practice. We basically need to find a permutation of input eleme 12 min read How to make Mergesort to perform O(n) comparisons in best case? As we know, Mergesort is a divide and conquer algorithm that splits the array to halves recursively until it reaches an array of the size of 1, and after that it merges sorted subarrays until the original array is fully sorted. Typical implementation of merge sort works in O(n Log n) time in all thr 3 min read Concurrent Merge Sort in Shared Memory Given a number 'n' and a n numbers, sort the numbers using Concurrent Merge Sort. (Hint: Try to use shmget, shmat system calls).Part1: The algorithm (HOW?) Recursively make two child processes, one for the left half, one of the right half. If the number of elements in the array for a process is less 10 min read Visualization of Merge SortSorting Algorithm Visualization : Merge SortThe human brain can easily process visuals instead of long codes to understand the algorithms. In this article, a program that program visualizes the Merge sort Algorithm has been implemented. The GUI(Graphical User Interface) is implemented using pygame package in python. Approach: An array of rand 3 min read Merge Sort Visualization in JavaScriptGUI(Graphical User Interface) helps users with better understanding programs. In this article, we will visualize Merge Sort using JavaScript. We will see how the arrays are divided and merged after sorting to get the final sorted array. Refer: Merge SortCanvas in HTMLAsynchronous Function in JavaSc 4 min read Visualize Merge sort Using Tkinter in PythonPrerequisites: Python GUI â tkinter In this article, we will create a GUI application that will help us to visualize the algorithm of merge sort using Tkinter in Python. Merge Sort is a popular sorting algorithm. It has a time complexity of N(logN) which is faster than other sorting algorithms like 5 min read Visualization of Merge sort using MatplotlibPrerequisites: Introduction to Matplotlib, Merge Sort Visualizing algorithms makes it easier to understand them by analyzing and comparing the number of operations that took place to compare and swap the elements. For this we will use matplotlib, to plot bar graphs to represent the elements of the a 3 min read 3D Visualisation of Merge Sort using MatplotlibVisualizing algorithms makes it easier to understand them by analyzing and comparing the number of operations that took place to compare and swap the elements. 3D visualization of algorithms is less common, for this we will use matplotlib to plot bar graphs and animate them to represent the elements 3 min read Some problems on Merge SortCount Inversions of an ArrayGiven an integer array arr[] of size n, find the inversion count in the array. Two array elements arr[i] and arr[j] form an inversion if arr[i] > arr[j] and i < j.Note: Inversion Count for an array indicates that how far (or close) the array is from being sorted. If the array is already sorted 15+ min read Count of smaller elements on right side of each element in an Array using Merge sortGiven an array arr[] of N integers, the task is to count the number of smaller elements on the right side for each of the element in the array Examples: Input: arr[] = {6, 3, 7, 2} Output: 2, 1, 1, 0 Explanation: Smaller elements after 6 = 2 [3, 2] Smaller elements after 3 = 1 [2] Smaller elements a 12 min read Sort a nearly sorted (or K sorted) arrayGiven an array arr[] and a number k . The array is sorted in a way that every element is at max k distance away from it sorted position. It means if we completely sort the array, then the index of the element can go from i - k to i + k where i is index in the given array. Our task is to completely s 6 min read Median of two Sorted Arrays of Different SizesGiven two sorted arrays, a[] and b[], the task is to find the median of these sorted arrays. Assume that the two sorted arrays are merged and then median is selected from the combined array.This is an extension of Median of two sorted arrays of equal size problem. Here we handle arrays of unequal si 15+ min read Merge k Sorted ArraysGiven K sorted arrays, merge them and print the sorted output.Examples:Input: K = 3, arr = { {1, 3, 5, 7}, {2, 4, 6, 8}, {0, 9, 10, 11}}Output: 0 1 2 3 4 5 6 7 8 9 10 11 Input: k = 4, arr = { {1}, {2, 4}, {3, 7, 9, 11}, {13} }Output: 1 2 3 4 7 9 11 13Table of ContentNaive - Concatenate all and SortU 15+ min read Merge K sorted arrays of different sizes | ( Divide and Conquer Approach )Given k sorted arrays of different length, merge them into a single array such that the merged array is also sorted.Examples: Input : {{3, 13}, {8, 10, 11} {9, 15}} Output : {3, 8, 9, 10, 11, 13, 15} Input : {{1, 5}, {2, 3, 4}} Output : {1, 2, 3, 4, 5} Let S be the total number of elements in all th 8 min read Merge K sorted linked listsGiven 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 Union and Intersection of two Linked List using Merge SortGiven two singly Linked Lists, create union and intersection lists that contain the union and intersection of the elements present in the given lists. Each of the two lists contains distinct node values.Note: The order of elements in output lists doesn't matter.Examples:Input: head1: 10 -> 15 - 15+ min read Sorting by combining Insertion Sort and Merge Sort algorithmsInsertion sort: The array is virtually split into a sorted and an unsorted part. Values from the unsorted part are picked and placed at the correct position in the sorted part.Advantages: Following are the advantages of insertion sort: If the size of the list to be sorted is small, insertion sort ru 2 min read Find array with k number of merge sort callsGiven two numbers n and k, find an array containing values in [1, n] and requires exactly k calls of recursive merge sort function. Examples: Input : n = 3 k = 3 Output : a[] = {2, 1, 3} Explanation: Here, a[] = {2, 1, 3} First of all, mergesort(0, 3) will be called, which then sets mid = 1 and call 6 min read Difference of two Linked Lists using Merge sortGiven two Linked List, the task is to create a Linked List to store the difference of Linked List 1 with Linked List 2, i.e. the elements present in List 1 but not in List 2.Examples: Input: List1: 10 -> 15 -> 4 ->20, List2: 8 -> 4 -> 2 -> 10 Output: 15 -> 20 Explanation: In the 14 min read Like