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


  • 0
    J
    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 cur.next:
                    if cur.next not in mapping:
                        node = RandomListNode(cur.next.label)
                        mapping[cur.next] = node
                    mapping[cur].next = mapping[cur.next]
                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 = cur.next
            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.