Class DoublyLinkedList<N>

Concrete implementation of a doubly linked list.

Key characteristics:

  • Bidirectional structure with head/tail tracking
  • Direct access to previous/next nodes
  • Efficient operations at both ends

Performance:

  • Head/tail operations: O(1)
  • Bidirectional traversal: O(n)
  • Random access: O(min(k,n-k))
  • Node deletion: O(1) with direct reference
// Create a new doubly linked list
const list = new DoublyLinkedList();

// Add nodes to the list
const node1 = new DoublyLinkedListNode();
const node2 = new DoublyLinkedListNode();

list.pushNode(node1);
list.pushNode(node2);

// Traverse the list
for (const node of list) {
console.log(node);
}

Type Parameters

Hierarchy (View Summary)

Implements

Constructors

Properties

head: null | N = null

The first node in the list, or null if empty.

size: number = 0

The number of nodes in the list.

tail: null | N = null

The last node in the list, or null if empty.

Methods

  • Returns an iterator for traversing the list.

    Parameters

    • Optionalreversed: boolean = false

      If true, the iterator will traverse the list in reverse order.

    Returns Generator<N, void, unknown>

    An iterator yielding nodes in the specified order.

    const list = new DoublyLinkedList();

    // Add nodes to the list
    list.pushNode(new DoublyLinkedListNode());
    list.pushNode(new DoublyLinkedListNode());

    // Forward iteration
    for (const node of list) {
    console.log(node);
    }

    // Reverse iteration
    for (const node of list[Symbol.iterator](true)) {
    console.log(node);
    }
  • Retrieves the node at the specified index.

    Parameters

    • index: number

      The zero-based index of the node to retrieve.

    Returns undefined | N & {}

    The node at the specified index, or undefined if the index is out of bounds.

    const list = new DoublyLinkedList();
    const node = new DoublyLinkedListNode();

    list.pushNode(node);

    console.log(list.nodeAt(0) === node); // true
    console.log(list.nodeAt(99)); // undefined
  • Removes and returns the last node of the list.

    Returns undefined | N & {}

    The removed node, or undefined if the list was empty.

    const list = new DoublyLinkedList();

    const node1 = new DoublyLinkedListNode();
    const node2 = new DoublyLinkedListNode();

    list.pushNode(node1);
    list.pushNode(node2);

    console.log(list.popNode() === node2); // true
    console.log(list.popNode() === node1); // true
    console.log(list.size); // 0
  • Adds a node to the end of the list.

    Parameters

    • node: N

      The node to add.

    Returns void

    const list = new DoublyLinkedList();

    const node1 = new DoublyLinkedListNode();
    const node2 = new DoublyLinkedListNode();

    list.pushNode(node1);
    list.pushNode(node2);

    console.log(list.head === node1); // true
    console.log(list.tail === node2); // true
  • Removes and returns the first node of the list.

    Returns undefined | N & {}

    The removed node, or undefined if the list was empty.

    const list = new DoublyLinkedList();

    const node1 = new DoublyLinkedListNode();
    const node2 = new DoublyLinkedListNode();

    list.pushNode(node1);
    list.pushNode(node2);

    console.log(list.shiftNode() === node1); // true
    console.log(list.shiftNode() === node2); // true
    console.log(list.size); // 0
  • Adds a node to the beginning of the list.

    Parameters

    • node: N

      The node to add.

    Returns void

    const list = new DoublyLinkedList();

    const node1 = new DoublyLinkedListNode();
    const node2 = new DoublyLinkedListNode();

    list.unshiftNode(node1);
    list.unshiftNode(node2);

    console.log(list.head === node2); // true
    console.log(list.tail === node1); // true