Similar to "Graph Clone", recursive JAVA solution

  • 3

    This is another deep clone problem for list, use a map to store its original and its new copy, recursive java solution:

    Map<RandomListNode, RandomListNode> map = new HashMap<>();
        public RandomListNode copyRandomList(RandomListNode head) {
            if (head == null) {
                return null;
            if (map.containsKey(head)) {
                return map.get(head);
            RandomListNode result = new RandomListNode(head.label);
            map.put(head, result);
   = copyRandomList(;
            result.random = copyRandomList(head.random);
            return result;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.