微信图片_20191221181154.jpg
class Solution:
# 返回 RandomListNode
def Clone(self, pHead):
# write code here
if not pHead:
return None
tmp = pHead
while tmp:
node = RandomListNode(tmp.label)
node.next = tmp.next
tmp.next = node
tmp = tmp.next.next
tmp = pHead
while tmp and tmp.next:
if tmp.random:
tmp.next.random = tmp.random.next
tmp = tmp.next.next
tmp = pHead
newHead = tmp.next
newTmp = newHead
while tmp and tmp.next:
tmp.next = tmp.next.next
if newTmp.next:
newTmp.next = newTmp.next.next
newTmp = newTmp.next
tmp = tmp.next
return newHead
网友评论