Last active
February 2, 2026 11:14
-
-
Save Daftscientist/268ef862d42c4377765e1999cc86ddfb to your computer and use it in GitHub Desktop.
LinkedList Implementation JavaScript
This file contains hidden or 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 InternalNode { | |
| constructor(data) { | |
| this.data = data; | |
| this.next = null; | |
| }; | |
| } | |
| class LinkedList { | |
| constructor() { | |
| this.head = null; | |
| this.tail = null; | |
| this.size = 0; | |
| } | |
| // General | |
| isEmpty() { | |
| return this.size === 0; | |
| }; | |
| getSize() { | |
| return this.size; | |
| }; | |
| clear() { | |
| this.head = null; | |
| this.tail = null; | |
| this.size = 0; | |
| return; | |
| }; | |
| // Adding Nodes | |
| prepend(data) { | |
| const toPrepend = new InternalNode(data); | |
| toPrepend.next = this.head; | |
| this.head = toPrepend; | |
| if (this.size == 0) this.tail = toPrepend; | |
| this.size++; | |
| return; | |
| }; | |
| append(data) { | |
| const toAppend = new InternalNode(data); | |
| if (this.isEmpty()) { | |
| this.head = toAppend; | |
| this.tail = toAppend; | |
| } else { | |
| this.tail.next = toAppend; | |
| this.tail = toAppend; | |
| }; | |
| this.size++; | |
| return; | |
| }; | |
| insertAt(index, data) { | |
| if (index < 0 || index > this.size) return; | |
| if (index === 0) this.prepend(data); | |
| if (index === this.size) this.append(data); | |
| const newNode = new InternalNode(data); | |
| let current = this.head; | |
| for (let i = 0; i < index - 1; i++) { | |
| current = current.next | |
| }; | |
| newNode.next = current.next; | |
| current.next = newNode; | |
| this.size++; | |
| return; | |
| }; | |
| // Removing Nodes | |
| removeFirst() { | |
| if (this.isEmpty()) return null; | |
| const toRemove = this.head.data; | |
| this.head = this.head.next; | |
| this.size--; | |
| if (this.size === 0) this.tail = null; | |
| return toRemove; | |
| }; | |
| removeLast() { | |
| if (this.isEmpty()) return null; | |
| if (this.size === 1) { | |
| const toRemove = this.head.data; | |
| this.head = null; | |
| this.tail = null; | |
| this.size = 0; | |
| return toRemove; | |
| }; | |
| let current = this.head; | |
| while (current.next !== this.tail) { | |
| current = current.next; | |
| }; | |
| const toRemove = this.tail.data; | |
| current.next = null; | |
| this.tail = current; | |
| this.size--; | |
| return toRemove; | |
| }; | |
| removeAt(index) { | |
| if (index < 0 || index >= this.size) return null; | |
| if (index === 0) return this.removeFirst(); | |
| if (index === this.size - 1) return this.removeLast(); | |
| let current = this.head; | |
| for (let i = 0; i < index - 1; i++) { | |
| current = current.next; | |
| }; | |
| const toRemove = current.next.data; | |
| current.next = current.next.next; | |
| this.size--; | |
| return toRemove; | |
| }; | |
| removeByValue(value) { | |
| if (this.isEmpty()) return; | |
| const doesContain = this.indexOf(value); | |
| if (!doesContain) return false; | |
| this.removeAt(doesContain); | |
| return doesContain; | |
| }; | |
| // Accessing Nodes | |
| get(index) { | |
| if (index < 0 || index >= this.size) return false; | |
| let current = this.head; | |
| for (let i = 0; i < index; i++) { | |
| current = current.next; | |
| }; | |
| return current.data; | |
| }; | |
| set(index, data) { | |
| if (index < 0 || index >= this.size) return false; | |
| let current = this.head; | |
| for (let i = 0; i < index; i++) { | |
| current = current.next; | |
| }; | |
| current.data = data; | |
| return current.data; | |
| }; | |
| contains(value) { | |
| if (this.isEmpty()) return; | |
| let found = false; | |
| this.forEach((data, index) => { | |
| if (data === value) found = true; | |
| }); | |
| return found; | |
| }; | |
| indexOf(value) { | |
| if (this.isEmpty()) return; | |
| let foundIndex = -1; | |
| this.forEach((data, index) => { | |
| if (data === value && foundIndex === -1) foundIndex = index; | |
| }); | |
| return foundIndex; | |
| }; | |
| // Output | |
| print() { | |
| let formationStr = String(); | |
| this.forEach((data, index) => { | |
| formationStr += data; | |
| if (index < this.getSize() - 1) { | |
| formationStr += ' -> '; | |
| }; | |
| }); | |
| console.log(formationStr); | |
| return formationStr; | |
| }; | |
| toArray() { | |
| let arr = Array(); | |
| this.forEach((data, index) => { | |
| arr.push(data); | |
| }); | |
| return arr; | |
| }; | |
| forEach(callback) { | |
| let current = this.head; | |
| let index = 0 | |
| while (current) { | |
| callback(current.data, index); | |
| current = current.next; | |
| index++; | |
| } | |
| return; | |
| }; | |
| // Utility | |
| reverse() { | |
| let prev; | |
| let current = this.head; | |
| this.tail = this.head; | |
| while (current) { | |
| let nextNode = current.next; | |
| current.next = prev; | |
| prev = current; | |
| current = nextNode; | |
| }; | |
| this.head = prev; | |
| }; | |
| clone() { | |
| const newLL = new LinkedList(); | |
| this.forEach((data, index) => newLL.append(data)); | |
| return newLL; | |
| }; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment