Python Merge Sort


  • 0
    class Solution(object):
        def mergeTwoLists(self, l1, l2)
            ans = []
            while l1 or l2:
                if not l1:
                    ans.append(l2.val)
                    l2 = l2.next
                elif not l2:
                    ans.append(l1.val)
                    l1 = l1.next
                else:
                    if l1.val < l2.val:
                        ans.append(l1.val)
                        l1 = l1.next
                    else:
                        ans.append(l2.val)
                        l2 = l2.next
            return ans
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.