-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopyListwithRandomPointer_Day49.py
More file actions
91 lines (73 loc) · 2.53 KB
/
Copy pathCopyListwithRandomPointer_Day49.py
File metadata and controls
91 lines (73 loc) · 2.53 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#Brute Approach
class Solution:
def copyRandomList(self, head: 'Optional[Node]') -> 'Optional[Node]':
if not head:
return None
# Step 1: Store all original nodes in a list
original_nodes = []
curr = head
while curr:
original_nodes.append(curr)
curr = curr.next
# Step 2: Create corresponding new nodes
new_nodes = [Node(node.val) for node in original_nodes]
# Step 3: Link next and random
for i, node in enumerate(original_nodes):
if node.next:
new_nodes[i].next = new_nodes[original_nodes.index(node.next)]
if node.random:
new_nodes[i].random = new_nodes[original_nodes.index(node.random)]
return new_nodes[0]
# Time Complexity: O(N²) (because of .index() lookups)
# Space Complexity: O(N) for storing nodes
#Better Approach
class Solution:
def copyRandomList(self, head: 'Optional[Node]') -> 'Optional[Node]':
if not head:
return None
# Step 1: Map original node to new node
old_to_new = {}
curr = head
while curr:
old_to_new[curr] = Node(curr.val)
curr = curr.next
# Step 2: Assign next and random pointers
curr = head
while curr:
if curr.next:
old_to_new[curr].next = old_to_new[curr.next]
if curr.random:
old_to_new[curr].random = old_to_new[curr.random]
curr = curr.next
return old_to_new[head]
# Time Complexity: O(N)
# Space Complexity: O(N) (hash map)
#Optimal Approach
class Solution:
def copyRandomList(self, head: 'Optional[Node]') -> 'Optional[Node]':
if not head:
return None
# Step 1: Interweave copy nodes
curr = head
while curr:
copy_node = Node(curr.val, curr.next, None)
curr.next = copy_node
curr = copy_node.next
# Step 2: Assign random pointers
curr = head
while curr:
if curr.random:
curr.next.random = curr.random.next
curr = curr.next.next
# Step 3: Separate original and copied list
curr = head
copy_head = head.next
while curr:
copy_node = curr.next
curr.next = copy_node.next
if copy_node.next:
copy_node.next = copy_node.next.next
curr = curr.next
return copy_head
# Time Complexity: O(N)
# Space Complexity: O(1) (excluding output list)