We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 388e17d commit a409297Copy full SHA for a409297
1 file changed
merge-k-sorted-lists/se6816.java
@@ -0,0 +1,28 @@
1
+
2
+class Solution {
3
+ public ListNode mergeKLists(ListNode[] lists) {
4
+ ListNode list= new ListNode(0); // head
5
+ ListNode curr=list;
6
+ PriorityQueue<ListNode> pq=new PriorityQueue<ListNode>((l1, l2)->{
7
+ return l1.val - l2.val;
8
+ });
9
+ for(int i=0; i<lists.length;i++){
10
+ if(lists[i]!=null){
11
+ pq.offer(lists[i]);
12
+ }
13
14
+ while(!pq.isEmpty()){
15
+ ListNode temp = pq.poll();
16
+ curr.next=new ListNode(temp.val);
17
+ curr=curr.next;
18
+ temp=temp.next;
19
+ if(temp!=null){
20
+ pq.offer(temp);
21
22
23
24
+ return list.next;
25
26
27
28
+}
0 commit comments