-
Notifications
You must be signed in to change notification settings - Fork 481
/
0138.py
37 lines (32 loc) · 939 Bytes
/
0138.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
35
36
37
# 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 None
h1, h2, h = head, head, head
while h1:
node = RandomListNode(h1.label)
node.next = h1.next
h1.next = node
h1 = node.next
while h2:
if h2.random:
h2.next.random = h2.random.next
h2 = h2.next.next
res = h.next
while h:
tmp = h.next
h.next = tmp.next
if h.next:
tmp.next = h.next.next
h = h.next
return res