Python solution easy to understand

  • 1
    class Solution(object):
        def copyRandomList(self, head):
        :type head: RandomListNode
        :rtype: RandomListNode
        d = dict()
        d[None] = None
        m = head
        while head:
            if head not in d:
                cpHead = RandomListNode(head.label)
                d[head] = cpHead
            if head.random not in d:
                cpRandom = RandomListNode(head.random.label)
            if not in d:
                cpNext = RandomListNode(
            d[head].next = d[]
            d[head].random = d[head.random]
            head =
        return d[m]

Log in to reply

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