Skip to content

Commit 9db690b

Browse files
add java solution for leetcode 21
1 parent dc3b469 commit 9db690b

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ LeetCode
391391
|24|[Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/)| [js](./algorithms/swapNodesInPairs/swapNodesInPairs.js) |Medium|
392392
|23|[Merge k Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/)| |Hard|
393393
|22|[Generate Parentheses](https://leetcode.com/problems/generate-parentheses/)| [js](./algorithms/generateParentheses/generateParentheses.js) |Medium|
394-
|21|[Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/)| |Easy|
394+
|21|[Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/)| [java](./algorithm/mergeTwoSortedLists/Solution.java) |Easy|
395395
|20|[Valid Parentheses](https://leetcode.com/problems/valid-parentheses/)| |Easy|
396396
|19|[Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/)| [js](./algorithms/removeNthNodeFromEndOfList/removeNthNodeFromEndOfList.js) |Easy|
397397
|18|[4Sum](https://leetcode.com/problems/4sum/)| |Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class Solution {
2+
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
3+
if (null == l1) {
4+
return l2;
5+
} else if (null == l2) {
6+
return l1;
7+
} else if (l1.val < l2.val) {
8+
l1.next = mergeTwoLists(l1.next, l2);
9+
return l1;
10+
} else {
11+
l2.next = mergeTwoLists(l1, l2.next);
12+
return l2;
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)