剑指 Offer 35. 复杂链表的复制
题源 https://leetcode-cn.com/problems/fu-za-lian-biao-de-fu-zhi-lcof/
Class RandomNode:
def __init__(self,x:int,next:'RandomNode'=None,random:'RandomNode'=None):
self.val=x
self.next=next
self.random=random
def copyRandomList(head:'RandomNode'):
visited={}
def dfs(root:'RandomNode'):
if not root:
return root
nonlocal visited
if root in visited:
return visited[root]
copy=RandomNode(root.val)
visited[root]=copy
copy.next=dfs(root.next)
copy.random=dfs(root.random)
return copy
return dfs(head)
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!