Leecode23
使用优先级队列和分治法解决
class Solution {
public static void main(String[] args) {
Solution s = new Solution();
ListNode l11 = new ListNode(1);
ListNode l12 = new ListNode(4);
ListNode l13 = new ListNode(5);
l11.next = l12;
l12.next = l13;
l13.next = null;
ListNode l21 = new ListNode(1);
ListNode l22 = new ListNode(3);
ListNode l23 = new ListNode(4);
l21.next = l22;
l22.next = l23;
l23.next = null;
ListNode l31 = new ListNode(2);
ListNode l32 = new ListNode(2);
l31.next = l32;
l32.next = null;
ListNode[] arr = {l11, l21, l31};
ListNode head = s.mergeKLists2(arr);
s.print(l11);
}
public void print(ListNode head) {
ListNode p = head;
while (p != null) {
System.out.print(p.val + "->");
p = p.next;
}
System.out.println("null");
}
//使用优先队列
public 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 (ListNode list : lists) {
if (list != null)
queue.add(list);
}
ListNode newHead = new ListNode(-1);
ListNode p = newHead;
while (!queue.isEmpty()) {
p.next = queue.poll();
p = p.next;
if (p.next != null) {
queue.add(p.next);
}
}
return newHead.next;
}
/*
使用分治法,分成两两不同的小组进行合并
*/
public ListNode mergeKLists2(ListNode[] lists) {
if (lists == null || lists.length == 0)
return null;
return merge(lists, 0, lists.length - 1);
}
private ListNode merge(ListNode[] lists, int left, int right) {
//循环先写终止条件
if (left == right)
return lists[left];
int mid = left + (right - left) / 2;
ListNode l1 = merge(lists, left, mid);
ListNode l2 = merge(lists, mid + 1, right);
return mergeTwoLists(l1, l2);
}
private ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null)
return l2;
if (l2 == null)
return l1;
//if...else...必然返回所以不报错
if (l1.val < l2.val) {
l1.next = mergeTwoLists(l1.next, l2);
return l1;
} else {
l2.next = mergeTwoLists(l1, l2.next);
return l2;
}
}
}
class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}