-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path138.py
34 lines (33 loc) · 908 Bytes
/
138.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Definition for singly-linked list with a random pointer.
# class RandomListNode(object):
# def __init__(self, x):
# self.label = x
# self.next = None
# self.random = None
class Solution(object):
def copyRandomList(self, head):
"""
:type head: RandomListNode
:rtype: RandomListNode
"""
if not head:
return
tag = head
while tag:
temp = tag.next
new = RandomListNode(tag.label)
tag.next = new
new.next = temp
tag = temp
tag = head
while tag:
if tag.random:
tag.next.random = tag.random.next
tag = tag.next.next
res = head.next
tag = head
while tag and tag.next:
temp = tag.next
tag.next = tag.next.next
tag = temp
return res