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

168
Visualizações
Why set head and tail as null if the list length is 1

I single linked list, we set head and tail as null if the list length is 0. i.e poping the last element would make head and tail zero.

  pop() {
    if (!this.head) return undefined;
    var current = this.head;
    var newTail = current; //newTail is the previous node of current
    while (current.next) {
      newTail = current;
      current = current.next;
    }
    this.tail = newTail;
    this.tail.next = null;
    this.length--;
    //If every items is poped out i.e we have zero items then make head and tail null
    if (this.length === 0) {
      this.head = null;
      this.tail = null;
    }
    return current;
  }

But in a doubly linked list why are we setting the head and tail as null for list length 1 instead of 0.

  pop() {
    if (!this.head) return undefined;
    var poppedNode = this.tail;
    if (this.length === 1) {
      this.head = null;
      this.tail = null;
    } else {
      this.tail = poppedNode.prev;
      this.tail.next = null;
      poppedNode.prev = null;
    }
    this.length--;
    return poppedNode;
  }

Shouldn't the above also set head and tail to null when length is zero ?

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

0

One of the differences in these two implementations is that the first checks the value of this.length before reducing it with 1, while the other does this after reducing it with 1. That explains why the first checks against 1 while the other checks against 0. But that has nothing to do with the difference between singly and doubly linked lists. It could have been the other way around.

We could harmonize the two pieces of code, so they only differ where necessary:

Singly Linked List

pop() {
    if (!this.head) return undefined;
    var poppedNode = this.tail;

    // Specific for singly linked list:
    let newTail = null;
    let current = this.head;
    while (current.next != null) {
        newTail = current;
        current = current.next;
    }
    this.tail = newTail;
    // End of specific part

    this.length--;
    if (this.length === 0) {
        this.head = null;
    } else {
        this.tail.next = null;
    }
    return poppedNode;
}

Doubly Linked List

pop() {
    if (!this.head) return undefined;
    var poppedNode = this.tail;

    // Specific for doubly linked list:
    this.tail = this.tail.prev;
    poppedNode.prev = null;
    // End of specific part

    this.length--;
    if (this.length === 0) {
        this.head = null;
    } else {
        this.tail.next = null;
    }
    return poppedNode;
}
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