Python beat 52%. Not a perfect solution but hope this is helpful


  • 4
    X

    class Solution(object):

    def mergeTwoLists(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        if l1 == None:
            return l2
        elif l2 == None:
            return l1
        head = ListNode(-1)
        p1 = l1
        p2 = l2
        p3 = head
        while p1 != None and p2 != None:
            if p1.val > p2.val:
                p3.next = p2
                p2 = p2.next
                p3 = p3.next
            else:
                p3.next = p1
                p1 = p1.next
                p3 = p3.next
        if p2 == None:
            p3.next = p1
            return head.next
        else:
            p3.next = p2
            return head.next

  • 0
    J
    How do you know the order is ascending?

  • 0
    L

    ascending is default sorted way


  • 0
    X

    it's a sorted list


Log in to reply
 

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