python tail recursive solution


  • 1
    G
    class Solution(object):
        def mergeTwoLists(self, l1, l2):
            """
            :type l1: ListNode
            :type l2: ListNode
            :rtype: ListNode
            """
            def cont(cur, l1 ,l2):
                if l1 is None:
                    cur.next = l2
                    return
                if l2 is None:
                    cur.next = l1
                    return
                if l1.val <= l2.val:
                    cur.next = l1
                    cont(cur.next, l1.next, l2)
                else:
                    cur.next = l2
                    cont(cur.next, l1, l2.next)
            head = ListNode(0)
            cur = head
            cont(cur, l1, l2)
            return head.next
    

Log in to reply
 

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