Skip to content

Commit 1ee3e62

Browse files
committed
Add solution #21
1 parent 4eb5e4d commit 1ee3e62

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
13|[Roman to Integer](./0013-roman-to-integer.js)|Easy|
2222
14|[Longest Common Prefix](./0014-longest-common-prefix.js)|Easy|
2323
17|[Letter Combinations of a Phone Number](./0017-letter-combinations-of-a-phone-number.js)|Medium|
24+
21|[Merge Two Sorted Lists](./0021-merge-two-sorted-lists.js)|Easy|
2425
27|[Remove Element](./0027-remove-element.js)|Easy|
2526
31|[Next Permutation](./0031-next-permutation.js)|Medium|
2627
36|[Valid Sudoku](./0036-valid-sudoku.js)|Medium|
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* 21. Merge Two Sorted Lists
3+
* https://leetcode.com/problems/merge-two-sorted-lists/
4+
* Difficulty: Easy
5+
*
6+
* Merge two sorted linked lists and return it as a sorted list.
7+
* The list should be made by splicing together the nodes of the
8+
* first two lists.
9+
*/
10+
11+
/**
12+
* Definition for singly-linked list.
13+
* function ListNode(val, next) {
14+
* this.val = (val===undefined ? 0 : val)
15+
* this.next = (next===undefined ? null : next)
16+
* }
17+
*/
18+
/**
19+
* @param {ListNode} l1
20+
* @param {ListNode} l2
21+
* @return {ListNode}
22+
*/
23+
var mergeTwoLists = function(l1, l2) {
24+
if (!l1 || !l2) {
25+
return l1 || l2;
26+
}
27+
28+
if (l1.val > l2.val) {
29+
[l2, l1] = [l1, l2];
30+
}
31+
32+
l1.next = mergeTwoLists(l1.next, l2);
33+
34+
return l1;
35+
};

0 commit comments

Comments
 (0)