Skip to content

Instantly share code, notes, and snippets.

@darshna09
Created June 4, 2021 04:49
Show Gist options
  • Save darshna09/0ef77fb86d560cfd8863df6206f4451e to your computer and use it in GitHub Desktop.
Save darshna09/0ef77fb86d560cfd8863df6206f4451e to your computer and use it in GitHub Desktop.
Single Linked List Implementation in JavaScript
// Creating node
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class LinkedList {
// Empty Linked List will not be formed.
constructor(value) {
this.head = {
value: value,
next: null
};
this.tail = this.head;
this.length = 1;
}
// Print the list
printList() {
let list = [];
let currentNode = this.head;
while (currentNode !== null) {
list.push(currentNode.value);
currentNode = currentNode.next;
}
console.log(list.join(' --> '));
}
// Append a value at the end
append(value) {
let newNode = new Node(value);
this.tail.next = newNode;
this.tail = newNode;
this.length++;
this.printList();
return this.length;
}
// Append a value at the beginning
prepend(value) {
let newNode = new Node(value);
newNode.next = this.head;
this.head = newNode;
this.length++;
this.printList();
return this.length;
}
// Insert a node at a given index
insert(index, value) {
// Verify index
if (!Number.isInteger(index) || index < 0 || index > this.length + 1) {
console.log(`Enter valid index. Current length of the linked list is ${this.length}`);
return this.length;
}
// Insert at index 0 = prepend
if (index === 0) {
this.prepend(value);
return this.length;
}
// Insert at the end = append
if (index === this.length) {
this.append(value);
return this.length;
}
// Insert at given index
let newNode = new Node(value);
let previousNode = this.head;
for (let k = 0; k < index - 1; k++) {
previousNode = previousNode.next;
}
let nextNode = previousNode.next;
newNode.next = nextNode;
previousNode.next = newNode;
this.length++;
this.printList();
return this.length;
}
// Remove a node at given index
remove(index) {
// Verify index
if (!Number.isInteger(index) || index < 0 || index >= this.length) {
console.log(`Enter valid index. Current length of the linked list is ${this.length}`);
return this.length;
}
// Remove head node
if (index === 0) {
this.head = this.head.next;
this.length--;
this.printList();
return this.length;
}
// Remove tail node
if (index === this.length - 1) {
let lastNode = this.head.next;
let secondLastNode = this.head;
while (lastNode.next !== null) {
secondLastNode = secondLastNode.next;
lastNode = lastNode.next;
}
secondLastNode.next = null;
this.tail = this.secondLastNode;
this.length--;
this.printList();
return this.length;
}
// Remove at given index
let previousNode = this.head;
for (let k = 0; k < index - 1; k++) {
previousNode = previousNode.next;
}
let nextNode = previousNode.next;
previousNode.next = nextNode.next;
this.length--;
this.printList();
return this.length;
}
}
const myLinkedList = new LinkedList(10);
myLinkedList.printList(); // 10
myLinkedList.append(5); // 10 --> 5
myLinkedList.append(16); // 10 --> 5 --> 16
myLinkedList.prepend(1); // 1 --> 10 --> 5 --> 16
// Insert 99 at index 2
myLinkedList.insert(2, 99); // 1 --> 10 --> 99 --> 5 --> 16
myLinkedList.insert(20, 88); // Enter valid index. Current length of the linked list is 5.
myLinkedList.insert(5, 80); // 1 --> 10 --> 99 --> 5 --> 16 --> 80
myLinkedList.insert(0, 80); // 80 --> 1 --> 10 --> 99 --> 5 --> 16 --> 80
myLinkedList.remove(0); // 1 --> 10 --> 99 --> 5 --> 16 --> 80
myLinkedList.remove(5); // 1 --> 10 --> 99 --> 5 --> 16
myLinkedList.remove(2); // 1 --> 10 --> 5 --> 16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment