Merge K sorted linked lists in Java


We are given a K number of linked lists of variable sizes which are sorted in their sequence and we have to merge the list into a resultant list in such a way that the resultant array is sorted in order and the resultant array is printed as the output to the user.

Let us understand with example:-

Input 

int k = 3;

list[0] = new Node(11);

list[0].next = new Node(15);

list[0].next.next = new Node(17);

list[1] = new Node(2);

list[1].next = new Node(3);

list[1].next.next = new Node(26);

list[1].next.next.next = new Node(39);

list[2] = new Node(4);

list[2].next = new Node(8);

list[2].next.next = new Node(10);

Output −The merged list is-->

2>> 3>> 4>> 8>> 10>> 11>> 15>> 17>> 26>> 39>> null

Explanation −We are given with K number of linked lists which are sorted in order. The merging process includes comparing the head of the linked list using java comparator function and merging them together in the resultant array.

Input 

int k = 2;

list[0] = new Node(1);

list[0].next = new Node(4);

list[0].next.next = new Node(5);

list[1] = new Node(2);

list[1].next = new Node(3);

list[1].next.next = new Node(6);

list[1].next.next.next = new Node(8);

Output − The merged list is-->

1>> 2>> 3>> 4>> 5>> 6>> 8>> null

Explanation −We are given with K number of linked lists which are sorted in order. The merging process includes comparing the head of the linked list using java comparator function and merging them together in the resultant array.

Approach used in the below program is as follows −

  • We are input with the number of list(K) that is required to be merged.

  • A node class is initialized that is responsible for creating nodes of a linked list.

  • After that the nodes of the linked list are initialized in a sorted order and the head of the linked list is passed through the function(mergeLists) with parameters as k

  • Inside the function a loop is iterated from the second list onwards inside the loop another loop is iterated which contains all the utility for the element’s comparison.

  • Head of both the first and the i’th list is captured and stored in the variables.

  • Both the heads are then checked for the smaller head element and the result and the resultant head is then set as the head of the final list.

  • Similar process is then done for the following elements of the list and the data are compared and stored according to their right order.

  • If the list is iterated to the end the last node is then set to null and the final list is returned to the user as output.

Example

import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
class Node {
   int data;
   Node next;
   public Node(int data) {
      this.data = data;
      this.next = null;
   }
}
public class testClass{
   public static Node mergeLists(Node[] list, int k) {
      PriorityQueue<Node> priorityQueue;
      priorityQueue = new PriorityQueue<Node>(Comparator.comparingInt(a ->((Node) a).data));
      priorityQueue.addAll(Arrays.asList(list).subList(0, k));
      Node head = null, last = null;
      while (!priorityQueue.isEmpty()) {
         Node min = priorityQueue.poll();
         if (head == null) {
            head = last = min;
         }
         else {
            last.next = min;
            last = min;
         }
         if (min.next != null) {
            priorityQueue.add(min.next);
         }
      }
      return head;
   }
   public static void main(String[] s) {
      int k = 3;
      Node[] list = new Node[k];
      list[0] = new Node(11);
      list[0].next = new Node(15);
      list[0].next.next = new Node(17);
      list[1] = new Node(2);
      list[1].next = new Node(3);
      list[1].next.next = new Node(26);
      list[1].next.next.next = new Node(39);
      list[2] = new Node(4);
      list[2].next = new Node(8);
      list[2].next.next = new Node(10);
      System.out.println("The merged list is-->");
      Node head = mergeLists(list, k);
      while (head != null) {
         System.out.print(head.data + ">> ");
         head = head.next;
      }
      System.out.print("null");
   }
}

Output

If we run the above code it will generate the following Output

The merged list is-->
2>> 3>> 4>> 8>> 10>> 11>> 15>> 17>> 26>> 39>> null

Updated on: 05-Nov-2021

586 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements