# leetcode刷题（80）—— 23. 合并K个排序链表

输入:
[
1->4->5,
1->3->4,
2->6
]



class Solution {
public static ListNode mergeKLists(ListNode[] lists) {
if (lists == null || lists.length == 0) return null;
PriorityQueue<ListNode> queue = new PriorityQueue<>(lists.length, new Comparator<ListNode>() {
@Override
public int compare(ListNode o1, ListNode o2) {
return o1.val - o2.val;
}
});
for (int i = 0; i < lists.length; i++) {
if (lists[i] != null) {
}
}
ListNode begin = new ListNode(-1);
ListNode temp = begin;
while (!queue.isEmpty()) {
ListNode listNode = queue.poll();
begin.next = listNode;
begin = begin.next;
if (begin.next != null) {
}
}
return temp.next;
}
}



//方法2 使用分治
public static ListNode mergeKLists(ListNode[] lists) {
if (lists == null || lists.length == 0) return null;
return merge(lists, 0, lists.length - 1);
}

public static ListNode merge(ListNode[] lists, int left, int right) {
if (left == right) return lists[left];
int middle = left + (right - left) / 2;
ListNode l1 = merge(lists, left, middle);
ListNode l2 = merge(lists, middle + 1, right);
return mergeTwoLists(l1, l2);
}

public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null) return l2;
if (l2 == null) return l1;
if (l1.val < l2.val) {
l1.next = mergeTwoLists(l1.next, l2);
return l1;
} else {
l2.next = mergeTwoLists(l2.next, l1);
return l2;
}
}


05-25 50

05-30 45

12-04 1753

07-14 28

04-26 50

03-18 31

04-09 48

10-25 296

05-12 62

06-12 46

#### 刷题---链表篇---23. 合并K个排序链表

©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。