From fef709ee834f67d489cb6dbe5d599a3b6b3f3e5f Mon Sep 17 00:00:00 2001 From: Vardaan Raj Singh <53212464+vardaan-raj@users.noreply.github.com> Date: Mon, 4 Oct 2021 11:15:27 +0530 Subject: [PATCH] Create CloneLinkedListRandomPointer.py --- CloneLinkedListRandomPointer.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 CloneLinkedListRandomPointer.py diff --git a/CloneLinkedListRandomPointer.py b/CloneLinkedListRandomPointer.py new file mode 100644 index 0000000..571e896 --- /dev/null +++ b/CloneLinkedListRandomPointer.py @@ -0,0 +1,21 @@ +class Solution: + def copyRandomList(self, head: 'Node') -> 'Node': + if not head: + return None + hash_map={} + temp=head + while temp: + hash_map[temp]=Node(temp.val) + temp=temp.next + temp=head + while temp: + if not temp.next: + hash_map[temp].next=None + else: + hash_map[temp].next=hash_map[temp.next] + if temp.random==None : + hash_map[temp].random=None + else: + hash_map[temp].random=hash_map[temp.random] + temp=temp.next + return hash_map[head]