Created
June 18, 2020 20:32
-
-
Save tjstalcup/ed867de07a2955cd59ace9bf76085f50 to your computer and use it in GitHub Desktop.
Singly Linked List ES6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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