Leetcode 21 Merge Two Sorted Lists
2017-11-16 16:17
405 查看
Java:
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1==null) return l2;
if(l2==null) return l1;
if(l1.val<l2.val){
l1.next=mergeTwoLists(l1.next,l2);
return l1;
}
else{
l2.next=mergeTwoLists(l1,l2.next);
return l2;
}
}
}
python3:
class Solution:
def mergeTwoLists(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
if l1 and l2 :
if l1.val>l2.val:
l1,l2=l2,l1
l1.next=self.mergeTwoLists(l1.next,l2)
return l1 or l2;
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1==null) return l2;
if(l2==null) return l1;
if(l1.val<l2.val){
l1.next=mergeTwoLists(l1.next,l2);
return l1;
}
else{
l2.next=mergeTwoLists(l1,l2.next);
return l2;
}
}
}
python3:
class Solution:
def mergeTwoLists(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
if l1 and l2 :
if l1.val>l2.val:
l1,l2=l2,l1
l1.next=self.mergeTwoLists(l1.next,l2)
return l1 or l2;
相关文章推荐
- 【LeetCode】21. Merge Two Sorted Lists
- [leetcode]21. Merge Two Sorted Lists
- LeetCodet题解--21. Merge Two Sorted Lists(合并两个排序好的链表)
- LeetCode 21 Merge Two Sorted Lists
- Leetcode-21: Merge Two Sorted Lists
- Leetcode 21 Merge Two Sorted Lists
- Leetcode 21 Merge Two Sorted Lists
- LeetCode之21----Merge Two Sorted Lists
- leetcode: (21) Merge Two Sorted Lists
- [LeetCode] 21 - Merge Two Sorted Lists
- LeetCode (21)Merge Two Sorted Lists
- leetcode21~Merge Two Sorted Lists
- 【LeetCode】21. Merge Two Sorted Lists
- LeetCode - 21. Merge Two Sorted Lists
- 【LeetCode-21】 Merge Two Sorted Lists(C++)
- LeetCode - 21. Merge Two Sorted Lists
- [Leetcode 21] 21 Merge Two Sorted Lists
- [LeetCode By Python] 21. Merge Two Sorted Lists
- leetcode-Merge Two Sorted Lists-21
- Leetcode 21 Merge Two Sorted Lists