Simple Python Solution Using Dict

  • 0
    class Solution(object):
        def __init__(self):
            self.hash = {}
        def copyRandomList(self, head):
            if not head:
                return None
            if head in self.hash:
                return self.hash[head]
            h = RandomListNode(head.label)
            self.hash[head] = h
   = self.copyRandomList(
            h.random = self.copyRandomList(head.random)
            return h

Log in to reply

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