题解 | #复杂链表的复制#
复杂链表的复制
https://www.nowcoder.com/practice/f836b2c43afc4b35ad6adc41ec941dba
# -*- coding:utf-8 -*- # class RandomListNode: # def __init__(self, x): # self.label = x # self.next = None # self.random = None class Solution: # 返回 RandomListNode def Clone(self, pHead): # write code here if pHead is None: return pHead res = RandomListNode(0) map = dict() cur = pHead pre = res while cur is not None: clone = RandomListNode(cur.label) map[cur] = clone pre.next = clone pre = pre.next cur = cur.next for (key,value) in map.items(): if key.random is None: value.random = None else: value.random = map[key.random] return res.next