Lightweight zero-dependency implementation if various linked lists in TypeScript.
You can use it to improve the performance of your node or browser applications built with JavaScript/TypeScript
This package contains four different implementations of linked lists:
- Singly linked list (
new SinglyLinkedList<T>()
) - Doubly linked list (
new DoublyLinkedList<T>()
) - Circular singly linked list (
new CircularSinglyLinkedList<T>()
) - Circular doubly linked list (
new CircularDoublyLinkedList<T>()
)
All linked lists contains similar properties and methods.
Here is what each class contains:
In all examples below, we used
SinglyLinkedList
implementation. But the usages are just the same for all implementations.
Converts the linked list to a native array
const lList = new SinglyLinkedList<number>()
const array = lList.appendTail(1).appendTail(2).appendTail(3).toArray()
// [1, 2, 3]
Appends a new node to the head
const lList = new SinglyLinkedList<number>()
const array = lList.appendHead(1).appendHead(2).appendHead(3).toArray()
// [3, 2, 1]
Appends a new node to the tail
const lList = new SinglyLinkedList<number>()
const array = lList.appendTail(1).appendTail(2).appendTail(3).toArray()
// [1, 2, 3]
Appends a new node to the given index
const lList = new SinglyLinkedList<number>()
const array = lList
.appendTail(1)
.appendTail(2)
.appendTail(3)
.appendAt(2, 10)
.toArray()
// [1, 2, 10, 3]
Removes all nodes
const lList = new SinglyLinkedList<number>()
const array = lList.appendTail(1).appendTail(2).appendTail(3).clear().toArray()
// []
Reverses the node's positions
const lList = new SinglyLinkedList<number>()
const array = lList
.appendTail(1)
.appendTail(2)
.appendTail(3)
.reverse()
.toArray()
// [3, 2, 1]
Finds first node with the given value and returns the index of that
const lList = new SinglyLinkedList<number>()
const index = lList.appendTail(1).appendTail(2).appendTail(3).indexOf(2)
// 1
Finds all nodes with the given value and returns the indexes of them
const lList = new SinglyLinkedList<number>()
const indexes = lList.appendTail(1).appendTail(2).appendTail(2).indexOfAll(2)
// [1, 2]
Checks if the linked list is empty
const lList = new SinglyLinkedList<number>()
const isEmpty = lList.isEmpty()
// true
Removes the head node
const lList = new SinglyLinkedList<number>()
const array = lList.appendHead(1).appendHead(2).removeHead().toArray()
// [1]
Removes the tail node
const lList = new SinglyLinkedList<number>()
const array = lList.appendHead(1).appendHead(2).removeTail().toArray()
// [2]
Removes node at the given index
const lList = new SinglyLinkedList<number>()
const array = lList.appendTail(1).appendTail(2).removeAt(0).toArray()
// [2]
Finds fist node by the given value and removes that
const lList = new SinglyLinkedList<number>()
const array = lList
.appendTail(1)
.appendTail(2)
.appendTail(2)
.removeBy(2)
.toArray()
// [1, 2]
Removes all nodes by the given value
const lList = new SinglyLinkedList<number>()
const array = lList
.appendTail(1)
.appendTail(2)
.appendTail(2)
.removeAllBy(2)
.toArray()
// [1]
Returns the head node
const lList = new SinglyLinkedList<number>()
const headValue = lList
.appendTail(1)
.appendTail(2)
.appendTail(3)
.getHead().value
// 1
Returns the tail node
const lList = new SinglyLinkedList<number>()
const tailValue = lList
.appendTail(1)
.appendTail(2)
.appendTail(3)
.getTail().value
// 3
Returns node at the given index
const lList = new SinglyLinkedList<number>()
const value = lList.appendTail(1).appendTail(2).appendTail(3).getAt(1).value
// 2
Finds and returns first node by the given value
const lList = new SinglyLinkedList<number>()
const value = lList.appendTail(1).appendTail(2).appendTail(3).getBy(1).value
// 1
Finds and returns all nodes by the given value
const lList = new SinglyLinkedList<number>()
const values = lList
.appendTail(1)
.appendTail(2)
.appendTail(2)
.getAllBy(2)
.map(node => node.value)
// [2, 2]
Finds and returns first node from the end (reversely)
const lList = new SinglyLinkedList<number>()
const value = lList
.appendTail(1)
.appendTail(2)
.appendTail(3)
.appendTail(4)
.appendTail(5)
.getNthFromEnd(1).value
// 4
Returns the length of the linked list
const lList = new SinglyLinkedList<number>()
const list = lList
.appendTail(1)
.appendTail(2)
.appendTail(3)
.appendTail(4)
.appendTail(5).length
// 5