UNPKG

dsa-master

Version:

A comprehensive data structures and algorithms library for JavaScript, featuring optimized implementations of common DS and algorithms.

80 lines (72 loc) 1.79 kB
class Node { constructor(value) { this.value = value; this.next = null; } } class LinkedList { constructor() { this.head = null; this.tail = null; this.size = 0; } // Insert at the end append(value) { let newNode = new Node(value); if (!this.head) { this.head = this.tail = newNode; } else { this.tail.next = newNode; this.tail = newNode; } this.size++; } // Insert at the beginning prepend(value) { let newNode = new Node(value); if (!this.head) { this.head = this.tail = newNode; } else { newNode.next = this.head; this.head = newNode; } this.size++; } // Delete a value delete(value) { if (!this.head) return false; if (this.head.value === value) { this.head = this.head.next; this.size--; return true; } let current = this.head; while (current.next && current.next.value !== value) { current = current.next; } if (current.next) { current.next = current.next.next; this.size--; return true; } return false; } display() { let current = this.head; let result = ""; while (current) { result += current.value + " -> "; current = current.next; } console.log(result + "null"); } } module.exports = LinkedList; /** * Time Complexity: * - append(): O(1) * - prepend(): O(1) * - delete(): O(n) * * Space Complexity: O(n) */