Python code failed to create a node with None as label


  • 0
    R
    class Solution(object):
        def copyRandomList(self, head):
            """
            :type head: RandomListNode
            :rtype: RandomListNode
            """
            if not head:
                return head
    
            nodes = {}
            pre_head = RandomListNode(None)
            tail = pre_head
            curr = head
            while curr:
                node = RandomListNode(curr.label)
                tail.next = node
                nodes[curr] = node
                tail = tail.next
                curr = curr.next
    
            tail = pre_head.next
            curr = head
            while curr:
                if curr.random:
                    tail.random = nodes[curr.random]
                else:
                    tail.random = None
                curr = curr.next
                tail = tail.next
            return pre_head.next
    

    This code failed due to Line 18: TypeError: int() argument must be a string or a number, not 'NoneType' with input {-1,#}
    But if I change pre_head = RandomListNode(None) to pre_head = RandomListNode(0), this code become accepted. So I think leetcode oj tried to convert None to int with int() where the TypeError is raised.
    However the description doesn't say it will convert the label to int. Could this be treated as a bug of the problem?


Log in to reply
 

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