Code runs fine with all my tests, why am I getting this error? (I notated everything)


  • 0
    O
    var mergeKLists = function(lists) {
        var answer = new ListNode(-Infinity);
        var pointer = answer;  //pointer so we can assign to last .next in our answer at all times
        lists.forEach(function(element, i){
            if (element === null || element.val === undefined){
                lists.splice(i,1);
                i--;
            }
        }) //remove all lists that don't contain a number from our lists array
        //here is a function to return the index in our lists array of the minimum value list
        var minIndex = function(lists){ // 
            var minimumIndex = 0;
            var length = lists.length
            for (var i = 1; i < length; i++){ 
                if (lists[i].val < lists[minimumIndex].val){//this is line 36 that the error refers to
                    minimumIndex = i;
                }
            }
            return minimumIndex;
        }
        
        var currentMinIndex;
        while (lists.length > 1){ 
          currentMinIndex = minIndex(lists); // find index of minimum value list
          pointer.next = lists[currentMinIndex]; //.next of our pointer points to this list
          lists[currentMinIndex] = lists[currentMinIndex].next; //the list identifier we chose points to the                                                     next sublist 
          if (lists[currentMinIndex] === null){ //if this new value is null, we eliminate this list from our                                     lists array as it is no longer needed
              lists.splice(currentMinIndex, 1);
          }
          pointer = pointer.next;               // pointer moves down our answer
        }
        pointer.next = lists[0];                // when only 1 list remains in list array, it is assigned to                                        the rest of our answer list
        return answer.next;                     //  return the part of answer comprised of lists, since the                                   first value (-Infinity)  was only created as a starting point
    };
    

    error: Runtime Error Message:
    Line 36: TypeError: Cannot read property 'val' of null
    Last executed input:
    [[0,1,2],[-10,-8,-5,-4],[],[],[-3],[-10,-9,-6,-4,-3,-2,-2,-1,2],[-9,-9,-2,-1,0,1],[-9,-4,-3,-2,2,2,3,3,4]]


Log in to reply
 

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