Help!!! Why my solution got StackOverflowError??


  • 0
    I
    public class Solution {
        HashMap<RandomListNode,RandomListNode> hm = new HashMap<RandomListNode,RandomListNode>();
        public RandomListNode copyRandomList(RandomListNode head) {
            if(head == null) return head;     
            RandomListNode cur = copyNode(head);
           
            return cur;
        }
        private RandomListNode copyNode(RandomListNode head){
            if(head == null) return null;
            if(hm.containsKey(head)) return hm.get(head);
            RandomListNode n = new RandomListNode(head.label);
            hm.put(head,n);
            n.next = copyNode(head.next);
            n.random = copyNode(head.random);
           
            return n;
        }
        
    }

  • 0
    D

    You may forget the case of rotated list. Originally I forgot this case either.


Log in to reply
 

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