Skip to content

Instantly share code, notes, and snippets.

@junminstorage
Created July 27, 2016 02:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save junminstorage/5e408c5b04847b28c30091535762afdd to your computer and use it in GitHub Desktop.
Save junminstorage/5e408c5b04847b28c30091535762afdd to your computer and use it in GitHub Desktop.
public RandomListNode copyRandomList(RandomListNode node) {
return copyHelper(node, new HashMap<>());
}
public static RandomListNode copyHelper(RandomListNode head, Map<Integer, RandomListNode> hm) {
if (head == null) {
return null;
}
if (hm.containsKey(head.label)) {
return hm.get(head.label);
}
RandomListNode node = new RandomListNode(head.label);
hm.put(node.label, node);
node.next = copyHelper(head.next, hm);
node.random = copyHelper(head.random, hm);
return node;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment