Java Program To Merge K Sorted Linked Lists – Set 1
Given K sorted linked lists of size N each, merge them and print the sorted output.
Examples:Â
Input: k = 3, n = 4
list1 = 1->3->5->7->NULL
list2 = 2->4->6->8->NULL
list3 = 0->9->10->11->NULL
Output: 0->1->2->3->4->5->6->7->8->9->10->11
Merged lists in a sorted order
where every element is greater
than the previous element.
Input: k = 3, n = 3
list1 = 1->3->7->NULL
list2 = 2->4->8->NULL
list3 = 9->10->11->NULL
Output: 1->2->3->4->7->8->9->10->11
Merged lists in a sorted order
where every element is greater
than the previous element.
Method 1 (Simple):
Approach:Â
A Simple Solution is to initialize the result as the first list. Now traverse all lists starting from the second list. Insert every node of the currently traversed list into result in a sorted way. Â
Java
import java.io.*;
class Node
{
int data;
Node next;
Node( int key)
{
data = key;
next = null ;
}
}
class GFG
{
static Node head;
static Node temp;
static void printList(Node node)
{
while (node != null )
{
System.out.print(node.data + " " );
node = node.next;
}
System.out.println();
}
static Node mergeKLists(Node arr[],
int last)
{
for ( int i = 1 ; i <= last; i++)
{
while ( true )
{
Node head_0 = arr[ 0 ];
Node head_i = arr[i];
if (head_i == null )
break ;
if (head_0.data >= head_i.data)
{
arr[i] = head_i.next;
head_i.next = head_0;
arr[ 0 ] = head_i;
}
else
{
while (head_0.next != null )
{
if (head_0.next.data >= head_i.data)
{
arr[i] = head_i.next;
head_i.next = head_0.next;
head_0.next = head_i;
break ;
}
head_0 = head_0.next;
if (head_0.next == null )
{
arr[i] = head_i.next;
head_i.next = null ;
head_0.next = head_i;
head_0.next.next = null ;
break ;
}
}
}
}
}
return arr[ 0 ];
}
public static void main (String[] args)
{
int k = 3 ;
int n = 4 ;
Node[] arr = new Node[k];
arr[ 0 ] = new Node( 1 );
arr[ 0 ].next = new Node( 3 );
arr[ 0 ].next.next = new Node( 5 );
arr[ 0 ].next.next.next = new Node( 7 );
arr[ 1 ] = new Node( 2 );
arr[ 1 ].next = new Node( 4 );
arr[ 1 ].next.next = new Node( 6 );
arr[ 1 ].next.next.next = new Node( 8 );
arr[ 2 ] = new Node( 0 );
arr[ 2 ].next = new Node( 9 );
arr[ 2 ].next.next = new Node( 10 );
arr[ 2 ].next.next.next = new Node( 11 );
head = mergeKLists(arr, k - 1 );
printList(head);
}
}
|
Output:
0 1 2 3 4 5 6 7 8 9 10 11
Complexity Analysis:Â
- Time complexity: O(nk2)
- Auxiliary Space: O(1).Â
As no extra space is required.
Method 2: Min Heap.Â
A Better solution is to use Min Heap-based solution which is discussed here for arrays. The time complexity of this solution would be O(nk Log k)
Method 3: Divide and Conquer.Â
In this post, Divide and Conquer approach is discussed. This approach doesn’t require extra space for heap and works in O(nk Log k)
It is known that merging of two linked lists can be done in O(n) time and O(n) space.Â
- The idea is to pair up K lists and merge each pair in linear time using O(n) space.
- After the first cycle, K/2 lists are left each of size 2*N. After the second cycle, K/4 lists are left each of size 4*N and so on.
- Repeat the procedure until we have only one list left.
Below is the implementation of the above idea.Â
Java
public class MergeKSortedLists
{
public static Node SortedMerge(Node a, Node b)
{
Node result = null ;
if (a == null )
return b;
else if (b == null )
return a;
if (a.data <= b.data)
{
result = a;
result.next = SortedMerge(a.next, b);
}
else
{
result = b;
result.next = SortedMerge(a, b.next);
}
return result;
}
public static Node mergeKLists(Node arr[],
int last)
{
while (last != 0 )
{
int i = 0 , j = last;
while (i < j)
{
arr[i] = SortedMerge(arr[i], arr[j]);
i++;
j--;
if (i >= j)
last = j;
}
}
return arr[ 0 ];
}
public static void printList(Node node)
{
while (node != null )
{
System.out.print(node.data + " " );
node = node.next;
}
}
public static void main(String args[])
{
int k = 3 ;
int n = 4 ;
Node arr[] = new Node[k];
arr[ 0 ] = new Node( 1 );
arr[ 0 ].next = new Node( 3 );
arr[ 0 ].next.next = new Node( 5 );
arr[ 0 ].next.next.next = new Node( 7 );
arr[ 1 ] = new Node( 2 );
arr[ 1 ].next = new Node( 4 );
arr[ 1 ].next.next = new Node( 6 );
arr[ 1 ].next.next.next = new Node( 8 );
arr[ 2 ] = new Node( 0 );
arr[ 2 ].next = new Node( 9 );
arr[ 2 ].next.next = new Node( 10 );
arr[ 2 ].next.next.next = new Node( 11 );
Node head = mergeKLists(arr, k - 1 );
printList(head);
}
}
class Node
{
int data;
Node next;
Node( int data)
{
this .data = data;
}
}
|
Output:
0 1 2 3 4 5 6 7 8 9 10 11
Complexity Analysis:Â
Assuming N(n*k) is the total number of nodes, n is the size of each linked list, and k is the total number of linked lists.
- Time Complexity: O(N*log k) or O(n*k*log k)
As outer while loop in function mergeKLists() runs log k times and every time it processes n*k elements.
- Auxiliary Space: O(N) or O(n*k)
Because recursion is used in SortedMerge() and to merge the final 2 linked lists of size N/2, N recursive calls will be made.
Please refer complete article on Merge K sorted linked lists | Set 1 for more details!
Last Updated :
03 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...