Python 9 lines recursive, easy to understand

  • 0
        def copyRandomList(self, head, map={}):
            if not head:
                return None
            if head in map:
                return map[head]
            newHead = RandomListNode(head.label)
            map[head] = newHead
   = self.copyRandomList(, 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.