Python 9 lines recursive, easy to understand


  • 0
    N
        def copyRandomList(self, head, map={}):
            if not head:
                return None
            if head in map:
                return map[head]
            newHead = RandomListNode(head.label)
            map[head] = newHead
            newHead.next = self.copyRandomList(head.next, map)
            newHead.random = self.copyRandomList(head.random, map)
            return newHead
    

Log in to reply
 

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