Javatpoint Logo
Javatpoint Logo

Merge K Sorted Lists

Problem Statement:

In this statement we have a List of Linked Lists where each and every linked list is sorted in ascending order, You need to merge the linked lists in such a way that the obtained list should be in non-decreasing order (ascending order)

Sample Test Cases:

Note: Before getting into the article, try coming up with an approach first for having better understanding.

Method 1: Brute Force Approach

A straightforward approach involves initializing the result with the first list and then iterating through the remaining lists. For each node in the current list, insert it into the result in a sorted manner.

Below is the Java Implementation of the above mentioned approach

public class MergeKSortedLists { // Node class for linked list static class ListNode { int data; ListNode next; public ListNode(int data) { this.data = data; this.next = null; } } static void printList(ListNode node) { while (node != null) { System.out.print(node.data + " "); node = node.next; } } // Main function that takes an array of lists and generates the sorted output static ListNode mergeKLists(ListNode[] lists, int last) { for (int i = 1; i <= last; i++) { while (true) { ListNode head0 = lists[0], headI = lists[i]; if (headI == null) break; if (head0.data >= headI.data) { lists[i] = headI.next; headI.next = head0; lists[0] = headI; } else { while (head0.next != null) { if (head0.next.data >= headI.data) { lists[i] = headI.next; headI.next = head0.next; head0.next = headI; break; } head0 = head0.next; if (head0.next == null) { lists[i] = headI.next; headI.next = null; head0.next = headI; head0.next.next = null; break; } } } } } return lists[0]; } // Utility function to create a new node static ListNode newNode(int data) { return new ListNode(data); } // Driver program to test above functions public static void main(String[] args) { // Number of linked lists int k = 3; // Number of elements in each list int n = 4; // Array of pointers storing the head nodes of the linked lists ListNode[] lists = new ListNode[k]; lists[0] = newNode(1); lists[0].next = newNode(3); lists[0].next.next = newNode(5); lists[0].next.next.next = newNode(7); lists[1] = newNode(2); lists[1].next = newNode(4); lists[1].next.next = newNode(6); lists[1].next.next.next = newNode(8); lists[2] = newNode(0); lists[2].next = newNode(9); lists[2].next.next = newNode(10); lists[2].next.next.next = newNode(11); // Merge all lists ListNode head = mergeKLists(lists, k - 1); // Print the merged list printList(head); } }

Output:

Merge K Sorted Lists

Complexity Analysis:

Time complexity: O(n^k-1), because we are traversing n times on each of the K lists.

Space Complexity: O(1).

Method 2: Using Min Heap:

This solution employs the Min Heap approach. Initially, a Min Heap is constructed by inserting the first element from each of the K Linked Lists. The algorithm proceeds by extracting the root element from the Min Heap and appending it to the output Linked List. Subsequently, the next element from the Linked List associated with the extracted element is inserted into the Min Heap. This process iterates until there are no elements remaining in the Min Heap, ultimately yielding the desired result.

Algorithm:

  1. Create a Min Heap to store elements from the K linked lists along with their list index.
  2. Insert the first element from each linked list into the Min Heap with its list index.
  3. Initialize an empty result linked list.
  4. Repeat the following steps until the Min Heap is not empty:
    • Extract the root element from the Min Heap, which contains the minimum value along with its list index.
    • Append the extracted element to the result linked list.
    • If there is a next element in the linked list associated with the extracted element, insert that element into the Min Heap with its list index.
  5. Return the head of the result linked list.

Note: The Min Heap ensures that the smallest element among the remaining candidates is always at the root, facilitating the construction of the sorted merged list. The algorithm terminates when there are no more elements in the Min Heap, indicating that all linked lists have been processed.

Java Implementation of the above mentioned approach

Explanation:

Output:

Merge K Sorted Lists

Complexity Analysis:

Time Complexity: O(n*k*logk)

Space Complexity: O(k)

Method 3: Using Divide & Conquer Approach:

The approach involves iteratively pairing up sorted lists, halving the total number of lists at each iteration, until all the lists are merged.

Follow these steps to solve the problem:

  1. Combine K lists by merging each pair efficiently with linear time complexity and utilizing O(N) space.
  2. After the initial cycle, there will be K/2 lists, each with a size of 2N. Subsequently, in the second cycle, there will be K/4 lists, each with a size of 4N, and so forth.
  3. Continue this process iteratively until only one list remains.

Java implementation of the above mentioned approach

Output:

Merge K Sorted Lists

Complexity Analysis:

Time Complexity: O(n*k^2)

Space Complexity: O(n)







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA




news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news