Created
July 27, 2016 02:11
-
-
Save junminstorage/5e408c5b04847b28c30091535762afdd 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
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