leetcode138Copy List with Random Pointer

来源:互联网 发布:mac被设密码了怎么办 编辑:程序博客网 时间:2024/05/21 22:27
class Solution(object):
    def copyRandomList(self, head):
        """
        :type head: RandomListNode
        :rtype: RandomListNode
        """
        #copyhead=RandomListNode(0)
        dic = collections.defaultdict(lambda: RandomListNode(10))
        #dic[None] = None
        if head==None:
            return head
        n = head
        while n:
            dic[n].label = n.label
            if n.next==None:
                dic[n].next=None
            else:
                dic[n].next = dic[n.next]
            if n.random==None:
                dic[n].random=None
            else:
                dic[n].random = dic[n.random]
            n = n.next

        return dic[head]


参考https://discuss.leetcode.com/topic/9557/clear-and-short-python-o-2n-and-o-n-solution/6

0 0
原创粉丝点击