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

  • 0
    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){
        }) //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
 = 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 moves down our answer
        } = lists[0];                // when only 1 list remains in list array, it is assigned to                                        the rest of our answer list
        return;                     //  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:

Log in to reply

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