A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list.

A clear solution

from collections import defaultdict

class Solution(object):
    def copyRandomList(self, head):
        dic = defaultdict(lambda: RandomListNode(0))
        dic[None] = None ## handle the None special case
        cursor = head
        while cursor:
            dic[cursor].label = cursor.label
            dic[cursor].next = dic[cursor.next]
            dic[cursor].random = dic[cursor.random]
            cursor = cursor.next
        return dic[head]

A tricky solution

## make up

refer:

results matching ""

    No results matching ""