75. Merge k Sorted Lists
Topic :
priority queue
Difficulty :
hard
Problem Link :
problem statement
You are given an array of k
linked-lists lists
, each linked-list is sorted in ascending order.
Merge all the linked-lists into one sorted linked-list and return it.
Example 1:
Input: lists = [[1,4,5],[1,3,4],[2,6]]
Output: [1,1,2,3,4,4,5,6]
Explanation: The linked-lists are:
[
1->4->5,
1->3->4,
2->6
]
merging them into one sorted list:
1->1->2->3->4->4->5->6
Example 2:
Input: lists = []
Output: []
Example 3:
Input: lists = [[]]
Output: []
Constraints:
k == lists.length
0 <= k <= 104
0 <= lists[i].length <= 500
-104 <= lists[i][j] <= 104
lists[i]
is sorted in ascending order.- The sum of
lists[i].length
will not exceed104
.
solution
import java.io.*;
import java.util.*;
class Merge_K_sorted_Lists
{
static class ListNode
{
int val;
ListNode next;
ListNode(int key)
{ val = key;
next = null;
}
}
static ListNode mergeKLists(ListNode[] lists) {
// create a mean heap so we get the node with least value
PriorityQueue<ListNode> pq=new PriorityQueue<>((a,b)->(a.val-b.val));
// add the head of the k lists in the priority queue
for(ListNode node:lists)
{
if(node!=null) pq.offer(node);
}
if(pq.isEmpty()) return null;
ListNode head=new ListNode(0);
ListNode last=head;
while(!pq.isEmpty())
{
ListNode curr=pq.poll();// take out the node with least val
last.next=curr; // add it to our ans list
last=last.next;
// if curr has more nodes attached to it then add curr,next to our priority queue
if(curr.next!=null) pq.offer(curr.next);
}
return head.next; // return head of the merged list
}
public static void main(String args[])
{ ListNode head1=new ListNode(1);
head1.next=new ListNode(3);
ListNode head2=new ListNode(4);
head2.next=new ListNode(5);
head2.next.next=new ListNode(6);
ListNode head3=new ListNode(8);
ListNode[] arr=new ListNode[3];
arr[0]=head1;
arr[1]=head2;
arr[2]=head3;
ListNode ans=mergeKLists(arr);
while(ans!=null)
{
System.out.print(ans.val+"-->");
ans=ans.next;
}
System.out.print("null");
}
}