天天看点

LeetCode题解(0138):复制带随机指针的链表(Python)

题目:原题链接(中等)

标签:链表

解法 时间复杂度 空间复杂度 执行用时
Ans 1 (Python) 52ms (42.35%)
Ans 2 (Python)

O

(

N

)

O(N)

O(N)

36ms (98.50%)
Ans 3 (Python)
def copyRandomList(self, head: 'Node') -> 'Node':
    import copy
    return copy.deepcopy(head)
           
def copyRandomList(self, head: 'Node') -> 'Node':
    # 处理特殊情况
    if not head:
        return None

    # 生成节点并存储在字典中
    hashmap = {}
    node = head
    while node:
        hashmap[node] = Node(node.val)
        node = node.next

    # 生成next和random指针
    node = head
    while node:
        if node.next:
            hashmap[node].next = hashmap[node.next]
        if node.random:
            hashmap[node].random = hashmap[node.random]
        node = node.next

    # 返回结果
    return hashmap[head]