LeetCode-剑指 Offer 35. 复杂链表的复制
思路:使用haspmap存储原来的结点和新节点的对应
代码:
"""
# 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
hashmap, p = {}, head
while p:
hashmap[p] = Node(p.val)
p = p.next
p = head
while p:
hashmap[p].next = hashmap.get(p.next)
hashmap[p].random = hashmap.get(p.random)
p = p.next
return hashmap[head]