Problem with today's challange Javascript

I created a solution and return a value (for example 8). However the system is receiving it as undefined. What am I doing wrong. My code below

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

/**
 * @param {ListNode} headA
 * @param {ListNode} headB
 * @return {ListNode}
 */
var getIntersectionNode = function(headA, headB) {
    let lengthA = getLength(headA);
    let lengthB = getLength(headB);
    //console.log(`lengthA, lengthB: ${lengthA}, ${lengthB}`);
    
    if(lengthA > lengthB) {
        headA = advanceN(headA, lengthA-lengthB);
    } else {
        headB = advanceN(headB, lengthB-lengthA);
    }
    
    while(headA.val != null) {
        console.log('val now: '+headA.val);
        if(headA == headB) {
            let result = headA.val;
            console.log(`about to return ${result}`);
            return result;
        }
        console.log('advancing 1');        
        headA = headA.next;
        headB = headB.next;
        
    }
    return null;
    

};

var advanceN = function(head, numSteps) {
    for(let step=0; step<numSteps; step++) {
        head = head.next;
        //console.log('moving 1 step, new head: '+head.val);
    }    
    //console.log(`numSteps, newHead: ${numSteps}, ${head.val}`);
    return head;
}



var getLength = function(head) {
    
    let length = 1;
    while(head.next != null) {
        length++;
        head  = head.next;
    }
    //console.log(`length: ${length}`);
    return length;
}

Comments (0)