合并k个排序链表,并且返回合并后的排序链表。尝试分析和描述其复杂度。
样例
给出3个排序链表[2->4->null,null,-1->null],返回 -1->2->4->null
代码
1.分治 + 归并排序的思想,NlogK (合并两个链表时间复杂度为 N ,对链表组分治时间复杂度为 logK),自顶向下分治一层层递归
/**
* Definition for ListNode.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int val) {
* this.val = val;
* this.next = null;
* }
* }
*/
public class Solution {
/**
* @param lists: a list of ListNode
* @return: The head of one sorted list.
*/
public ListNode mergeKLists(List<ListNode> lists) {
if (lists.size() == 0) {
return null;
}
return mergeHelper(lists, 0, lists.size() - 1);
}
private ListNode mergeHelper(List<ListNode> lists, int start, int end) {
// 只有一条链表,返回链表头结点
if (start == end) {
return lists.get(start);
}
int mid = start + (end - start) / 2;
ListNode left = mergeHelper(lists, start, mid);
ListNode right = mergeHelper(lists, mid + 1, end);
return mergeTwoLists(left, right);
}
private ListNode mergeTwoLists(ListNode list1, ListNode list2) {
ListNode dummy = new ListNode(0);
ListNode tail = dummy;
while (list1 != null && list2 != null) {
if (list1.val < list2.val) {
tail.next = list1;
tail = list1;
list1 = list1.next;
} else {
tail.next = list2;
tail = list2;
list2 = list2.next;
}
}
if (list1 != null) {
tail.next = list1;
} else {
tail.next = list2;
}
return dummy.next;
}
}
- heap 时间复杂度NlogK
PriorityQueue
PriorityQueue是基于优先堆的一个无界队列,本质是一个堆,这个优先队列中的元素可以默认自然排序或者通过提供的Comparator(比较器)在队列实例化的时排序。Java默认从小到大顺序,Cpp默认从大到小顺序。
public class Solution {
// 自定义Comparator,Comparator方法第一个参数减去第二个参数就是从小到大排序
private Comparator<ListNode> ListNodeComparator = new Comparator<ListNode>() {
public int compare(ListNode left, ListNode right) {
return left.val - right.val;
}
};
public ListNode mergeKLists(List<ListNode> lists) {
if (lists == null || lists.size() == 0) {
return null;
}
// 可直接确定PriortyQueue的大小为lists.size()
// 用最小堆实现PriorityQueue
Queue<ListNode> heap = new PriorityQueue<ListNode>(lists.size(), ListNodeComparator);
// K个链表的表头加入heap
for (int i = 0; i < lists.size(); i++) {
if (lists.get(i) != null) {
heap.add(lists.get(i));
}
}
ListNode dummy = new ListNode(0);
ListNode tail = dummy;
// while执行N次,N是所有结点个数
// 此处是本算法的精髓
while (!heap.isEmpty()) {
// head即为堆的弹出当前最小值结点
ListNode head = heap.poll();
tail.next = head;
tail = head;
// head结点所在链表在head后仍有结点,加入heap,和其余结点比较
if (head.next != null) {
heap.add(head.next);
}
}
return dummy.next;
}
}
- merge two by two 时间复杂度NlogK,自底向上归并
/**
* Definition for ListNode.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int val) {
* this.val = val;
* this.next = null;
* }
* }
*/
public class Solution {
/**
* @param lists: a list of ListNode
* @return: The head of one sorted list.
*/
public ListNode mergeKLists(List<ListNode> lists) {
if (lists == null || lists.size() == 0) {
return null;
}
while (lists.size() > 1) {
List<ListNode> new_lists = new ArrayList<ListNode>();
// 每两条链表相互合并,合并后链表加入new_lists
// 合并完一轮后list = new_lists,继续两两合并,直到只剩一个链表
for (int i = 0; i + 1 < lists.size(); i += 2) {
ListNode merged_list = merge(lists.get(i), lists.get(i+1));
new_lists.add(merged_list);
}
// 某一轮合并链表个数为奇数时,最后一个链表直接加入new_lists
if (lists.size() % 2 == 1) {
new_lists.add(lists.get(lists.size() - 1));
}
lists = new_lists;
}
return lists.get(0);
}
// 合并两个链表
private ListNode merge(ListNode a, ListNode b) {
ListNode dummy = new ListNode(0);
ListNode tail = dummy;
while (a != null && b != null) {
if (a.val < b.val) {
tail.next = a;
a = a.next;
} else {
tail.next = b;
b = b.next;
}
tail = tail.next;
}
if (a != null) {
tail.next = a;
} else {
tail.next = b;
}
return dummy.next;
}
}