Skip to content

Instantly share code, notes, and snippets.

@clohr
Created March 7, 2017 00:37
Show Gist options
  • Select an option

  • Save clohr/f0fa954a94740287bb08df3ee9ffefb7 to your computer and use it in GitHub Desktop.

Select an option

Save clohr/f0fa954a94740287bb08df3ee9ffefb7 to your computer and use it in GitHub Desktop.
ES6 Singly Linked List
class Node {
constructor(data) {
this.data = data
this.next = null
}
}
class SinglyList {
constructor() {
this._length = 0;
this.head = null;
}
add(value) {
const node = new Node(value)
let currentNode = this.head;
// 1st use-case: an empty list
if (!currentNode) {
this.head = node;
this._length++;
return node;
}
// 2nd use-case: a non-empty list
while (currentNode.next) {
currentNode = currentNode.next;
}
currentNode.next = node;
this._length++;
return node;
}
searchNodeAt(position) {
let currentNode = this.head
let length = this._length
let count = 1
const message = {failure: 'Failure: non-existent node in this list.'}
// 1st use-case: an invalid position
if (length === 0 || position < 1 || position > length) {
throw new Error(message.failure)
}
// 2nd use-case: a valid position
while (count < position) {
currentNode = currentNode.next
count++
}
return currentNode
}
remove(position) {
let currentNode = this.head
let length = this._length
let count = 0
const message = {failure: 'Failure: non-existent node in this list.'}
let beforeNodeToDelete = null
let nodeToDelete = null
let deletedNode = null
// 1st use-case: an invalid position
if (position < 0 || position > length) {
throw new Error(message.failure)
}
// 2nd use-case: the first node is removed
if (position === 1) {
this.head = currentNode.next
deletedNode = currentNode
currentNode = null
this._length--
return deletedNode
}
// 3rd use-case: any other node is removed
while (count < position) {
beforeNodeToDelete = currentNode
nodeToDelete = currentNode.next
count++;
}
beforeNodeToDelete.next = nodeToDelete.next
deletedNode = nodeToDelete
nodeToDelete = null
this._length--
return deletedNode
}
}
export default SinglyList
@clohr
Copy link
Copy Markdown
Author

clohr commented Mar 7, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment