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

250
Visualizações
how Variable assignment work in javascript?
class Node{
    constructor(val) {
        this.val = val;
        this.next = null;
    }
}

class SinglyLingkedList{
    constructor() {
        this.head = null;
        this.tail = null;
        this.length = 0;
    }

    push(val){
        var newNode = new Node(val)
        if (this.head == null) {
            this.head = newNode;
            this.tail = this.head;
        } else{
            this.tail.next = newNode;
            this.tail = newNode;
        }
        
        this.length++;
        return this
    }

    reverse(){
        var node = this.head;
        this.head.next = null;
        this.tail = this.head;
        return node;
    }
}

var list = new SinglyLingkedList()
list.push("1")
list.push("2")
list.push("3")
list.reverse()

I'm new at programming. I'm pretty confused with my variable assignment in the reverse method, especially at

        var node = this.head;
        this.head.next = null;
        this.tail = this.head;
        return node;

Why return node is affected by this.head.next null? is not like what I expected its return

Node : {val: '1', next: null}

not

Node : {val: '1', next: Node}

I wanted to know why it happened?

about 4 years ago · Juan Pablo Isaza
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