Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

184
Visualizações
"TypeError: Cannot set properties of undefined (setting 'next')"

Input: Given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.

Input & Output Illustration (open for understanding)

var addTwoNumbers = function(l1, l2) {
let carry = 0,
    sum = 0;
let runningNode = new ListNode(0, null);
let headNode = runningNode;

while (l1 !== null || l2 !== null) {

    sum = l1 != null ? l1.val : 0 + l2 != null ? l2.val : 0 + carry;
    carry = 0;
    // Error is in below line it states "TypeError: Cannot set properties of undefined (setting 'next')" 
    runningNode.next = ListNode(sum % 10, null)
    runningNode = runningNode.next;
    //How to fix it?
    if (l1) {
        l1.next;
    }
    if (l2) {
        l2.next;
    }
}

if (carry) {
    runningNode.next = ListNode(carry);
}

return headNode;

};

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Several issues:

  • When calling the constructor, new is needed, otherwise this is undefined and references like this.next are invalid. This correction is needed at two places in your code.

  • In the assignment to sum, the + operator has precedence over the ? : operator, but in your case you want it the other way, so you need to add parentheses.

  • The carry is never set to anything else than 0. It should get 1 when the sum is greater than 9.

  • The final result will always return a list that starts with 0, which is a dummy node that head refers to. This makes the result ten times too large. Return the next node.

Corrected code:

var addTwoNumbers = function(l1, l2) {
    let carry = 0,
        sum = 0;
    let runningNode = new ListNode(0, null);
    let headNode = runningNode;
    while (l1 !== null || l2 !== null) {
        // Use parentheses to make sure the addition happens last:
        sum = (l1 != null ? l1.val : 0) + (l2 != null ? l2.val : 0) + carry;
        // Need to set carry
        carry = Math.floor(sum / 10); 
        // Need "new" when calling constructor
        runningNode.next = new ListNode(sum % 10, null); 
        runningNode = runningNode.next;
        // Must assign!
        if (l1) {
            l1 = l1.next;
        }
        if (l2) {
            l2 = l2.next;
        }
    }
    if (carry) {
        // Must call with "new":
        runningNode.next = new ListNode(carry);
    }
    return headNode.next; // skip the zero node
};
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda