Quick actions

cmd+k|ctrl+k

Navigation

Languages

linked list implementation

Snippet info

Language

TypeScript

Visibility

public

Author

gigi-bigi

Created

2025-03-22T09:01:58.841906Z

Updated

2025-03-22T09:03:59.033601Z

type Node = {
  value: number;
  next: Node | null;
};

export class LinkedList {
  private head: Node;
  private tail: Node;
  private length;

  constructor(value: number) {
    this.head = {
      value,
      next: null,
    };

    this.tail = this.head;
    this.length = 1;
  }

  append(value: number) {
    const newNode = {
      value,
      next: null,
    };
    let next = this.getNext(this.head);
    
    console.log('next', next);

    next = newNode;
    this.length++;
    this.tail = next;
  }

  private getNext(head: Node | null) {
    const next = head?.next;
    if (next && next !== null) {
      this.getNext(head?.next);
    }

    return next;
  }
}

// 10 --> 5 --> 16

/**
  let myLinkedList = {
    head: {
      value: 10,
      next: {
        value: 5,
        next: {
          value: 16,
          next: null
        }
      }
    }
  }
 */

const myLinkedList = new LinkedList(10);
myLinkedList.append(5);
myLinkedList.append(16);
console.log(myLinkedList);
INFO