链接:https://leetcode-cn.com/problems/copy-list-with-random-pointer/
利用字典存放,先复制,后指针
"""
# Definition for a Node.
class Node:
def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None):
self.val = int(x)
self.next = next
self.random = random
"""
class Solution:
def copyRandomList(self, head: 'Node') -> 'Node':
if not head:return None
dic = {}
new_head = head
while new_head:
dic[new_head]= Node(new_head.val)
new_head=new_head.next
new_head = head
while new_head:
if new_head.next:
dic[new_head].next = dic[new_head.next]
if new_head.random:
dic[new_head].random = dic[new_head.random]
new_head = new_head.next
return dic[head]