# Definition for singly-linked list with a random pointer.
# class RandomListNode:
# def __init__(self, x):
# self.label = x
# self.next = None
# self.random = None
class Solution:
# @param head, a RandomListNode
# @return a RandomListNode
def copyRandomList(self, head):
if head == None: return None
#insert
current = head
while current != None:
next = current.next
current.next = RandomListNode(current.label)
current.next.next = next
current = next
#reset random pointer
current = head
while current != None:
next = current.next
if current.random != None:
next.random = current.random.next
current = next.next
#split
nhead = head.next
current, ncurrent = head, nhead
while current != None:
current.next = ncurrent.next
ncurrent.next = ncurrent.next.next if ncurrent.next != None else None
current = current.next
ncurrent = ncurrent.next
return nhead