-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path138.copy-list-with-random-pointer.py
45 lines (40 loc) · 1.26 KB
/
138.copy-list-with-random-pointer.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
34
35
36
37
38
39
40
41
42
43
44
#
# @lc app=leetcode id=138 lang=python3
#
# [138] Copy List with Random Pointer
#
# @lc code=start
# TAGS: Hash Table, Linked List
"""
# 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:
"""
O(1) Space Solution: https://leetcode.com/problems/copy-list-with-random-pointer/discuss/43491/A-solution-with-constant-space-complexity-O(1)-and-linear-time-complexity-O(N)
3 Step:
- Create temp nodes between original nodes
- Assign the pointers for random because we already know where they are
- Seperate temp and original nodes and return the temp nodes
"""
# 40 ms, 36.34%. Time and Space: O(N)
def copyRandomList(self, head: 'Node') -> 'Node':
ref = {}
root = temp = Node(0)
node = head
while node:
temp.next = Node(node.val)
ref[node] = temp.next
temp = temp.next
node = node.next
node = head
while node:
if node.random:
ref[node].random = ref[node.random]
node = node.next
return root.next
# @lc code=end