xxxxxxxxxx
// Linked Lists of nodes
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
}
// Add a value at beginning of list
addStart(value) {
const node = new Node(value);
node.next = this.head;
this.head = node;
}
// Add a value at end of list
addEnd(value) {
const node = new Node(value);
let curr = this.head;
if (curr == null) {
this.head = node;
return;
}
while (curr !== null && curr.next !== null) {
curr = curr.next;
}
curr.next = node;
}
}
const list = new LinkedList();
list.addStart(1);
list.addStart(2);
list.addEnd(3);
console.log(list.head.value); // 2 (head of list)
xxxxxxxxxx
class LinkedList {
constructor(head = null) {
this.head = head
}
}
class ListNode {
constructor(value,next=null){
this.value = value;
this.next = next;
};
}
let node1 = new ListNode(2);
let node2 = new ListNode(4);
node1.next = node2;
let list = new LinkedList(node1);
console.log(list.head.next);
xxxxxxxxxx
// A balanced linked list in JavaScript
function BalancedLinkedList() {
this.head = null;
this.tail = null;
function Node(data) {
this.data = data;
this.next = null;
}
this.add = function(data) {
var newNode = new Node(data);
if (this.head === null) {
this.head = newNode;
this.tail = newNode;
} else {
var currentNode = this.head;
while (currentNode.next !== null) {
currentNode = currentNode.next;
}
currentNode.next = newNode;
this.tail = newNode;
}
};
this.remove = function(data) {
var currentNode = this.head;
var previousNode = null;
while (currentNode !== null && currentNode.data !== data) {
previousNode = currentNode;
currentNode = currentNode.next;
}
if (currentNode === null) {
return false;
} else if (previousNode === null) {
this.head = currentNode.next;
} else {
previousNode.next = currentNode.next;
}
return true;
};
}
// A hash table in JavaScript
function HashMap() {
this.table = {};
this.add = function(key, value) {
this.table[key] = value;
};
this.get = function(key) {
return this.table[key];
};
this.remove = function(key) {
delete this.table[key];
};
}
// A linked list with a sentinel node in JavaScript
function LinkedListWithSentinel() {
this.sentinel = new Node(null);
this.head = this.sentinel;
this.tail = this.sentinel;
function Node(data) {
this.data = data;
this.next = null;
}
this.add = function(data) {
var newNode = new Node(data);
this.tail.next = newNode;
this.tail = newNode;
};
this.remove = function(data) {
var currentNode = this.head.next;
var previousNode = this.head;
while (currentNode !== null && currentNode.data !== data) {
previousNode = currentNode;
currentNode = currentNode.next;
}
if (currentNode === null) {
return false;
} else {
previousNode.next = currentNode.next;
}
return true;
};
}
xxxxxxxxxx
class LinkedList {
constructor() {
this.nodes = [];
}
get size() {
return this.nodes.length;
}
get head() {
return this.size ? this.nodes[0] : null;
}
get tail() {
return this.size ? this.nodes[this.size - 1] : null;
}
insertAt(index, value) {
const previousNode = this.nodes[index - 1] || null;
const nextNode = this.nodes[index] || null;
const node = { value, next: nextNode };
if (previousNode) previousNode.next = node;
this.nodes.splice(index, 0, node);
}
insertFirst(value) {
this.insertAt(0, value);
}
insertLast(value) {
this.insertAt(this.size, value);
}
getAt(index) {
return this.nodes[index];
}
removeAt(index) {
const previousNode = this.nodes[index - 1];
const nextNode = this.nodes[index + 1] || null;
if (previousNode) previousNode.next = nextNode;
return this.nodes.splice(index, 1);
}
clear() {
this.nodes = [];
}
reverse() {
this.nodes = this.nodes.reduce(
(acc, { value }) => [{ value, next: acc[0] || null }, acc],
[]
);
}
*[Symbol.iterator]() {
yield* this.nodes;
}
}