| 44 | // } |
| 45 | |
| 46 | public ListNode mergeKLists(ListNode[] lists) { |
| 47 | if (lists.length == 0) return null; |
| 48 | PriorityQueue<ListNode> queue = new PriorityQueue<>(lists.length, new Comparator<ListNode>() { |
| 49 | @Override |
| 50 | public int compare(ListNode o1, ListNode o2) { |
| 51 | if (o1.val < o2.val) return -1; |
| 52 | else if (o1.val == o2.val) return 0; |
| 53 | else return 1; |
| 54 | } |
| 55 | }); |
| 56 | ListNode node = new ListNode(0), tmp = node; |
| 57 | for (ListNode l : lists) { |
| 58 | if (l != null) queue.add(l); |
| 59 | } |
| 60 | while (!queue.isEmpty()) { |
| 61 | tmp.next = queue.poll(); |
| 62 | tmp = tmp.next; |
| 63 | if (tmp.next != null) queue.add(tmp.next); |
| 64 | } |
| 65 | return node.next; |
| 66 | } |
| 67 | |
| 68 | public static void main(String[] args) { |
| 69 | Solution solution = new Solution(); |