-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.js
More file actions
37 lines (34 loc) · 790 Bytes
/
solution.js
File metadata and controls
37 lines (34 loc) · 790 Bytes
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
/**
* 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) {
let head = res = null
while (l1 !== null && l2 !== null) {
if (res === null) {
head = res = new ListNode(Math.min(l1.val, l2.val))
} else {
head.next = new ListNode(Math.min(l1.val, l2.val))
head = head.next
}
if (l1.val <= l2.val) {
l1 = l1.next
} else {
l2 = l2.next
}
}
if (res === null) {
res = l1 !== null ? l1 : l2
} else {
head.next = l1 !== null ? l1 : l2
}
return res
};