-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcopy-list-with-random-pointer.py
More file actions
31 lines (25 loc) · 958 Bytes
/
copy-list-with-random-pointer.py
File metadata and controls
31 lines (25 loc) · 958 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
26
27
28
29
30
31
from typing import Optional
# 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:
# Time complexity: O(n) where n is the length of the linked list
# Space complexity: O(n)
def copyRandomList(self, head: Optional[Node]) -> Optional[Node]:
dummy_head = curr = Node(0)
dic = {}
curr_original = head
while curr_original:
curr.next = Node(curr_original.val)
curr = curr.next
dic[curr_original] = curr
curr_original = curr_original.next
curr_original = head
while curr_original:
if curr_original.random:
dic[curr_original].random = dic[curr_original.random]
curr_original = curr_original.next
return dummy_head.next