JSFiddle - React, Tailwind, and code Playground

by Slava Slava

JavaScript

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

class LinkedList {
  constructor(value) {
    const newNode = new Node(value);
    this.head = newNode;
    this.tail = this.head;
    this.length = 1;
  }

  printList() {
    let temp = this.head;
    while (temp !== null) {
      console.log(temp.value);
      temp = temp.next;
    }
  }

  getHead() {
    if (this.head === null) {
      console.log("Head: null");
    } else {
      console.log("Head: " + this.head.value);
    }
  }

  getTail() {
    if (this.tail === null) {
      console.log("Tail: null");
    } else {
      console.log("Tail: " + this.tail.value);
    }
  }

  getLength() {
    console.log("Length: " + this.length);
  }

  makeEmpty() {
    this.head = null;
    this.tail = null;
    this.length = 0;
  }

  push(value) {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
      this.tail = newNode;
    } else {
      this.tail.next = newNode;
      this.tail = newNode;
    }
    this.length++;
    return this;
  }

  pop() {
    if (!this.tail) {
      return undefined
    } else {
      let prev = this.head;
      let temp = this.head;


      while(temp.next) {
				temp = temp.next;
        
        if(temp.next) {
        	prev = temp;
        }
      } 
      
      this.tail = prev;
    }

    /* if(this.length === 1) {
        this.makeEmpty()
    } */

    this.length--

  }

}



let myLinkedList = new LinkedList(1);
myLinkedList.push(2);

// (2) Items in LL - Returns 2 Node
if (myLinkedList.length !== 0) {
  console.log(myLinkedList.pop().value);
} else {
  console.log("null");
}

// (1) Item in LL - Returns 1 Node
if (myLinkedList.length !== 0) {
  console.log(myLinkedList.pop().value);
} else {
  console.log("null");
}

// (0) Items in LL - Returns null
if (myLinkedList.length !== 0) {
  console.log(myLinkedList.pop().value);
} else {
  console.log("null");
}


/*
    EXPECTED OUTPUT:
    ----------------
  ...