Skip to content

Commit a95a19b

Browse files
committed
add a solution and update doc
1 parent 91f343a commit a95a19b

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

Python/copy-list-with-random-pointer.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,25 @@ def copyRandomList(self, head):
5858

5959
return dummy.next
6060

61+
# time: O(n)
62+
# space: O(n)
63+
from collections import defaultdict
6164

65+
66+
class Solution3(object):
67+
def copyRandomList(self, head):
68+
"""
69+
:type head: RandomListNode
70+
:rtype: RandomListNode
71+
"""
72+
clone = defaultdict(lambda: RandomListNode(0))
73+
clone[None] = None
74+
cur = head
75+
76+
while cur:
77+
clone[cur].label = cur.label
78+
clone[cur].next = clone[cur.next]
79+
clone[cur].random = clone[cur.random]
80+
cur = cur.next
81+
82+
return clone[head]

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@
292292
082| [Remove Duplicates from Sorted List II](https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/)| [C++](./C++/remove-duplicates-from-sorted-list-ii.cpp) [Python](./Python/remove-duplicates-from-sorted-list-ii.py) | _O(n)_ | _O(1)_ | Medium ||
293293
083| [Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list/)| [C++](./C++/remove-duplicates-from-sorted-list.cpp) [Python](./Python/remove-duplicates-from-sorted-list.py) | _O(n)_ | _O(1)_ | Easy ||
294294
092| [Reverse Linked List II](https://leetcode.com/problems/reverse-linked-list-ii/)| [C++](./C++/reverse-linked-list-ii.cpp) [Python](./Python/reverse-linked-list-ii.py) | _O(n)_ | _O(1)_ | Medium ||
295-
138| [Copy List with Random Pointer](https://leetcode.com/problems/copy-list-with-random-pointer/) | [C++](./C++/copy-list-with-random-pointer.cpp) [Python](./Python/copy-list-with-random-pointer.py) | _O(n)_ | _O(1)_ | Hard ||
295+
138| [Copy List with Random Pointer](https://leetcode.com/problems/copy-list-with-random-pointer/) | [C++](./C++/copy-list-with-random-pointer.cpp) [Python](./Python/copy-list-with-random-pointer.py) | _O(n)_ | _O(1)_ | Medium ||
296296
160| [Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/)| [C++](./C++/intersection-of-two-linked-lists.cpp) [Python](./Python/intersection-of-two-linked-lists.py) | _O(m + n)_ | _O(1)_ | Easy ||
297297
203| [Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements/)| [C++](./C++/remove-linked-list-elements.cpp) [Python](./Python/remove-linked-list-elements.py) | _O(n)_ | _O(1)_ | Easy ||
298298
206| [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/)| [C++](./C++/reverse-linked-list.cpp) [Python](./Python/reverse-linked-list.py) | _O(n)_ | _O(1)_ | Easy ||

0 commit comments

Comments
 (0)