Skip to content

Commit 8de52d4

Browse files
committed
feat(leetcode): add No.138
1 parent 3f1b6c5 commit 8de52d4

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# https://leetcode.com/problems/copy-list-with-random-pointer/description/
2+
#
3+
# algorithms
4+
# Medium (25.56%)
5+
# Total Accepted: 195.5K
6+
# Total Submissions: 765K
7+
# beats 100.0% of python submissions
8+
9+
# Definition for singly-linked list with a random pointer.
10+
class RandomListNode(object):
11+
def __init__(self, x):
12+
self.label = x
13+
self.next = None
14+
self.random = None
15+
16+
17+
class Solution(object):
18+
def copyRandomList(self, head):
19+
dic = {}
20+
m = n = head
21+
while m: # copy node itself
22+
dic[m] = RandomListNode(m.label)
23+
m = m.next
24+
while n: # link the node and copy the random pointer of the current node
25+
dic[n].next = dic.get(n.next)
26+
dic[n].random = dic.get(n.random)
27+
n = n.next
28+
return dic.get(head)

0 commit comments

Comments
 (0)