Abstract Linked Lists - v1.0.4
    Preparing search index...

    Class SinglyLinkedList<N>

    Concrete implementation of a singly linked list.

    Key characteristics:

    • Forward-linked structure with head/tail tracking
    • Memory efficient single directional links
    • Supports both forward and reverse traversal

    Performance:

    • Head operations: O(1)
    • Tail operations: O(n)
    • Forward traversal: O(n)
    • Reverse traversal: O(n) time and space
    • Random access: O(n)
    // Create a new singly linked list
    const list = new SinglyLinkedList();

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

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

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

    Type Parameters

    Hierarchy (View Summary)

    Implements

    Index

    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 SinglyLinkedList();

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

      // 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 SinglyLinkedList();
      const node = new SinglyLinkedListNode();

      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 SinglyLinkedList();

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

      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 SinglyLinkedList();

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

      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 SinglyLinkedList();

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

      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 SinglyLinkedList();

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

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

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