Created
December 21, 2021 21:02
-
-
Save mustafadalga/c8d8f63d628f96456b94abd2ff6f3e0e to your computer and use it in GitHub Desktop.
Singly Linked Lists
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 Node { | |
constructor(val) { | |
this.val = val; | |
this.next = null; | |
} | |
} | |
class SinglyLinkedList { | |
constructor() { | |
this.head = null; | |
this.tail = null; | |
this.length = 0; | |
} | |
push(val) { | |
const newNode = new Node(val); | |
if (!this.head) { | |
this.head = newNode; | |
this.tail = this.head; | |
} else { | |
this.tail.next = newNode; | |
this.tail = newNode | |
} | |
this.length++; | |
return this; | |
} | |
pop() { | |
if (!this.head) return undefined; | |
let current = this.head; | |
let newTail = current; | |
while (current.next) { | |
newTail = current; | |
current = current.next; | |
} | |
this.tail = newTail; | |
this.tail.next = null; | |
this.length--; | |
if (this.length == 0) { | |
this.head = null; | |
this.tail = null; | |
} | |
return current; | |
} | |
shift() { | |
if (!this.head) return undefined; | |
let currentHead = this.head; | |
this.head = this.head.next; | |
this.length--; | |
if (this.length == 0) { | |
this.tail = null; | |
} | |
return currentHead; | |
} | |
unshift(val) { | |
const newNode = new Node(val); | |
if (this.head) { | |
newNode.next = this.head; | |
} else { | |
this.tail = newNode; | |
} | |
this.head = newNode; | |
this.length++; | |
return this; | |
} | |
get(index) { | |
if (index < 0 || index >= this.length) return undefined; | |
let count = 0; | |
let current = this.head; | |
while (count < index) { | |
current = current.next; | |
count++; | |
} | |
return current; | |
} | |
set(index, val) { | |
const node = this.get(index) | |
if (node) { | |
node.val = val | |
return true; | |
} | |
return false; | |
} | |
insert(index, val) { | |
if (index < 0 || index > this.length) return false; | |
if (index == this.length) return !!this.push(val); | |
if (index == 0) return !!this.unshift(val); | |
const newNode = new Node(val); | |
const prev = this.get(index - 1); | |
const temp = prev.next; | |
prev.next = newNode; | |
newNode.next = temp; | |
this.length++; | |
return true; | |
} | |
remove(index) { | |
if (index < 0 || index >= this.length) return undefined; | |
if (index == this.length - 1) return !!this.pop(); | |
if (index == 0) return !!this.shift(); | |
const prevNode = this.get(index - 1); | |
const current = prevNode.next; | |
prevNode.next = current.next; | |
this.length--; | |
return current; | |
} | |
reverse() { | |
let node = { ...this.head }; | |
let next; | |
let previous = null; | |
while (node) { | |
next = node.next; | |
node.next = previous; | |
previous = node; | |
node = next; | |
} | |
return previous | |
} | |
} | |
const list = new SinglyLinkedList(); | |
list.push("hi"); | |
list.push("how"); | |
list.push("are"); | |
list.push("you"); | |
list.push("today"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment