File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed
Leetcode/leetcodeTags/LinkedList Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change 1+ package LinkedList ;
2+
3+ import java .util .PriorityQueue ;
4+
5+ public class MergeKSortedLists23 {
6+
7+ public class ListNode {
8+ int val ;
9+ ListNode next ;
10+
11+ ListNode () {
12+ }
13+
14+ ListNode (int val ) {
15+ this .val = val ;
16+ }
17+
18+ ListNode (int val , ListNode next ) {
19+ this .val = val ;
20+ this .next = next ;
21+ }
22+ }
23+
24+ public ListNode mergeKLists (ListNode [] lists ) {
25+
26+ PriorityQueue <Integer > minHeap = new PriorityQueue <>();
27+
28+ for (ListNode head : lists ) {
29+
30+ while (head != null ) {
31+ minHeap .add (head .val );
32+ head = head .next ;
33+ }
34+ }
35+
36+ ListNode dummy = new ListNode (-1 );
37+ ListNode cursor = dummy ;
38+
39+ while (!minHeap .isEmpty ()) {
40+ cursor .next = new ListNode (minHeap .remove ());
41+ cursor = cursor .next ;
42+ }
43+
44+ return dummy .next ;
45+ }
46+
47+ }
You can’t perform that action at this time.
0 commit comments