Rest
...args: NodeData[]The head of the list, the first node
Private
sizeInternal size reference
The tail of the list, the last node
The length of the list
Append one or any number of nodes to the end of the list. This modifies the list in place and returns the list itself to make this method chainable.
new LinkedList(1).append(2).append(3, 4); // 1 <=> 2 <=> 3 <=> 4
Rest
...args: NodeData[]Data to be stored in the node, takes any number of arguments
Removes all nodes from a list
list.clear();
The filter() method creates a new list with all nodes that pass the test implemented by the provided function.
new LinkedList(1, 2, 3, 4, 5).filter(val => val < 4); // 1 <=> 2 <=> 3
Function to test each node val in the list. Return true to keep the node
Indicates if the list should be filtered in reverse order, default is false
Returns the value of the first element in the list that satisfies the provided testing function. Otherwise undefined is returned.
new LinkedList(1, 2, 3).find(val => val === 1); // 1
Function to test val against
Returns the index of the first node in the list that satisfies the provided testing function. Ohterwise -1 is returned.
new LinkedList(1, 2, 3).findIndex(val => val === 3); // 2
Function to test val against
Returns the first node in the list that satisfies the provided testing function. Otherwise undefined is returned.
new LinkedList(1, 2, 3).findNode(val => val === 1);
// { prev: null, val: 1, next: SinglyLinkedListNode }
Function to test val against
Return the first node and its index in the list that satisfies the testing function
new LinkedList(1, 2, 3).findNodeIndex(val => val === 1);
// { node: SinglyLinkedListNode, index: 0 }
A function to be applied to the val of each node
The forEach() method executes a provided function once for each list node.
new LinkedList(1, 2, 3).forEach(val => log(val)); // 1 2 3
Function to execute for each element, taking up to three arguments.
Indicates if the list should be walked in reverse order, default is false
Get the node at index, zero based
new LinkedList(1, 2, 3).getNode(0);
// { prev: null, val: 1, next: SinglyLinkedListNode }
Insert a new node after this one
const list = new LinkedList(2, 3);
list.insertAfter(list.head, 1); // 1 <=> 2 <=> 3
The reference node
Data to be saved in the node
Insert a new node at a given index position. If index is out of bounds, the node is appended, if index is negative or 0, it will be prepended.
new LinkedList(1, 3).insertAt(1, 2); // 1 <=> 2 <=> 3
The index to insert the new node at
Data to be stored on the new node
Insert a new node before the reference node
const list = new LinkedList(1, 3);
list.insertBefore(list.tail, 2); // 1 <=> 2 <=> 3
The node reference
Data to save in the node
The map() method creates a new list with the results of calling a provided function on every node in the calling list.
new LinkedList(1, 2, 3).map(val => val + 10); // 11 <=> 12 <=> 13
Function that produces an node of the new list, taking up to three arguments
Indicates if the list should be mapped in reverse order, default is false
Merge the current list with another. Both lists will be equal after merging.
const list = new LinkedList(1, 2);
const otherList = new LinkedList(3);
list.merge(otherList);
(list === otherList); // true
The list to be merged
Prepend any number of val arguments to the list. The argument list is prepended as a block to reduce confusion:
new LinkedList(3, 4).prepend(0, 1, 2); // [0, 1, 2, 3, 4]
Rest
...args: NodeData[]Data to be stored in the node, accepts any number of arguments
Reduce over each node in the list
new LinkedList(1, 2, 3).reduce(n => n += 1, 0); // 3
A reducer function
Optional
start: anyAn initial value
The final state of the accumulator
Remove the node at the specified index
new LinkedList(1, 2, 3).removeAt(2); // { prev: null, val: 3, next: null, list: null }
Index at which to remove
Private
removePrivate helper function to reduce duplication of pop() and shift() methods
Remove the specified node from the list and return the removed node afterwards.
const list = new LinkedList(1, 2, 3);
list.removeNode(list.tail); // { prev: null, val: 3, next: null, list: null }
The node to be removed
The reverse() function reverses the list in place and returns the list itself.
new LinkedList(1, 2, 3).reverse(); // 3 <=> 2 <=> 1
The slice() method returns a shallow copy of a portion of a list into a new list object selected from start to end (end not included). The original list will not be modified.
const list = new LinkedList(1, 2, 3, 4, 5);
const newList = list.slice(0, 3); // 1 <=> 2 <=> 3
Start index
Optional
end: numberEnd index, optional
Sorts the linked list using the provided compare function
A function used to compare the val of two nodes. It should return a boolean. True will insert a before b, false will insert b before a. (a, b) => a < b or (1, 2) => 1 < 2 === true, 2 will be inserted after 1, the sort order will be ascending.
Static
fromConvert any iterable to a new linked list
const array = [1, 2, 3];
const list = LinkedList.from(array);
Any iterable datatype like Array or Map
Generated using TypeDoc
A doubly linked list