MCPcopy Create free account
hub / github.com/Vishruth-S/CompetitiveCode / Solution

Class Solution

LeetCode_problems/merge k sorted list/Solution.java:2–24  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1// here we are merging k sorted lists
2class Solution {
3 public ListNode mergeKLists(ListNode[] lists) {
4 PriorityQueue<Integer> minHeap = new PriorityQueue<>();
5 //we are using min heap
6 //we will fill heap with k sorted list
7 //what min heap will do (it will arrange number form low to high automatically)
8 for(ListNode head:lists){
9 while(head!=null){
10 minHeap.add(head.val);
11 head=head.next;
12 }
13 }
14 //we make one single list form heap
15 ListNode dummy = new ListNode(-1);
16 ListNode head= dummy;
17 while(!minHeap.isEmpty()){
18 head.next = new ListNode(minHeap.remove());
19 head=head.next;
20 }
21
22 return dummy.next;
23 }
24}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected