[object Object] for Copy List with Random Pointer result? Where is the problem?


  • 0
    B

    I have been debugging this issue for a while but couldn't get it. Can someone help me figure it out? Thanks.

    Input:	{-1,#}
    Output:	[object Object]
    Expected:	{-1,#}
    

    Here is my code:

    function RandomListNode(label) {
        this.label = label;
        this.next = this.random = null;
    }
    
    var copyRandomList = function(head) {
      if(!head) return null;
      var newheadbefore = new RandomListNode(0);
      var map = {};
      var current = head;
      var newpointer = newheadbefore;
    
      while(current !== null){
        var newNode = new RandomListNode(current.label);
        map[current] = newNode;
        newpointer.next = newNode;
        newpointer = newpointer.next;
        current = current.next;
      }
      current = head;
      newpointer = newheadbefore.next;
    
      while(current !== null){
        var randomNode = map[current.random];
        newpointer.random = randomNode;
        newpointer = newpointer.next;
        current = current.next;
      }
    
      return newheadbefore.next;
    };
    

    09/02 Updated:

     var copyRandomList = function(head) {
      if(!head) return null;
      var newheadbefore = new RandomListNode(0);
      var map = new Map();
      var current = head;
      var newpointer = newheadbefore;
    
      while(current !== null){
        var newNode = new RandomListNode(current.label);
        var key = JSON.stringify(current);
        map.set(key, newNode);
        newpointer.next = newNode;
        newpointer = newpointer.next;
        current = current.next;
      }
      current = head;
      newpointer = newheadbefore.next;
    
      while(current !== null){
        var randomKey = current.random? current.random.label: null;
        var randomNode = map.get(randomKey);
        newpointer.random = randomNode;
        newpointer = newpointer.next;
        current = current.next;
      }
    
      return newheadbefore.next;
    };

  • 0
    E

    In JS the KEYs are Strings. So all of your RandomListNode object will be converted to "[object Object]" as your map's key.


  • 0
    B

    I understand object's key in JS is String, but I'm using old node as keys and store new node as values. In the end, I return newheadbefore.next, which is from the value. How can I return the right format Leetcode want? I'm confused.


  • 0
    F

    I am experiencing the same issue with JS. I am aware of that js objects can only have string keys so I used Map object from ES6.

    Edit: It appears that you have to use "new RandomListNode" to create new nodes instead of a simple "{}".


  • 0

    The RandomListNode class (or function in JS) is already defined for you, you should not define it yourself.


  • 0
    B

    Thanks for letting me know you have the similar issue. May I ask how did you manage to pass it? I used Map object from ES6 and my code still has runtime error. You can see my updated code above. Thanks.


  • 0
    F

    Maybe because you use "JSON.stringify(current)" as key to store objects and then use "current.random? current.random.label: null" to get the value?


Log in to reply
 

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