R
C

LinkedList

v0.0.8tested

Doubly linked list with O(1) push/pop on both ends and O(1) insert/remove by node reference

Signature

ts
class LinkedList<T> implements LinkedListLike<T>

Type Parameters

T

Properties

PropertyTypeDescription
lengthreadonlynumberGets the number of elements in the list
isEmptyreadonlybooleanChecks if the list is empty
headreadonlyLinkedListNode<T> | undefinedGets the first node
tailreadonlyLinkedListNode<T> | undefinedGets the last node

Methods

pushBack

Appends a value to the end of the list

ts
public pushBack(value: T): LinkedListNode<T>
ParameterTypeDescription
valueTThe value to append
ReturnsLinkedListNode<T>The created node
pushFront

Prepends a value to the beginning of the list

ts
public pushFront(value: T): LinkedListNode<T>
ParameterTypeDescription
valueTThe value to prepend
ReturnsLinkedListNode<T>The created node
popBack

Removes and returns the last value

ts
public popBack(): T | undefined
ReturnsT | undefinedThe last value, or `undefined` if the list is empty
popFront

Removes and returns the first value

ts
public popFront(): T | undefined
ReturnsT | undefinedThe first value, or `undefined` if the list is empty
peekBack

Returns the last value without removing it

ts
public peekBack(): T | undefined
ReturnsT | undefinedThe last value, or `undefined` if the list is empty
peekFront

Returns the first value without removing it

ts
public peekFront(): T | undefined
ReturnsT | undefinedThe first value, or `undefined` if the list is empty
insertBefore

Inserts a value before the given node

ts
public insertBefore(node: LinkedListNode<T>, value: T): LinkedListNode<T>
ParameterTypeDescription
nodeLinkedListNode<T>The reference node
valueTThe value to insert
ReturnsLinkedListNode<T>The created node
insertAfter

Inserts a value after the given node

ts
public insertAfter(node: LinkedListNode<T>, value: T): LinkedListNode<T>
ParameterTypeDescription
nodeLinkedListNode<T>The reference node
valueTThe value to insert
ReturnsLinkedListNode<T>The created node
remove

Removes a node from the list by reference in O(1)

ts
public remove(node: LinkedListNode<T>): T
ParameterTypeDescription
nodeLinkedListNode<T>The node to remove
ReturnsTThe value of the removed node
clear

Removes all elements from the list

ts
public clear(): this
ReturnsthisThe list instance for chaining
toArray

Returns a shallow copy of the list values as an array

ts
public toArray(): T[]
ReturnsT[]Array of values from head to tail
toString

Returns a string representation of the list

ts
public toString(): string
ReturnsstringString representation
[Symbol.iterator]

Iterator over list values from head to tail

ts
public* [Symbol.iterator](): Iterator<T>
ReturnsIterator<T, any, any>
[Symbol.asyncIterator]

Async iterator over list values from head to tail

ts
public async* [Symbol.asyncIterator](): AsyncIterator<T>
ReturnsAsyncIterator<T, any, any>