- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopyListWithRandom.py
More file actions
Latest commit
25 lines (21 loc) · 724 Bytes
/
Copy pathcopyListWithRandom.py
File metadata and controls
25 lines (21 loc) · 724 Bytes
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
importcollections
classRandomListNode: def__init__(self, x): self.label=xself.next=Noneself.random=None
classSolution:
defcopyRandomList(self, head):
ref=head
check=collections.defaultdict()
check[None] =None
ifnothead: returnNone
whilehead: check[head] =RandomListNode(head.label) head=head.nexthead=ref
whilehead: check[head].next=check[head.next] head=head.next
head=ref
whilehead: check[head].random=check[head.random] head=head.nextreturncheck[ref]
defprintlist(head):
whilehead:
printhead.label
head=head.next
n=RandomListNode(1)n2=RandomListNode(2)
n2=RandomListNode()
n.next=n2a=Solution()
b=a.copyRandomList(n)
printlist(b)