Skip to content

Instantly share code, notes, and snippets.

@enijkamp
Created July 4, 2017 08:12
Show Gist options
  • Save enijkamp/2d1783ab33470d3bc79cc64ea94f729d to your computer and use it in GitHub Desktop.
Save enijkamp/2d1783ab33470d3bc79cc64ea94f729d to your computer and use it in GitHub Desktop.
class Solution(object):
def copyRandomList(self, head):
if head == None: return None
map_to_copy = {}
node = head
while node != None:
node_copy = RandomListNode(node.label)
map_to_copy[node] = node_copy
node = node.next
node = head
while node != None:
node_copy = map_to_copy[node]
if node.next: node_copy.next = map_to_copy[node.next]
if node.random: node_copy.random = map_to_copy[node.random]
node = node.next
return map_to_copy[head]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment