Skip to content

Instantly share code, notes, and snippets.

@svlapin
Created March 13, 2022 16:46
Show Gist options
  • Save svlapin/6380b2e0c3056cfdb423fbd9e082c4fc to your computer and use it in GitHub Desktop.
Save svlapin/6380b2e0c3056cfdb423fbd9e082c4fc to your computer and use it in GitHub Desktop.
class Solution:
def copyRandomList(self, head: 'Optional[Node]') -> 'Optional[Node]':
old_to_new = {}
def clone_node(node):
if not node:
return None
if node in old_to_new:
return old_to_new[node]
new_node = Node(node.val)
old_to_new[node] = new_node
new_node.random = clone_node(node.random)
new_node.next = clone_node(node.next)
return new_node
return clone_node(head)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment