合并 k 个排序链表,返回合并后的排序链表。请分析和描述算法的复杂度。
示例:
代码语言:javascript复制输入:
[
1->4->5,
1->3->4,
2->6
]
输出: 1->1->2->3->4->4->5->6
解:分治算法。
代码语言:javascript复制/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
private ListNode merge(ListNode l1, ListNode l2) {
ListNode l = new ListNode(0);
ListNode p = l;
while (l1 != null && l2 != null) {
if (l1.val < l2.val) {
p.next = l1;
l1 = l1.next;
} else {
p.next = l2;
l2 = l2.next;
}
p = p.next;
}
if (l1 != null)
p.next = l1;
if (l2 != null)
p.next = l2;
return l.next;
}
public ListNode mergeKLists(ListNode[] lists) {
return partion(lists, 0, lists.length - 1);
}
private ListNode partion(ListNode[] lists, int s, int e) {
if (s == e) {
return lists[s];
}
if (s < e) {
int q = (s e) / 2;
ListNode l1 = partion(lists, s, q);
ListNode l2 = partion(lists, q 1, e);
return merge(l1, l2);
} else
return null;
}
private ListNode merge(ListNode l1, ListNode l2) {
ListNode l = new ListNode(0);
ListNode p = l;
while (l1 != null && l2 != null) {
if (l1.val < l2.val) {
p.next = l1;
l1 = l1.next;
} else {
p.next = l2;
l2 = l2.next;
}
p = p.next;
}
if (l1 != null)
p.next = l1;
if (l2 != null)
p.next = l2;
return l.next;
}
}