-
Notifications
You must be signed in to change notification settings - Fork 344
/
合并两个有序链表.js
59 lines (55 loc) · 1.03 KB
/
合并两个有序链表.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
function ListNode(val) {
this.val = val
this.next = null
}
let node1 = new ListNode(1)
node1.next = new ListNode(2)
node1.next.next = new ListNode(4)
let node2 = new ListNode(1)
node2.next = new ListNode(3)
node2.next.next = new ListNode(4)
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/
let mergeTwoLists = function(l1, l2) {
let arr = []
if (!l1 && !l2) {
return null
}
while (l1 || l2) {
let runL1 = () => {
arr.push(l1.val)
l1 = l1.next
}
let runL2 = () => {
arr.push(l2.val)
l2 = l2.next
}
if (!l1) {
runL2()
} else if (!l2) {
runL1()
} else if (l1.val > l2.val) {
runL2()
} else {
runL1()
}
}
return arrToList(arr)
}
function arrToList(arr) {
let i = 0
let r = new ListNode()
let head = r
while (i < arr.length) {
head.val = arr[i]
if (++i < arr.length) {
head.next = new ListNode()
head = head.next
}
}
return r
}
console.log(JSON.stringify(mergeTwoLists(node1, node2)))