Why is my python code wrong?


  • 0
    L
    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution:
        # @param two ListNodes
        # @return a ListNode
        def mergeTwoLists(self, l1, l2):
            if l1 == None:
                return l2
            if l2 == None:
                return l1
            m = 1
            p1 = l1
            while p1.next != None:
                m += 1
                p1 = p1.next
            n = 1
            p2 = l2
            while p2.next != None:
                n += 1
                p2 = p2.next
            
            p1 = l1
            p2 = l2
            if l1.val <= l2.val:
                s = l1
                p1 = p1.next
            else:
                s = l2
                p2 = p2.next
            s1 = s
            for k = range(m+n-1):
                if p1 == None:
                    s1.next = p2
                    return s
                if p2 == None:
                    s1.next = p1
                    return s
                if p1.val <= p2.val:
                    s1.next = p1
                    s1 = p1
                    p1 = p1.next
                else:
                    s1.next = p2
                    s1 = p2
                    p2 = p2.next
    

    When input is {}, {}, I get Runtime Error Message: Line 35: SyntaxError: invalid syntax

    Line 35 is:
    for k = range(m+n-1):

    However, the beginning of my code is:

        if l1 == None:
            return l2
        if l2 == None:
            return l1
    

    I think it should return None when the input is {}, {}


  • 0
    K

    ask youself:
    what is {} ?
    is {} the same as None?
    hint: empty object or not an object


  • 0
    L

    Thank you kevintomlee. In my opinion, when the list is empty, the ListNode should be None. I tried the following but still got runtime error

        if not l1:
            return l2
        if not l2:
            return l1
    
    
        if l1.val == None:
            return l2
        if l2.val == None:
            return l1
    
    
        if l1 == {}:
            return l2
        if l2 == {}:
            return l1
    

  • 0
    K

    ok..first of all sorry for that i didn't read your full code.
    two problems here in your code(after a quick scan):
    1.{} is not an empty list, it is an empry dictionary(also, None is different than empty)
    if you wanto test your code, supply something like ListNode()
    2.for k = range(m+n-1): should be:
    for k in range(m+n-1)


  • 0
    L

    Thanks! My code is accepted after using "for k in range(m+n-1)".


  • 0
    S

    And this code can optimized even further for this solution. Please look at other solutions for clues. But good job otherwise getting there !


Log in to reply
 

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