Skip to content

Commit f9a83e7

Browse files
committed
Add merge-two-sorted-lists solution
1 parent 84283ab commit f9a83e7

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

merge-two-sorted-lists/Jeehay28.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
14+
// Time Complexity: O(m + n)
15+
// Space Complexity: O(m + n)
16+
17+
var mergeTwoLists = function(list1, list2) {
18+
19+
20+
if(!(list1 && list2)) {
21+
return list1 || list2;
22+
}
23+
24+
if(list1.val < list2.val) {
25+
list1.next = mergeTwoLists(list1.next, list2);
26+
return list1;
27+
} else {
28+
list2.next = mergeTwoLists(list1, list2.next);
29+
return list2;
30+
}
31+
32+
};
33+
34+

0 commit comments

Comments
 (0)