-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.py
33 lines (29 loc) · 865 Bytes
/
Solution.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
"""
# Definition for a Node.
class Node:
def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None):
self.val = int(x)
self.next = next
self.random = random
"""
class Solution:
def copyRandomList(self, head: 'Node') -> 'Node':
if head is None:
return None
cur = head
while cur:
node = Node(cur.val, cur.next)
cur.next = node
cur = node.next
cur = head
while cur:
cur.next.random = None if cur.random is None else cur.random.next
cur = cur.next.next
copy = head.next
cur = head
while cur:
next = cur.next
cur.next = next.next
next.next = None if next.next is None else next.next.next
cur = cur.next
return copy