Cannot read property 'next' of null


  • 0
    H

    why it comed to Runtime Error Message:
    Line 20: TypeError: Cannot read property 'next' of null

    /**
     * Definition for singly-linked list.
     * function ListNode(val) {
     *     this.val = val;
     *     this.next = null;
     * }
     */
    /**
     * @param {ListNode} l1
     * @param {ListNode} l2
     * @return {ListNode}
     */
    var addTwoNumbers = function (l1, l2) {
        var l3 = new ListNode();
        var node = l3;
        var sum = 0;
        while (l1 || l2 || sum !== 0) {
            sum += (l1 ? l1.val : 0) + (l2 ? l2.val : 0);
            node.val = sum % 10;
            l1 = l1.next ? l1.next : null;
            l2 = l2.next ? l2.next : null;
            sum = Math.floor(sum / 10);
            if (l1 || l2 || sum !== 0) {
                node.next = new ListNode();
                node = node.next;
            }
        }
        return l3;
    };

Log in to reply
 

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