Skip to content

Instantly share code, notes, and snippets.

@jdgo-mars
Created April 21, 2019 15:30
Show Gist options
  • Save jdgo-mars/34c4c48e3b76d74d7071a747b56cdbef to your computer and use it in GitHub Desktop.
Save jdgo-mars/34c4c48e3b76d74d7071a747b56cdbef to your computer and use it in GitHub Desktop.
class LinkList {
constructor(value) {
this._head = { value, next: null };
this._tail = this._head;
}
insert(value) {
//update tail as needed
const node = { value, next: null };
this._tail.next = node;
this._tail = node;
}
removeTail() {
let currentNode = this._head;
// if we reach the 2nd last node stop
while (currentNode.next != this._tail) {
// we assign the next to the currentNode
currentNode = currentNode.next;
}
// reset next node to null
currentNode.next = null;
// tail is now the 2nd latest node
this._tail = currentNode;
}
isHead() {}
isTail() {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment