File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 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 )
You can’t perform that action at this time.
0 commit comments