Skip to content

Commit 7db2de1

Browse files
committed
adding updates
1 parent 48c12ee commit 7db2de1

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from typing import List, Optional
2+
3+
# Definition for a Node.
4+
class Node:
5+
def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None):
6+
self.val = int(x)
7+
self.next = next
8+
self.random = random
9+
10+
11+
class Solution:
12+
def copyRandomList(self, head: 'Optional[Node]') -> 'Optional[Node]':
13+
if not head:
14+
return None
15+
old_to_new = {}
16+
17+
curr = head
18+
while curr:
19+
old_to_new[curr] = Node(curr.val)
20+
curr = curr.next
21+
22+
curr = head
23+
while curr:
24+
old_to_new[curr].next = old_to_new.get(curr.next)
25+
old_to_new[curr].random = old_to_new.get(curr.random)
26+
curr = curr.next
27+
28+
return old_to_new[head]

0 commit comments

Comments
 (0)