| 1 | // here we are merging k sorted lists |
| 2 | class 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 | } |
nothing calls this directly
no outgoing calls
no test coverage detected