Skip to content

Commit b7ed029

Browse files
committed
Time: 53 ms (92.59%), Space: 51.6 MB (47.29%) - LeetHub
1 parent 6bcd0cd commit b7ed029

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} list1
10+
* @param {ListNode} list2
11+
* @return {ListNode}
12+
*/
13+
var mergeTwoLists = function (list1, list2) {
14+
let dummy = new ListNode();
15+
let current = dummy;
16+
while (list1 && list2) {
17+
if (list1.val <= list2.val) {
18+
current.next = list1;
19+
list1 = list1.next;
20+
} else {
21+
current.next = list2;
22+
list2 = list2.next;
23+
}
24+
current = current.next;
25+
}
26+
if (list1) {
27+
current.next = list1;
28+
} else {
29+
current.next = list2;
30+
}
31+
return dummy.next;
32+
};

0 commit comments

Comments
 (0)