Created
March 13, 2022 16:46
-
-
Save svlapin/6380b2e0c3056cfdb423fbd9e082c4fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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