-
Notifications
You must be signed in to change notification settings - Fork 43
/
copy-list-with-random-pointer.py
281 lines (241 loc) · 8.45 KB
/
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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
"""
A linked list of length n is given such that each node contains an additional random pointer, which could point to any node in the list, or null.
Construct a deep copy of the list. The deep copy should consist of exactly n brand new nodes, where each new node has its value set to the value of its corresponding original node. Both the next and random pointer of the new nodes should point to new nodes in the copied list such that the pointers in the original list and copied list represent the same list state. None of the pointers in the new list should point to nodes in the original list.
For example, if there are two nodes X and Y in the original list, where X.random --> Y, then for the corresponding two nodes x and y in the copied list, x.random --> y.
Return the head of the copied linked list.
The linked list is represented in the input/output as a list of n nodes. Each node is represented as a pair of [val, random_index] where:
val: an integer representing Node.val
random_index: the index of the node (range from 0 to n-1) that the random pointer points to, or null if it does not point to any node.
Your code will only be given the head of the original linked list.
Example 1:
Input: head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
Output: [[7,null],[13,0],[11,4],[10,2],[1,0]]
Example 2:
Input: head = [[1,1],[2,1]]
Output: [[1,1],[2,1]]
Example 3:
Input: head = [[3,null],[3,0],[3,null]]
Output: [[3,null],[3,0],[3,null]]
Example 4:
Input: head = []
Output: []
Explanation: The given linked list is empty (null pointer), so return null.
Constraints:
0 <= n <= 1000
-10000 <= Node.val <= 10000
Node.random is null or is pointing to some node in the linked list.
"""
# V0
# IDEA :
# step 1) make 2 objects (m, n) refer to same instance (head)
# step 2) go through m, and set up the dict
# step 3) go through n, and get the random pointer via the dict we set up in step 2)
class Node(object):
def __init__(self, val, next, random):
self.val = val
self.next = next
self.random = random
class Solution:
def copyRandomList(self, head):
dic = dict()
### NOTE : make m, and n refer to same instance (head)
m = n = head
while m:
### NOTE : the value in dict is Node type (LinkedList)
dic[m] = Node(m.val)
m = m.next
while n:
dic[n].next = dic.get(n.next)
dic[n].random = dic.get(n.random)
n = n.next
return dic.get(head)
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/80787528
"""
# Definition for a Node.
class Node(object):
def __init__(self, val, next, random):
self.val = val
self.next = next
self.random = random
"""
class Solution(object):
def copyRandomList(self, head):
"""
:type head: Node
:rtype: Node
"""
nodeDict = dict()
dummy = Node(0, None, None)
nodeDict[head] = dummy
newHead, pointer = dummy, head
while pointer:
node = Node(pointer.val, pointer.next, None)
nodeDict[pointer] = node
newHead.next = node
newHead, pointer = newHead.next, pointer.next
pointer = head
while pointer:
if pointer.random:
nodeDict[pointer].random = nodeDict[pointer.random]
pointer = pointer.next
return dummy.next
### Test case : dev
# V1'
# http://bookshadow.com/weblog/2015/07/31/leetcode-copy-list-random-pointer/
class Solution:
# @param head, a RandomListNode
# @return a RandomListNode
def copyRandomList(self, head):
nodeDict = dict()
dummy = RandomListNode(0)
pointer, newHead = head, dummy
while pointer:
newNode = RandomListNode(pointer.label)
nodeDict[pointer] = newHead.next = newNode
newHead, pointer = newHead.next, pointer.next
pointer, newHead = head, dummy.next
while pointer:
if pointer.random:
newHead.random = nodeDict[pointer.random]
pointer, newHead = pointer.next, newHead.next
return dummy.next
# V1''
# https://leetcode.com/problems/copy-list-with-random-pointer/discuss/43485/Clear-and-short-python-O(2n)-and-O(n)-solution
# Definition for a Node.
class Node(object):
def __init__(self, val, next, random):
self.val = val
self.next = next
self.random = random
class Solution:
def copyRandomList(self, head):
dic = dict()
m = n = head
while m:
dic[m] = Node(m.val)
m = m.next
while n:
dic[n].next = dic.get(n.next)
dic[n].random = dic.get(n.random)
n = n.next
return dic.get(head)
# V1'''
# https://leetcode.com/problems/copy-list-with-random-pointer/discuss/43485/Clear-and-short-python-O(2n)-and-O(n)-solution
class Node(object):
def __init__(self, val, next, random):
self.val = val
self.next = next
self.random = random
class Solution:
def copyRandomList(self, head):
dic = collections.defaultdict(lambda: Node(0))
dic[None] = None
n = head
while n:
dic[n].val = n.val
dic[n].next = dic[n.next]
dic[n].random = dic[n.random]
n = n.next
return dic[head]
# V1'''''
# https://www.jiuzhang.com/solution/copy-list-with-random-pointer/#tag-highlight-lang-python
class Solution:
# @param head: A RandomListNode
# @return: A RandomListNode
def copyRandomList(self, head):
# write your code here
if head == None:
return None
myMap = {}
nHead = RandomListNode(head.label)
myMap[head] = nHead
p = head
q = nHead
while p != None:
q.random = p.random
if p.next != None:
q.next = RandomListNode(p.next.label)
myMap[p.next] = q.next
else:
q.next = None
p = p.next
q = q.next
p = nHead
while p!= None:
if p.random != None:
p.random = myMap[p.random]
p = p.next
return nHead
# V2
# Time: O(n)
# Space: O(1)
class RandomListNode(object):
def __init__(self, x):
self.label = x
self.next = None
self.random = None
class Solution(object):
# @param head, a RandomListNode
# @return a RandomListNode
def copyRandomList(self, head):
# copy and combine copied list with original list
current = head
while current:
copied = RandomListNode(current.label)
copied.next = current.__next__
current.next = copied
current = copied.__next__
# update random node in copied list
current = head
while current:
if current.random:
current.next.random = current.random.__next__
current = current.next.__next__
# split copied list from combined one
dummy = RandomListNode(0)
copied_current, current = dummy, head
while current:
copied_current.next = current.__next__
current.next = current.next.__next__
copied_current, current = copied_current.__next__, current.__next__
return dummy.__next__
# V3
# Time: O(n)
# Space: O(n)
class Solution2(object):
# @param head, a RandomListNode
# @return a RandomListNode
def copyRandomList(self, head):
dummy = RandomListNode(0)
current, prev, copies = head, dummy, {}
while current:
copied = RandomListNode(current.label)
copies[current] = copied
prev.next = copied
prev, current = prev.__next__, current.__next__
current = head
while current:
if current.random:
copies[current].random = copies[current.random]
current = current.__next__
return dummy.__next__
# V4
# time: O(n)
# space: O(n)
from collections import defaultdict
class Solution3(object):
def copyRandomList(self, head):
"""
:type head: RandomListNode
:rtype: RandomListNode
"""
clone = defaultdict(lambda: RandomListNode(0))
clone[None] = None
cur = head
while cur:
clone[cur].label = cur.label
clone[cur].next = clone[cur.__next__]
clone[cur].random = clone[cur.random]
cur = cur.__next__
return clone[head]