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) { = data; = null; } } static void printList(ListNode node) { while (node != null) { System.out.print( + " "); node =; } } // 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 ( >= { lists[i] =; = head0; lists[0] = headI; } else { while ( != null) { if ( >= { lists[i] =; =; = headI; break; } head0 =; if ( == null) { lists[i] =; = null; = headI; = 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] = newNode(5); lists[0] = newNode(7); lists[1] = newNode(2); lists[1].next = newNode(4); lists[1] = newNode(6); lists[1] = newNode(8); lists[2] = newNode(0); lists[2].next = newNode(9); lists[2] = newNode(10); lists[2] = newNode(11); // Merge all lists ListNode head = mergeKLists(lists, k - 1); // Print the merged list printList(head); } }


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.


  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



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


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


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA