|
12 | 12 | * Analyze and describe its complexity.*/
|
13 | 13 |
|
14 | 14 | publicclass_23 {
|
15 |
| - |
16 |
| -publicListNodemergeKLists(ListNode[]lists) { |
17 |
| -PriorityQueue<ListNode>heap =newPriorityQueue((Comparator<ListNode>) (o1,o2) ->o1.val -o2.val); |
18 |
| - |
19 |
| -for (ListNodenode :lists) { |
20 |
| -if (node !=null) { |
21 |
| -heap.offer(node); |
| 15 | +publicstaticclassSolution1 { |
| 16 | +publicListNodemergeKLists(ListNode[]lists) { |
| 17 | +PriorityQueue<ListNode>heap =newPriorityQueue((Comparator<ListNode>) (o1,o2) ->o1.val -o2.val); |
| 18 | + |
| 19 | +for (ListNodenode :lists) { |
| 20 | +if (node !=null) { |
| 21 | +heap.offer(node); |
| 22 | + } |
22 | 23 | }
|
23 |
| - } |
24 | 24 |
|
25 |
| -ListNodepre =newListNode(-1); |
26 |
| -ListNodetemp =pre; |
27 |
| -while (!heap.isEmpty()) { |
28 |
| -ListNodecurr =heap.poll(); |
29 |
| -temp.next =newListNode(curr.val); |
30 |
| -if (curr.next !=null) { |
31 |
| -heap.offer(curr.next); |
| 25 | +ListNodepre =newListNode(-1); |
| 26 | +ListNodetemp =pre; |
| 27 | +while (!heap.isEmpty()) { |
| 28 | +ListNodecurr =heap.poll(); |
| 29 | +temp.next =newListNode(curr.val); |
| 30 | +if (curr.next !=null) { |
| 31 | +heap.offer(curr.next); |
| 32 | + } |
| 33 | +temp =temp.next; |
32 | 34 | }
|
33 |
| -temp =temp.next; |
| 35 | +returnpre.next; |
34 | 36 | }
|
35 |
| -returnpre.next; |
36 | 37 | }
|
37 | 38 |
|
38 | 39 | }
|