Blog Archive

Sunday, September 14, 2014

[leetcode]Copy List with Random Pointer @ Python - 南郭子綦 - 博客园

[leetcode]Copy List with Random Pointer @ Python - 南郭子綦 - 博客园: "if head == None: return None
tmp = head
while tmp:
newNode = RandomListNode(tmp.label)
newNode.next = tmp.next
tmp.next = newNode
tmp = tmp.next.next
tmp = head
while tmp:
if tmp.random:
tmp.next.random = tmp.random.next
tmp = tmp.next.next
newhead = head.next
pold = head
pnew = newhead
while pnew.next:
pold.next = pnew.next
pold = pold.next
pnew.next = pold.next
pnew = pnew.next
pold.next = None
pnew.next = None
return newhead"



'via Blog this'

No comments:

Post a Comment