javascript-collections-framework
    Preparing search index...

    Class LinkedList<E>Sealed

    A double-ended queue stored as a doubly linked list, whose node handles allow O(1) insertion and removal next to a known element: moving an entry to the front of a cache, inserting next to a cursor, unlinking a cancelled task. It works wherever a Deque is expected: add + poll as a queue, push + pop at the end as a stack.

    addFirstNode, addLastNode, insertBefore and insertAfter return a handle (LinkedListNode); removeNode, insertBefore and insertAfter take one. Each element has one handle for as long as it is in the list. There is no index access; use ArrayList for that.

    null and undefined are stored, as in an array, so poll(), peek() and pop() can return a stored undefined: read isEmpty() (or use removeFirst, getFirst and the other throwing forms) to tell "empty" apart. Two lists are equal only when they are the same object; compare toArray() results to compare contents. Adding or removing while iterating throws ConcurrentModificationError; walking by handles never does.

    The class is not designed to be subclassed.

    const list = new LinkedList<string>()
    const b = list.addLastNode('b')
    list.insertBefore(b, 'a')
    list.removeNode(b) // 'b'
    list.toArray() // ['a']

    Type Parameters

    • E

    Implements

    Index
    • Creates a list holding the elements of elements, in order (the first is the front); empty when elements is omitted, undefined or null (as new Set(null) is). null and undefined elements are stored.

      Time: O(m) for m elements.

      Type Parameters

      • E

      Parameters

      • Optionalelements: Iterable<E, any, any> | null

        The initial elements.

      Returns LinkedList<E>

      TypeError when elements is neither iterable nor null/undefined.

      new LinkedList(['a', 'b']) // LinkedList(2) ['a', 'b']
      
    • get "[COLLECTION_BRAND]"(): true

      Marks collections so isCollection recognises them across copies of the package. Its key is not exported, so the getter is not called directly. Time: O(1).

      Returns true

    • get "[DEQUE_BRAND]"(): true

      Marks deques so isDeque recognises them across copies of the package. Its key is not exported, so the getter is not called directly. Time: O(1).

      Returns true

    • get "[LINKED_LIST_BRAND]"(): true

      Marks instances of this class so instanceof recognises them across copies of the package. Its key is not exported, so the getter is not called directly. Time: O(1).

      Returns true

    • get "[QUEUE_BRAND]"(): true

      Marks queues so isQueue recognises them across copies of the package. Its key is not exported, so the getter is not called directly. Time: O(1).

      Returns true

    • get "[SEQUENCED_COLLECTION_BRAND]"(): true

      Marks sequenced collections so isSequencedCollection recognises them across copies of the package. Its key is not exported, so the getter is not called directly. Time: O(1).

      Returns true

    • get "[toStringTag]"(): string

      'LinkedList', for Object.prototype.toString. Time: O(1).

      Returns string

    • get revision(): number

      A number that changes on every change to the contents and never on a read. Its exact values are unspecified. Time: O(1).

      Returns number

    • get size(): number

      The number of elements. Time: O(1).

      Returns number

    • The value-equality protocol; delegates to equals (identity). Time: O(1).

      Parameters

      • other: unknown

      Returns boolean

    • Node's inspect hook: LinkedList(2) [ 1, 2 ] in console.log. Time: as toString.

      Parameters

      • depth: number | null
      • options: object
      • inspect: (value: unknown, options: object) => string

      Returns string

    • Returns an iterator over the elements from first to last.

      Time: O(1) to create; O(1) per step.

      Returns IterableIterator<E>

      A new iterator.

      ConcurrentModificationError (from next()) when elements are added or removed after the iterator is created, including through a cursor.

      for (const element of new LinkedList([1, 2])) console.log(element)
      
    • Adds element at the end and returns the list, so calls chain.

      Time: O(1).

      Parameters

      • element: E

        The element to add.

      Returns this

      The list.

      new LinkedList<number>().add(1).add(2) // LinkedList(2) [1, 2]
      
    • Adds every element of elements at the end, in order, after reading them all (so a list can add itself).

      Time: O(m) for m elements.

      Parameters

      • elements: Iterable<E>

        The elements to add.

      Returns boolean

      true when any element was added.

      TypeError when elements is not iterable.

      new LinkedList([1]).addAll([2, 3]) // true; [1, 2, 3]
      
    • Adds element at the front and returns the list.

      Time: O(1).

      Parameters

      • element: E

        The element to add.

      Returns this

      The list.

      new LinkedList([2]).addFirst(1) // LinkedList(2) [1, 2]
      
    • Adds element at the front, as addFirst does, and returns its handle.

      Time: O(1).

      Parameters

      • element: E

        The element to add.

      Returns LinkedListNode<E>

      The new element's handle.

      const node = list.addFirstNode('a')
      list.firstNode() === node // true
    • Adds element at the end and returns the list; the same as add.

      Time: O(1).

      Parameters

      • element: E

        The element to add.

      Returns this

      The list.

      new LinkedList([1]).addLast(2) // LinkedList(2) [1, 2]
      
    • Adds element at the end, as addLast does, and returns its handle.

      Time: O(1).

      Parameters

      • element: E

        The element to add.

      Returns LinkedListNode<E>

      The new element's handle.

      const node = list.addLastNode('z')
      list.lastNode() === node // true
    • Removes every element.

      Time: O(n).

      Returns void

      list.clear() // list.size === 0
      
    • Returns a new LinkedList with the same elements in the same order, independent of this one (the elements themselves are not copied).

      Time: O(n).

      Returns LinkedList<E>

      The copy.

      const copy = list.copy()
      copy.poll() // list is unchanged
    • Returns a cursor positioned before the first element; remove() removes the element next() returned. reversed().cursor() walks from the last element to the first.

      Time: O(1) to create; next and remove O(1).

      Returns Cursor<E>

      A new cursor.

      const cursor = list.cursor()
      while (cursor.hasNext()) if (cursor.next() < 0) cursor.remove()
    • Removes the first element equal to element under SameValueZero, searching from the front. reversed().delete(element) removes the last one instead.

      Time: O(n).

      Parameters

      • element: E

        The value to remove.

      Returns boolean

      true when an element was removed.

      new LinkedList([1, 2, 1]).delete(1) // true; [2, 1]
      
    • Returns whether other is this list. Lists compare by identity; compare toArray() to compare contents. Called through a Proxy of the list, it is true for that proxy too; the list itself does not know its proxies, so list.equals(proxy) is false.

      Time: O(1).

      Parameters

      • other: unknown

        Any value.

      Returns boolean

      true when other is this list.

      new LinkedList([1]).equals(new LinkedList([1])) // false
      
    • Returns the handle of the first element, or undefined when the list is empty.

      Time: O(1).

      Returns LinkedListNode<E> | undefined

      The first handle, or undefined.

      // Remove while walking: read `next` before removing.
      for (let node = list.firstNode(); node !== undefined; ) {
      const next = node.next
      if (node.value < 0) list.removeNode(node)
      node = next
      }
    • Calls action(element) for each element from first to last, with this set to thisArg.

      Time: O(n) plus the calls.

      Parameters

      • action: (element: E) => void

        Called with each element.

      • OptionalthisArg: unknown

        The this value for action.

      Returns void

      TypeError when action is not a function.

      ConcurrentModificationError when action adds or removes elements.

      new LinkedList(['a', 'b']).forEach((s) => console.log(s))
      
    • Returns the first element.

      Time: O(1).

      Returns E

      The first element.

      NoSuchElementError when the list is empty.

      new LinkedList([1, 2]).getFirst() // 1
      
    • Returns the last element.

      Time: O(1).

      Returns E

      The last element.

      NoSuchElementError when the list is empty.

      new LinkedList([1, 2]).getLast() // 2
      
    • Returns whether some element is equal to element under SameValueZero (null and undefined included).

      Time: O(n).

      Parameters

      • element: E

        The value to look for.

      Returns boolean

      true when present.

      new LinkedList([1, 2]).has(2) // true
      
    • Inserts element immediately after the element of node and returns the new handle.

      Time: O(1).

      Parameters

      • node: LinkedListNode<E>

        A handle of an element of this list.

      • element: E

        The element to insert.

      Returns LinkedListNode<E>

      The new element's handle.

      IllegalArgumentError when node is not a handle of an element now in this list (removed, from another list, or not a handle); nothing changes.

      const a = list.addFirstNode('a')
      list.insertAfter(a, 'b') // ['a', 'b', ...]
    • Inserts element immediately before the element of node and returns the new handle.

      Time: O(1).

      Parameters

      • node: LinkedListNode<E>

        A handle of an element of this list.

      • element: E

        The element to insert.

      Returns LinkedListNode<E>

      The new element's handle.

      IllegalArgumentError when node is not a handle of an element now in this list (removed, from another list, or not a handle); nothing changes.

      const c = list.addLastNode('c')
      list.insertBefore(c, 'b') // [..., 'b', 'c']
    • Returns whether the list has no elements.

      Time: O(1).

      Returns boolean

      size === 0.

      new LinkedList().isEmpty() // true
      
    • Returns the handle of the last element, or undefined when the list is empty.

      Time: O(1).

      Returns LinkedListNode<E> | undefined

      The last handle, or undefined.

      for (let node = list.lastNode(); node !== undefined; node = node.previous) console.log(node.value)
      
    • Returns the first element without removing it, or undefined when the list is empty (or the first element is undefined; isEmpty() tells them apart).

      Time: O(1).

      Returns E | undefined

      The first element, or undefined.

      new LinkedList([1, 2]).peek() // 1
      
    • Returns the last element without removing it, or undefined when the list is empty (or the last element is undefined; isEmpty() tells them apart).

      Time: O(1).

      Returns E | undefined

      The last element, or undefined.

      new LinkedList([1, 2]).peekLast() // 2
      
    • Removes and returns the first element, or returns undefined when the list is empty. A stored undefined is returned as well: check isEmpty() first to tell them apart.

      Time: O(1).

      Returns E | undefined

      The removed element, or undefined.

      new LinkedList(['a', 'b']).poll() // 'a'
      
    • Removes and returns the last element (a stored undefined included), or returns undefined when the list is empty, as Array.prototype.pop does.

      Time: O(1).

      Returns E | undefined

      The removed element, or undefined.

      new LinkedList([1, 2]).pop() // 2
      
    • Adds element at the end, as add does, and returns the list. Unlike Array.prototype.push, it returns the list, not the new length.

      Time: O(1).

      Parameters

      • element: E

        The element to add.

      Returns this

      The list.

      new LinkedList([1]).push(2) // LinkedList(2) [1, 2]
      
    • Removes every element equal to one in elements (SameValueZero). Decides first, so an error while deciding removes nothing.

      Time: O(n + m) for m elements.

      Parameters

      • elements: Iterable<E>

        The elements to remove.

      Returns boolean

      true when any element was removed.

      TypeError when elements is not iterable.

      new LinkedList([1, 2, 1]).removeAll([1]) // true; [2]
      
    • Removes and returns the first element.

      Time: O(1).

      Returns E

      The removed element.

      NoSuchElementError when the list is empty.

      new LinkedList([1, 2]).removeFirst() // 1
      
    • Calls predicate(element) on every element, then removes those for which it returned a truthy value; an error from predicate removes nothing.

      Time: O(n) plus the calls.

      Parameters

      • predicate: (element: E) => unknown

        Called with each element.

      Returns boolean

      true when any element was removed.

      TypeError when predicate is not a function.

      ConcurrentModificationError when predicate adds or removes elements.

      new LinkedList([1, -2, 3]).removeIf((n) => n < 0) // true; [1, 3]
      
    • Removes and returns the last element.

      Time: O(1).

      Returns E

      The removed element.

      NoSuchElementError when the list is empty.

      new LinkedList([1, 2]).removeLast() // 2
      
    • Removes the element of node and returns it. Afterwards the handle's next and previous are undefined and its value can still be read.

      Time: O(1).

      Parameters

      Returns E

      The removed element.

      IllegalArgumentError when node is not a handle of an element now in this list (removed, from another list, or not a handle); nothing changes.

      // Move an entry to the front.
      list.addFirstNode(list.removeNode(node))
    • Removes every element not equal to one in elements (SameValueZero). Decides first, so an error while deciding removes nothing.

      Time: O(n + m) for m elements.

      Parameters

      • elements: Iterable<E>

        The elements to keep.

      Returns boolean

      true when any element was removed.

      TypeError when elements is not iterable.

      new LinkedList([1, 2, 3]).retainAll([2]) // true; [2]
      
    • Returns a live view of this list in reverse order: its first element is this list's last, and changes through either show in both (the view's push adds at this list's front, its poll is this list's pop). reversed().reversed() returns this list.

      Time: O(1); each operation on the view costs what its mirror costs.

      Returns Deque<E>

      The reversed view.

      const list = new LinkedList([1, 2, 3])
      list.reversed().peek() // 3
      list.reversed().delete(1) // removes the last 1
    • Returns a new array of the elements, from first to last.

      Time: O(n).

      Returns E[]

      A new array.

      new LinkedList([1, 2]).toArray() // [1, 2]
      
    • Returns a new array of the elements, so JSON.stringify writes the list as an array.

      Time: O(n).

      Returns E[]

      A new array.

      JSON.stringify(new LinkedList([1, 2])) // '[1,2]'
      
    • Returns the class name, size and elements, e.g. LinkedList(2) [1, 'a'].

      Time: O(length of the output) for flat contents.

      Returns string

      The text.

      String(new LinkedList([1, 'a'])) // "LinkedList(2) [1, 'a']"
      
    • Returns whether value is a LinkedList, including one created by another copy of the library with the same major version. A reversed view is not a LinkedList.

      Time: O(1).

      Parameters

      • value: unknown

        The value on the left of instanceof.

      Returns boolean

      true when value is a LinkedList.

      new LinkedList() instanceof LinkedList // true
      
    • Returns a new list of the elements of elements, in order, like Array.from but for iterables only.

      Time: O(m) for m elements.

      Type Parameters

      • T

      Parameters

      • elements: Iterable<T>

        The elements.

      Returns LinkedList<T>

      A new list.

      TypeError when elements is not iterable (null, undefined and array-likes included).

      LinkedList.from(new Set([1, 2])) // LinkedList(2) [1, 2]
      
    • Returns a new list of mapFn(element, index) for each element of elements, in order, with this set to thisArg, as Array.from does. elements is read while mapFn runs, so items mapFn appends to an array source are included.

      Time: O(m) plus the calls.

      Type Parameters

      • T
      • U

      Parameters

      • elements: Iterable<T>

        The elements.

      • mapFn: (element: T, index: number) => U

        Called with each element and its index; its result is stored.

      • OptionalthisArg: unknown

        The this value for mapFn.

      Returns LinkedList<U>

      A new list.

      TypeError when mapFn is not a function (checked first) or elements is not iterable.

      Error whatever mapFn throws; no collection is returned.

      LinkedList.from([1, 2], (n) => n * 10) // LinkedList(2) [10, 20]