Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- problem='https://practice.geeksforgeeks.org/problems/clone-a-linked-list-with-next-and-random-pointer/1'
- class Solution:
- def copyList(self, head):
- cur=head
- while cur:
- t=cur.next#next node
- cp=Node(cur.data)#copy node
- cur.next=cp#setting up copied nodes
- cp.next=t
- cur=cp.next
- cur=head
- while cur:#arrange random pointers
- cur.next.arb=cur.arb.next if cur.arb else None
- cur=cur.next.next if cur.next else None
- cur=head
- t=ans=head.next
- while cur:#setup ans chain
- cur.next=cur.next.next if cur.next else None
- t.next=t.next.next if t.next else None
- cur,t=cur.next,t.next
- return ans
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement