Why am I getting runtime error??

  • 0

    Here is my code. I am wondering why do I get stackoverflow exception...
    It's weird. If I move the code inside copy(){..} to copyRandomList(){..}, it passes OJ. I think they are totally same code, but this code does not work. Could anybody help me with this.

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

Log in to reply

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