Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 26e9e46

Browse files
authored
Merge pull request neetcode-gh#245 from FelixRodriguezJr/patch-6
Create 23-Merge-k-Sorted-Lists.java
2 parents e9c18d4 + 9f8a644 commit 26e9e46

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

java/23-Merge-k-Sorted-Lists.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public ListNode mergeKLists(ListNode[] lists) {
3+
4+
Queue<Integer> minHeap = new PriorityQueue<>();
5+
6+
for(ListNode nodes : lists){
7+
ListNode current = nodes;
8+
while(current != null){
9+
minHeap.add(current.val);
10+
current = current.next;
11+
}
12+
}
13+
14+
ListNode dummy = new ListNode(0);
15+
ListNode current = dummy;
16+
17+
while(!minHeap.isEmpty()){
18+
current.next = new ListNode(minHeap.poll());
19+
current = current.next;
20+
}
21+
22+
return dummy.next;
23+
}
24+
}

0 commit comments

Comments
 (0)