合并K個排序鏈表
題目描述
基本思路
這道題屬于雙鏈表合并的進階。理解這道題首先需要了解有序雙鏈表合并的解法。
已知鏈表有序,使用兩個指針指向兩個鏈表,逐一比較大小移動指針。代碼很簡單如下所示。
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(Integer.MIN_VALUE);
ListNode cur = dummy;
while (l1 != null && l2 != null) {
if (l1.val < l2.val) {
cur.next = l1;
l1 = l1.next;
} else {
cur.next = l2;
l2 = l2.next;
}
cur = cur.next;
}
cur.next = l1 == null ? l2 : l1;
return dummy.next;
}
兩個有序鏈表合并,我們用到了兩個指針。那么N個鏈表,顯然無法保存N個指針去遍歷和移動。所以需要換種思路,可以對鏈表逐一進行兩兩合并。但是這樣會導致鏈表的重復遍歷。此時可以用到歸并排序中的思路:分治法。將鏈表對半拆分到單鏈表,再進行兩兩合并,這樣就不會有重復遍歷,詳情見方法三。
同時,如果題目不要求空間復雜度,可以使用數(shù)組或集合來快速實現(xiàn)整體的排序,容易理解,見方法一。
擴展思路,雙鏈表合并,每次需要比較計算兩個指針大小。那么k個鏈表每次需要計算K個指針對應的節(jié)點大小。此時可以想到使用優(yōu)先級隊列,具體見方法二。
方法一:數(shù)組集合
不要求空間復雜度情況,可以借助數(shù)組集合,遍歷所有的鏈表上的節(jié)點,放入集合中,進行排序。再從集合中將其取出,構造新鏈表
/**
* 如果不限制空間復雜度,可以放到數(shù)組中排序,再拿出
* 時間復雜度O(NlogN) = 遍歷所有值 O(N) + 穩(wěn)定排序算法O(NlongN) + 遍歷創(chuàng)建新鏈表O(N)
*/
public ListNode mergeKLists1(ListNode[] lists) {
List<ListNode> allList = new ArrayList<>();
for (ListNode node : lists) {
while (node != null) {
allList.add(node);
node = node.next;
}
}
allList.sort(new Comparator<ListNode>() {
@Override
public int compare(ListNode o1, ListNode o2) {
return o1.val - o2.val;
}
});
ListNode dummy = new ListNode(0);
ListNode cur = dummy;
for (ListNode node : allList) {
cur.next = node;
cur = cur.next;
}
cur.next = null;
return dummy.next;
}
方法二:優(yōu)先級隊列
使用大小為鏈表長度的優(yōu)先級隊列,可以將優(yōu)先級隊列看成大小為k的小根堆。將k個鏈表的頭節(jié)點全部加入小根堆中。當堆不為空,從堆中彈出堆頂,即最小值,并將當前節(jié)點的next指針指向該值,當前節(jié)點后移。此時當前節(jié)點指向了k個鏈表中最小的節(jié)點,判斷其下一個節(jié)點是否為空,不為空則將下一個節(jié)點加入小根堆中。
/**
* 如果不限制空間復雜度,也可以使用優(yōu)先級隊列
*/
public ListNode mergeKLists2(ListNode[] lists) {
//空集合初始化優(yōu)先級隊列會拋出異常, 此處需要先判空
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;
}
});
ListNode dummy = new ListNode(0);
ListNode cur = dummy;
for (ListNode node : lists) {
if (node != null) {
queue.add(node);
}
}
while (!queue.isEmpty()) {
cur.next = queue.poll();
cur = cur.next;
if (cur.next != null) {
queue.add(cur.next);
}
}
return dummy.next;
}
方法三:分治法
思路類似鏈表的歸并排序,使用分治法,遞歸將鏈表拆分,直到每個都為單獨鏈表,再將其兩兩合并。
這樣避免了鏈表的重復遍歷。
/**
* 分治法:遞歸拆分鏈表變成單獨鏈表,再兩兩合并 思路類似鏈表的歸并排序
* 時間復雜度:O(Nlogk),其中k 鏈表的數(shù)目
*/
public ListNode mergeKLists(ListNode[] lists) {
return partion(lists, 0, lists.length - 1);
}
public ListNode partion(ListNode[] lists, int start, int end) {
//終止條件
if (start == end) {
return lists[start];
}
int mid = start + ((end - start) >> 1);
ListNode l1 = partion(lists, start, mid);
ListNode l2 = partion(lists, mid + 1, end);
return mergeTwoLists(l1, l2);
}
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(Integer.MIN_VALUE);
ListNode cur = dummy;
while (l1 != null && l2 != null) {
if (l1.val < l2.val) {
cur.next = l1;
l1 = l1.next;
} else {
cur.next = l2;
l2 = l2.next;
}
cur = cur.next;
}
cur.next = l1 == null ? l2 : l1;
return dummy.next;
}