Is this a bad solution? Why nobody approached the problem like this?

  • 0
    class Solution(object):
        def copyRandomList(self, head):
            :type head: RandomListNode
            :rtype: RandomListNode
            if not head:
                return None        
            cur = head
            mapping = {}    
            while cur:                
                if cur not in mapping:
                    node = RandomListNode(cur.label)
                    mapping[cur] = node
                    if not in mapping:
                        node = RandomListNode(
                        mapping[] = node
                    mapping[cur].next = mapping[]
                if cur.random:
                    if cur.random not in mapping:
                        node = RandomListNode(cur.random.label)
                        mapping[cur.random] = node
                    mapping[cur].random = mapping[cur.random]
                cur =
            return mapping[head]

    I saw a similar solution and someone give him a down vote? Can anyone explain why?
    Thank you!

Log in to reply

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