Python Runtime error unhashable type


  • 0
    S

    I am working on the solution to "Copy List with Random Pointer" but believe I am stuck with copying for empty inputs.

    # Definition for singly-linked list with a random pointer.
    # class RandomListNode:
    #     def __init__(self, x):
    #         self.label = x
    #         self.next = None
    #         self.random = None
    

    My code has a runtime error presumably on these lines:

    def copyRandomList(self, head):
        myRandom = RandomListNode(head.label)if head is not None else dict()
    

    ... ...line #29 EOF

    Runtime Error Message: Line 60: TypeError: unhashable type: 'dict'
    Last executed input: {}

    What is being expected in the test-case ? Also what is the code running at line #60 in the test setup for evaluating this code.


  • 0
    S

    Would you mind share code with some explanation? That may help people find out the reason.


  • 0
    S

    The following code worked for me:

    if head is None: return None
        copy = RandomListNode(head.label)
    

Log in to reply
 

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