題目:
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
地址
分析:
- 每個(gè)鏈表已經(jīng)是有序了的
- 每個(gè)鏈表都有可能為空
思路:
- 可以使用歸并算法
因?yàn)槊總€(gè)鏈表都已經(jīng)是有序了的得封,所以跟歸并算法非常相似
- 可以使用歸并算法
- 可以使用最小堆
首先直接將它們都加入堆中苞轿,則堆頂元素是最小值瘩例。
只要堆不為空见坑,就一直取出堆頂元素,加入到返回鏈表中佑钾。如果取出的元素的子節(jié)點(diǎn)不為空,還需要繼續(xù)加入堆
- 可以使用最小堆
方式一
使用歸并排序的思路,遞歸調(diào)用吨岭。
假設(shè):k = lists.length , n = lists中的最長(zhǎng)的子鏈表長(zhǎng)度
時(shí)間復(fù)雜度:nklogk
空間復(fù)雜度:nk
public class T023 {
public static void main(String[] args) {
int[] arr1 = {1, 2, 3, 4, 5};
int[] arr2 = {-1, 2, 3, 4, 88};
int[] arr3 = {1, 6, 33, 42, 44};
int[] arr4 = {1, 7, 43, 52, 65};
ListNode node1 = UtilListNode.makeListNode(arr1);
ListNode node2 = UtilListNode.makeListNode(arr2);
ListNode node3 = UtilListNode.makeListNode(arr3);
ListNode node4 = UtilListNode.makeListNode(arr4);
ListNode[] lists = {node1, node2, node3, node4};
ListNode head = new T023().mergeKLists(lists);
UtilListNode.show(head);
}
public ListNode mergeKLists(ListNode[] lists) {
if (lists == null || lists.length == 0) return null;
return mergeSort(lists, 0, lists.length-1);
}
// 歸并排序的思路
public ListNode mergeSort(ListNode[] lists, int l, int r) {
if (l < r) {
int mid = (r - l) / 2 + l;
return mergeTwoLists(mergeSort(lists, l, mid), mergeSort(lists, mid + 1, r));
}
return lists[l];
}
// 排列兩個(gè)有序了的鏈表
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
// 虛擬頭節(jié)點(diǎn)
ListNode dummy = new ListNode(0);
ListNode head = dummy;
while (l1 != null && l2 != null) {
if (l1.val <= l2.val) {
head.next = new ListNode(l1.val);
l1 = l1.next;
} else {
head.next = new ListNode(l2.val);
l2 = l2.next;
}
head = head.next;
}
while (l1 != null) {
head.next = new ListNode(l1.val);
l1 = l1.next;
head = head.next;
}
while (l2 != null) {
head.next = new ListNode(l2.val);
l2 = l2.next;
head = head.next;
}
return dummy.next;
}
}
方式二
使用堆(優(yōu)先隊(duì)列)的方式,先將它們都加入隊(duì)列峦树,每次取出頂部元素辣辫,加入返回鏈表中。在看下該取出的元素的子節(jié)點(diǎn)是否為空魁巩,若不為空繼續(xù)加入堆急灭。
時(shí)間復(fù)雜度: 堆讀取是 log(k),一共有 n*k 個(gè)元素谷遂,則時(shí)間復(fù)雜度為:nklogk
空間復(fù)雜度:k
public class T023_2 {
public static void main(String[] args) {
int[] arr1 = {1, 2, 3, 4, 5};
int[] arr2 = {-1, 2, 3, 4, 88};
int[] arr3 = {1, 6, 33, 42, 44};
int[] arr4 = {1, 7, 43, 52, 65};
ListNode node1 = UtilListNode.makeListNode(arr1);
ListNode node2 = UtilListNode.makeListNode(arr2);
ListNode node3 = UtilListNode.makeListNode(arr3);
ListNode node4 = UtilListNode.makeListNode(arr4);
ListNode[] lists = {node1, node2, node3, node4};
ListNode head = new T023_2().mergeKLists(lists);
UtilListNode.show(head);
}
public ListNode mergeKLists(ListNode[] lists) {
if (lists == null || lists.length == 0) return null;
// 因?yàn)樵撴湵硪呀?jīng)是有序的葬馋,所以可以使用堆
PriorityQueue<ListNode> heep = new PriorityQueue<ListNode>(11, new Comparator<ListNode>() {
@Override
public int compare(ListNode o1, ListNode o2) {
return o1.val - o2.val;
}
});
for (int i = 0; i < lists.length; i++) {
// 加入堆中
ListNode list = lists[i];
if (list != null)
heep.offer(list);
}
ListNode pre = null;
ListNode head = null;
while (!heep.isEmpty()) {
// 取出堆頂
ListNode cur = heep.poll();
if (head == null) {
head = cur;
} else {
pre.next = cur;
}
pre = cur;
// 如果下個(gè)元素不為空,將它的下個(gè)元素加入堆
if (cur.next != null) {
heep.offer(cur.next);
}
}
return head;
}
}