-
Notifications
You must be signed in to change notification settings - Fork 71
/
No23.merge-k-sorted-lists.js
71 lines (64 loc) · 1.56 KB
/
No23.merge-k-sorted-lists.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* Difficulty:
* Hard
*
* Desc:
* Merge k sorted linked lists and return it as one sorted list.
* Analyze and describe its complexity.
*
* 将 k 个已排好序的链表合并。注意优化其复杂度
*/
/*
* 思路:
* 算法和 21 题 merge two sorted lists 一致
* 除此以外还有一种方法:
* 即一开始就将所有链表的第一个元素放入最小堆中,每次从最小堆中取出最小值,
* 将其添加到答案的最后,并将其所属链表的下一个元素加入到最小堆当中
*
* 或者还有比较偷鸡的办法,就是每次从 lists 中 pop 两个队列,然后进行 two lists merge
* 甚至可以依一次 pop 更多的队列,使复杂度进一步降低
*/
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/
var mergeTwoLists = function(l1, l2) {
var result = new ListNode();
var current = result;
while(l1 && l2) {
if (l1.val < l2.val) {
current.next = l1;
l1 = l1.next;
} else {
current.next = l2;
l2 = l2.next;
}
current = current.next;
}
if (l1) {
current.next = l1;
} else if (l2) {
current.next = l2;
}
return result.next;
};
/**
* @param {ListNode[]} lists
* @return {ListNode}
*/
var mergeKLists = function(lists) {
if (lists.length === 0) return null;
var result = lists[0];
for (var i = 1; i < lists.length; i += 1) {
result = mergeTwoLists(result, lists[i]);
}
return resul;
};