Python Solution


  • 0
    T
    def copyRandomList(self, head):
            """
            :type head: RandomListNode
            :rtype: RandomListNode
            """
            if head is None:
                return
            old_new_map = dict()
            old_node = head
            while old_node is not None:
                rln = RandomListNode(old_node.label)
                old_new_map[old_node] = rln
                old_node = old_node.next
            
            for on, nn in old_new_map.items():
                if on.next is not None:
                    nn.next = old_new_map[on.next]
                if on.random is not None:
                    nn.random = old_new_map[on.random]
            return old_new_map[head]
    

Log in to reply
 

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