leetcode-Merge Two Sorted Lists

https://leetcode.com/problems/merge-two-sorted-lists/

class Solution:
    # @param {ListNode} l1
    # @param {ListNode} l2
    # @return {ListNode}
    def mergeTwoLists(self, l1, l2):
        if l1 is None: return l2
        if l2 is None: return l1
        
        if l1.val > l2.val:
            l1, l2 = l2, l1
        l1.next = self.mergeTwoLists(l1.next, l2)
        return l1

之前硬生生的把题意理解为一定要新建一个列表并返回,搞的我好不乐乎。。

看了讨论区才知道并不是这样,返回正确的列表就可以了。顺便看到了这个给力的方法瓦!

原文地址:https://www.cnblogs.com/Blaxon/p/4685595.html