-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
33 lines (28 loc) · 808 Bytes
/
Solution.java
File metadata and controls
33 lines (28 loc) · 808 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
@lc id : 23
@problem : Merge k Sorted Lists
@author : rohit
@date : 09/07/2020
@url : https://leetcode.com/problems/merge-k-sorted-lists/
*/
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
ListNode res = new ListNode(0);
ListNode curr = res;
//Push all elementos into pq of all lists
for(ListNode list : lists){
while(list != null){
pq.add(list.val);
list = list.next;
}
}
//Remove from pq and add to list
while(pq.size() > 0){
ListNode node = new ListNode(pq.remove());
curr.next = node;
curr = curr.next;
}
return res.next;
}
}