输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)
1 # -*- coding:utf-8 -*- 2 # class RandomListNode: 3 # def __init__(self, x): 4 # self.label = x 5 # self.next = None 6 # self.random = None 7 class Solution: 8 # 返回 RandomListNode 9 def Clone(self, pHead): 10 if pHead == None: 11 return None 12 dHead = pHead 13 while dHead: 14 n = RandomListNode(dHead.label) 15 n.next = dHead.next 16 dHead.next = n 17 dHead = n.next 18 dHead = pHead 19 while dHead: 20 dHead.next.random = dHead.random 21 dHead = dHead.next.next 22 23 dHead = pHead 24 copyhead = RandomListNode(0) 25 copyhead.next = pHead.next 26 firstOne = True 27 while dHead: 28 copy = dHead.next 29 dHead.next = dHead.next.next 30 if copy.next != None: 31 copy.next = copy.next.next 32 else: 33 copy.next = None 34 dHead = dHead.next 35 return copyhead.next 36 # write code here