-
Notifications
You must be signed in to change notification settings - Fork 24
/
0023-merge-k-sorted-lists.js
52 lines (43 loc) · 1.24 KB
/
0023-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
// 23. Merge k Sorted Lists
// Hard 27%
// Merge k sorted linked lists and return it as one sorted list. Analyze and
// describe its complexity.
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode[]} lists
* @return {ListNode}
*/
const mergeKLists = function(lists) {
if (lists == null || lists.length === 0) return null
const mergeTwoLists = (l1, l2) => {
if (l1 == null) return l2
if (l2 == null) return l1
if (l1.val > l2.val) {
l2.next = mergeTwoLists(l1, l2.next)
return l2
} else{
l1.next = mergeTwoLists(l1.next, l2)
return l1
}
}
while(lists.length > 1) lists.push(mergeTwoLists(lists.shift(), lists.shift()))
return lists.shift()
}
const ListNode = require('../structs/ListNode')
;[
[[1, 3], [0, 1], []],
].forEach((lists) => {
lists = lists.map(l => ListNode.from(l))
console.log((mergeKLists(lists) || '').toString())
})
// Solution:
// 分治思想,在两个链表结合的时候,使用21-merge-two-sorted-lists.js中的方法。
// 这里使用队列来实现分治。
// 队列中的前两个链表结合成一个,并插入到队列末尾。
// Submission Result: Accepted