Skip to content

Instantly share code, notes, and snippets.

@tjstalcup
Created June 18, 2020 20:32
Show Gist options
  • Save tjstalcup/ed867de07a2955cd59ace9bf76085f50 to your computer and use it in GitHub Desktop.
Save tjstalcup/ed867de07a2955cd59ace9bf76085f50 to your computer and use it in GitHub Desktop.
Singly Linked List ES6
class LinkedList {
this.head = null;
push = val => {
const node = {
value: val,
next: null
}
if(!this.head){
this.head = node;
}
else{
current = this.head;
while(current.next){
current = current.next;
}
current.next = node;
}
}
}
const sll = new LinkedList();
//push node
sll.push(2);
sll.push(3);
sll.push(4);
//check values by traversing LinkedList
sll.head; //Object {value: 2, next: Object}
sll.head.next; //Object {value: 3, next: Object}
sll.head.next.next; //Object {value: 4, next: null}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment