Merge K sorted linked lists | Set 1

Introduction

The linked list is one of the most important concepts to know while preparing for interviews. Having a good grasp of a linked list can be a huge plus point in coding interviews.

Problem Statement

In this problem, we will be given K sorted linked lists. We need to merge all lists such that the newly created list is also in sorted order.

Problem Statement Understanding

The problem statement is quite straightforward, we will be given K linked lists that are sorted in nature, and then we need to form a linked list using the nodes of all the given linked lists such that the newly formed list is sorted in order.

Let's try to understand the problem with the help of examples:

If the sorted lists given to us are 2→3→5→NULL, 1→4→8→NULL, and 6→7→9→NULL.

  • According to the problem statement, we need to merge all these given linked lists into a single linked list in such a way that after merging, the final linked list is sorted in nature.
  • The list that we need to return must contain all the nodes of all three given lists in sorted order.
  • So, after merging, the newly formed sorted list will be 1→2→3→4→5→6→7→8→9→NULL

Let’s take another example:
If the sorted lists given to us are 2→8→9→10→NULL, 11→13→17→NULL.

  • In this case, after merging all the given lists, the final resultant sorted linked list will be 2→8→9→10→11→13→17→NULL

At this point, we have understood the problem statement. Now we will try to formulate an approach for this problem.

Before moving to the approach section, try to think about how you can approach this problem.

  • If stuck, no problem, we will thoroughly see how we can approach this problem in the next section.

Let’s move to the approach section.

Approach 1

  • Initialize a result list with the first list.
  • Traverse all K lists one by one, starting from the second list, and merge them with the initialized result list using the merge two sorted list concepts.
  • At last, when we have traversed all the K lists, our result list will be containing all the nodes of the K lists in sorted order.
  • Finally, we can return this result list as our output.

Note: In case if you are not aware of how to merge two sorted linked lists, please check this article.

Some Important Observations

  • Every time we add a sorted list into our result list, its size increases by N (Complexity wise in the worst case we will take N as the size of the longest list out of all K lists).
  • So, when we add the second list, the complexity is 2N. Then, on adding the third list, the complexity is 3N.
  • Similarly, when we are adding the Kth list, the complexity is *KN**.
  • So, the total complexity is (2N + 3N + 4N + .... + KN) = N (2+3+4+...+K), which is asymptotically equal to (NK2).

Time Complexity: *(NK2), N is the number of nodes in the list, and K is the total number of lists
Space Complexity:** O(1)

We can observe that if the number of lists given is quite large, then the above approach will result in TLE.

  • So, we need to reduce the time complexity, which is possible if we use the divide and conquer technique.

Approach 2

  • In this approach, we merge the linked lists in pairs.
  • In the first iteration, we will have K/2 pairs so; we will merge them using the merge two sorted lists concept.
  • Then after the first iteration, we will have K/2 lists, i.e., K/4 pairs of lists to merge.
  • After each iteration, the number of lists to be merged will reduce by half of their previous count.
  • At last, we will be left with a single list which will contain all lists merged in sorted order.

Let's move to the algorithm section.

Algorithm

  • Initialize a variable back with the last array index (this array is containing the head of all the lists).
  • Run a while loop till back is not equal to 0.
  • Inside while loop, we need to merge pairs so, initialize two variables, i.e., i with 0 and j with back.
  • Now, we need to run a while loop as long as i is less than j.
  • Now merge ith and jth list and store it as ith element of the array.
  • Now, increment i by one and decrement j by one.
  • If i is greater than or equal to j, we need to update back, by j.
  • After execution of both the while loops, we need to return the first element of the array.

Dry Run


Code Implementation

#include
using namespace std;

// class with constructor for a node of Linked list
class Node {
    public:
    int data;
    Node* next;
    // constructor
    Node(int x){
        data = x;
        next = NULL;
}
};
// This function prints data of the linked list
void print(Node* head)
{
    Node* curr = head;
    while (curr != NULL) {
        cout << curr->data << " -> ";
        curr = curr->next;
    }
    cout<<"NULL";
}

// This function merges two sorted linked lists into one sorted
// list
Node *mergeTwoLists(Node *l1, Node *l2) {
        Node dummy(INT_MIN);
        Node *tail = &dummy;
        
        while (l1 && l2) {
            if (l1->data < l2->data) {
                tail->next = l1;
                l1 = l1->next;
            } else {
                tail->next = l2;
                l2 = l2->next;
            }
            tail = tail->next;
        }

        tail->next = l1 ? l1 : l2;
        return dummy.next;
    }


// This function merges 'K' sorted lists into one sorted list
Node* mergeKLists(Node* arr[], int back)
{
    // run the while loop till 'back' is not equal to
    while (back != 0) {
        int i = 0, j = back;
 
        while (i < j) {
            // merge ith and jth list and store the list at
            // ith index of array
            arr[i] = mergeTwoLists(arr[i], arr[j]);
 
            // increment 'i' and decrement 'j' by one
            i++, j--;
 
            // update 'back' by 'j' once j is less than or
            // equal to 'i'
            if (i >= j)
                back = j;
        }
    }
 
    return arr[0];
}

// main function
int main()
{
    Node * arr[3];
    Node *head = new Node(2);
    head->next = new Node(3);
    head->next->next = new Node(5);
    Node *h2 = new Node(1);
    h2->next = new Node(4);
    h2->next->next = new Node(8);

    Node *h3 = new Node(6);
    h3->next = new Node(7);
    h3->next->next = new Node(9);

    arr[0] = head;
    arr[1] = h2;
    arr[2] = h3;
    cout<<"Final resultant merged linked list"<

						 

Output

Final resultant merged linked list
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> NULL

Time Complexity: O(NKlog(K)), N is the number of nodes in the list, and K is the total number of lists

There is also a min heap based solution to this problem, to check out the heap based solution please visit [Merge K sorted linked lists using min heap](Merge k sorted linked lists Set 2 Using Min Heap).

So, in this blog, we have tried to explain how you can merge K sorted linked lists in the most optimal way. If you want to practice more questions on linked lists, feel free to solve them at Prepbytes Linked List.

Previous post Merge k sorted linked lists | Set 2 (Using Min Heap)
Next post Deletion in Circular Linked List

Leave a Reply

Your email address will not be published. Required fields are marked *