Why Map will work but HashMap not????? If you change map to hashmap, the code doesn't work1

  • 0
    public class Solution {
    Map<Integer, RandomListNode> map = new HashMap<>();
    public RandomListNode copyRandomList(RandomListNode head) {
        if(head == null){
            return null;
            return map.get(head.label);
        RandomListNode node = new RandomListNode(head.label);
        map.put(node.label, node);
        node.next = copyRandomList(head.next);
        node.random = copyRandomList(head.random);
        return node;


  • 0

    However, I don't think your code works, since the question did't say the label is unique, what if the label is duplicate but they are actually two different node (i.e. different memory address).

Log in to reply

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