File tree 2 files changed +36
-0
lines changed
2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change 21
21
13|[ Roman to Integer] ( ./0013-roman-to-integer.js ) |Easy|
22
22
14|[ Longest Common Prefix] ( ./0014-longest-common-prefix.js ) |Easy|
23
23
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|
24
25
27|[ Remove Element] ( ./0027-remove-element.js ) |Easy|
25
26
31|[ Next Permutation] ( ./0031-next-permutation.js ) |Medium|
26
27
36|[ Valid Sudoku] ( ./0036-valid-sudoku.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments