11 lines Python solution


  • 0
    R
    class Solution(object):
        def copyRandomList(self, head):
            """ 
            :type head: RandomListNode
            :rtype: RandomListNode
            """
            map = {None : None}
            temp = head
            while temp:
                map[temp] = RandomListNode(temp.label)
                temp = temp.next
            temp = head
            while temp:
                map[temp].next = map[temp.next]
                map[temp].random = map[temp.random]
                temp = temp.next
            return map[head]
    

    The {None : None} key-value pair is used handle null random pointers and the tail of the List.


Log in to reply
 

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