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

211
Visualizações
How do I dynamically access property accessors in javascript?

How do I dynamically access Property accessors in javascript? Lets say i'm building a linked list. Lets say this list can be any length and it changes dynamically.

var answerList = new ListNode(4);
answerList.next = new ListNode(5);
answerList.next.next = new ListNode(6);
answerList.next.next.next = new ListNode(7);

How can I make this go to the 100th .next without doing:

answerList.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

if you want to insert 100 elements in a LinkedList do

var head = new ListNode(0);
var temp = head; // sets temp to head 
for(let i=1; i<100;i++){
     temp.next = new ListNode(i); // links new nodes to temp
     temp = temp.next;} // at the end head->0->1->2..->98->99
about 4 years ago · Juan Pablo Isaza Relatório

0

You can achieve this using recursion if you pass currLevel and finalLevel as:

function getValue(linkedList, currLevel, finalLevel) {
  if (currLevel === finalLevel) return linkedList?.value;
  return getValue(linkedList.next, currLevel + 1, finalLevel);
}

You have to handle the case where the is not so many node that you passing a number then It could be possible that there won't be .next property on it i.e. means that is the last element in the linked list. one such way is as:

return (linkedList.next && getValue(linkedList.next, currLevel + 1, finalLevel));

class ListNode {
  constructor(val) {
    this.value = val;
    this.next = null;
  }
}

var answerList = new ListNode(4);
answerList.next = new ListNode(5);
answerList.next.next = new ListNode(6);
answerList.next.next.next = new ListNode(7);

function getValue(linkedList, currLevel, finalLevel) {
  if (currLevel === finalLevel) return linkedList?.value;
  return getValue(linkedList.next, currLevel + 1, finalLevel);
}

console.log(getValue(answerList, 0, 3));

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