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

248
Visualizações
I am passing Object as refrence to a function and doing some changes, but object remains unchanged

I am implementing an Linked List and inside a function I am changing the head with some other element of Linked List, which appears proper inside that function when I console.log but outside that function when I console.log and iteratively print the data value, the head remains unchanged and the element with which I changed the head doesn't print

## My implementation of linked list
export class Node<T = number> {
  public get data(): T {
    return this._data;
  }

  public set data(v: T) {
    this._data = v;
  }

  public next: Node<T> | null = null;
  constructor(private _data: T) {}
}

export default class LinkedList<T = number> implements ILinkedList<T> {
  static fromArray<T>(array: Array<T>): LinkedList<T> {
    const ll = new LinkedList<T>();
    for (let i = array.length - 1; i >= 0; i--) {
      ll.insertInBegin(array[i]);
    }
    return ll;
  }

  print(): void {
    let current = this.head;
    while (current.next !== null) {
      console.log(current.data);
      current = current.next;
    }
    console.log(current.data);
  }
  public head: Node<T> | null = null;
  #size: number = 0;

  public get size(): number {
    return this.#size;
  }

  insertInBegin(data: T): Node<T> {
    const newNode = new Node(data);
    newNode.next = this.head;
    this.head = newNode;
    this.#size++;
    return newNode;
  }
  
}

### This is what i want to do
function partition(head: Node | null, x: number): Node | null {
  let current = head.next;
  head.next = current.next;
  current.next = head;
  head = current;
  console.log('Inside parititon:head', head);
  // Inside parititon:head Node {
  //   _data: 4,
  //   next: Node { _data: 7, next: Node { _data: 3, next: [Node] } }
  // }
  return head;
}
const a = LinkedList.fromArray([7, 4, 3, 2, 5, 2]);
console.log('Before partition');
// Before partition
// 7
// 4
// 3
// 2
// 5
// 2
a.print();
partition(a.head, 3);
console.log('Outside head:', a.head);
// Outside head: Node {
//   _data: 7,
//   next: Node { _data: 3, next: Node { _data: 2, next: [Node] } }
// }
a.print();
// 7
// 3
// 2
// 5
// 2
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