题目
关键词
链表
代码记录
"""
# 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':
or_seq=[]
or_map={}
cur=head
while cur:
or_seq.append(cur)
or_map[cur]=cur.random
cur=cur.next
copy_seq=[]
for n in or_seq:
copy_seq.append(Node(n.val))
for i in range(len(or_seq)-1):
copy_seq[i].next=copy_seq[i+1]
for i in range(len(or_seq)):
if or_map[or_seq[i]]:
copy_seq[i].random=copy_seq[or_seq.index(or_map[or_seq[i]])]
return copy_seq[0] if copy_seq else None