
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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
- Related Articles
- Merge k Sorted Lists in Python
- Merge two sorted linked lists using C++.
- Program to merge K-sorted lists in Python
- Merge k sorted arrays in Java
- Merge Two Sorted Lists in Python
- Merge Sort for Linked Lists using C++.
- Program to merge in between linked lists in Python
- Merge k sorted arrays of different sizes in C++
- Merge two sorted arrays in Java
- Java Program to Merge two lists
- JavaScript Program for Finding Intersection of Two Sorted Linked Lists
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes in Python
- Merge a linked list into another linked list at alternate positions in Java
- Merge Sorted Array in Python
- How to create an array of linked lists in java?
