Simple Python Solution Using Dict


  • 0
    D
    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
            
            h.next = self.copyRandomList(head.next)
            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.