-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathq0138.py
More file actions
25 lines (21 loc) · 715 Bytes
/
q0138.py
File metadata and controls
25 lines (21 loc) · 715 Bytes
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
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:
dic = {}
def copyRandomList(self, head: 'Node') -> 'Node':
self.dic = {}
return self.deepCopyNode(head)
def deepCopyNode(self, node: 'Node') -> 'Node':
if node is None:
return None
if node not in self.dic:
newNode = Node(node.val)
self.dic[node] = newNode
newNode.next = self.deepCopyNode(node.next)
newNode.random = self.deepCopyNode(node.random)
return newNode
else:
return self.dic[node]