-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (39 loc) · 921 Bytes
/
index.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
/*
Merge two sorted linked lists and return it as a new list. The new list
should be made by splicing together the nodes of the first two lists.
Example:
Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4
*/
function mergeTwoSortedLists(list1, list2) {
if (!list1 && !list2) return null;
if (!list1) return list2;
if (!list2) return list1;
let list = null;
let cur = null;
if (list1.val < list2.val) {
list = list1;
cur = list1;
list1 = list1.next;
} else {
list = list2;
cur = list2;
list2 = list2.next;
}
while (list1 || list2) {
if (((list1) ? list1.val : Infinity) < ((list2) ? list2.val : Infinity)) {
cur.next = list1;
cur = cur.next;
list1 = list1.next;
} else {
cur.next = list2;
cur = cur.next;
list2 = list2.next;
}
}
return list;
}
function ListNode(val, next = null) {
this.val = val;
this.next = next;
}