-
Notifications
You must be signed in to change notification settings - Fork 0
/
23.js
63 lines (59 loc) · 1.27 KB
/
23.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
/*
* @lc app=leetcode.cn id=23 lang=javascript
*
* [23] 合并K个排序链表
*/
// @lc code=start
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode[]} lists
* @return {ListNode}
*/
var mergeKLists = function(lists) {
if (!lists || lists.length == 0) {
return new ListNode().next
}
if (lists.length == 1) {
return lists[0]
}
let mergeTwo = (l1,l2) => {
let p1 = l1;
let p2 = l2;
let res = new ListNode();
let head = res;
while(p1 && p2) {
if (p1.val > p2.val) {
res.next = p2
res = res.next
p2 = p2.next
} else {
res.next = p1
res = res.next
p1 = p1.next
}
}
if (p1 && !p2) {
res.next = p1
}
else if(p2 && !p1) {
res.next = p2
}
return head.next
};
let arr = []
for (let i = 0; i < lists.length; i+=2) {
if (lists[i+1]) {
arr.push(mergeTwo(lists[i],lists[i+1]))
} else {
arr.push(lists[i])
}
}
return mergeKLists(arr)
};
// @lc code=end