A complex input list (each node has a node value, and two pointers, one pointing to the next node, a special pointer to any other node),
returns a value after the head of the list replication complex. (Note that the output results do not return parameter node reference, otherwise the program will return empty sentenced questions directly)
Problem-solving ideas: both sides of the cycle:
1: two first pass opening arrays, the presence of each of the first node, the second node exists for each value of the new node is opened. While establishing a node memory address to index mapping id2index
2: to open a new traversing node array, and the next supplement by id2index random pointers.
# 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==None: return None iter_array,array=[],[] id2index=dict() index=0 #p=pHead while pHead: iter_array.append(pHead) array.append(RandomListNode(pHead.label)) id2index[id(pHead)]=index index+=1 pHead=pHead.next for p, res in zip(iter_array,array): if p.next: res.next=array[id2index[id(p.next)]] if p.random: res.random=array[id2index[id(p.random)]] return array[0]