class Solution:
def copyRandomList(self, head: 'Node') -> 'Node':
return copy.deepcopy(head)
class Solution:
def copyRandomList(self, head: 'Node') -> 'Node':
def dfs(head):
if not head: return None
if head in visited:
return visited[head]
# 创建新结点
copy = Node(head.val, None, None)
visited[head] = copy
copy.next = dfs(head.next)
copy.random = dfs(head.random)
return copy
visited = {}
return dfs(head)
class Solution:
def copyRandomList(self, head: 'Node') -> 'Node':
visited = {}
def bfs(head):
if not head: return head
clone = Node(head.val, None, None) # 创建新结点
queue = collections.deque()
queue.append(head)
visited[head] = clone
while queue:
tmp = queue.pop()
if tmp.next and tmp.next not in visited:
visited[tmp.next] = Node(tmp.next.val, [], [])
queue.append(tmp.next)
if tmp.random and tmp.random not in visited:
visited[tmp.random] = Node(tmp.random.val, [], [])
queue.append(tmp.random)
visited[tmp].next = visited.get(tmp.next)
visited[tmp].random = visited.get(tmp.random)
return clone
return bfs(head)
class Solution:
def copyRandomList(self, head: 'Node') -> 'Node':
visited = {}
def getClonedNode(node):
if node:
if node in visited:
return visited[node]
else:
visited[node] = Node(node.val,None,None)
return visited[node]
return None
if not head: return head
old_node = head
new_node = Node(old_node.val,None,None)
visited[old_node] = new_node
while old_node:
new_node.random = getClonedNode(old_node.random)
new_node.next = getClonedNode(old_node.next)
old_node = old_node.next
new_node = new_node.next
return visited[head]
class Solution:
def copyRandomList(self, head: 'Node') -> 'Node':
if not head: return head
cur = head
while cur:
new_node = Node(cur.val,None,None) # 克隆新结点
new_node.next = cur.next
cur.next = new_node # 克隆新结点在cur 后面
cur = new_node.next # 移动到下一个要克隆的点
cur = head
while cur: # 链接random
cur.next.random = cur.random.next if cur.random else None
cur = cur.next.next
cur_old_list = head # 将两个链表分开
cur_new_list = head.next
new_head = head.next
while cur_old_list:
cur_old_list.next = cur_old_list.next.next
cur_new_list.next = cur_new_list.next.next if cur_new_list.next else None
cur_old_list = cur_old_list.next
cur_new_list = cur_new_list.next
return new_head