Use the length getter to get the number of nodes in the list. The push/unshift methods also return the length of the list.
importLinkedListfrom'core/linked-list';
const list = newLinkedList();
list.push(10, 11); // 2 list.unshift(9); // 3
// 3 console.log(list.length);
To create a new list based on another, use slice.
importLinkedListfrom'core/linked-list';
const list = newLinkedList([1, 2, 3]);
// [3] console.log([...list.slice(-1)]);
// [1, 2] console.log([...list.slice(0, -1)]);
There are 2 iterators to traverse the list: values (used by default) and reverse.
Note that unlike arrays, here reverse returns an iterator, not a new linked list.
However, you can pass this iterator to the constructor when creating a new list.
core/linked-list
This module provides a class to organize a double-ended two-way linked list. For convenience, the list API is similar to the regular JS Array API.
API
Constructor
The LinkedList constructor can take any Iterable object whose elements will be used to populate the list.
Own API
first
Data of the first node in the list.
last
Data of the last node in the list.
clear
Clears all nodes from the list.
reverse
Returns an iterator over the data in the list. The traversal will proceed from the last node to the first.
Array-Like API
For convenience, the LinkedList API is similar to the regular JS Array API:
Adding and removing is done through the
push/pop/shift/unshift
methods.Checking if a node is in the list with a given value is done using
includes
.Use the
length
getter to get the number of nodes in the list. Thepush/unshift
methods also return the length of the list.To create a new list based on another, use
slice
.There are 2 iterators to traverse the list:
values
(used by default) andreverse
. Note that unlike arrays, herereverse
returns an iterator, not a new linked list. However, you can pass this iterator to the constructor when creating a new list.