Class SinglyLinkedList<NodeData>

A doubly linked list

const list = new LinkedList(1, 2, 3);
const listFromArray = LinkedList.from([1, 2, 3]);

Type Parameters

  • NodeData = any

Hierarchy

  • SinglyLinkedList

Constructors

Properties

head: null | SinglyLinkedListNode<NodeData>

The head of the list, the first node

size: number

Internal size reference

tail: null | SinglyLinkedListNode<NodeData>

The tail of the list, the last node

Accessors

Methods

  • 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
    

    Parameters

    • Rest ...args: NodeData[]

      Data to be stored in the node, takes any number of arguments

    Returns SinglyLinkedList<NodeData>

  • 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
    

    Parameters

    • f: ((data, index, list) => boolean)

      Function to test each node val in the list. Return true to keep the node

        • (data, index, list): boolean
        • Parameters

          Returns boolean

    • reverse: boolean = false

      Indicates if the list should be filtered in reverse order, default is false

    Returns SinglyLinkedList<{} | NodeData>

  • 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
    

    Parameters

    • f: ((data, index, list) => boolean)

      Function to test val against

        • (data, index, list): boolean
        • Parameters

          Returns boolean

    Returns undefined | NodeData

  • 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
    

    Parameters

    • f: ((data, index, list) => boolean)

      Function to test val against

        • (data, index, list): boolean
        • Parameters

          Returns boolean

    Returns number

  • 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 }

    Parameters

    • f: ((data, index, list) => boolean)

      Function to test val against

        • (data, index, list): boolean
        • Parameters

          Returns boolean

    Returns undefined | SinglyLinkedListNode<NodeData>

  • 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 }

    Parameters

    • f: ((data, index, list) => boolean)

      A function to be applied to the val of each node

        • (data, index, list): boolean
        • Parameters

          Returns boolean

    Returns undefined | {
        index: number;
        node: SinglyLinkedListNode<NodeData>;
    }

  • The forEach() method executes a provided function once for each list node.

    new LinkedList(1, 2, 3).forEach(val => log(val)); // 1 2 3
    

    Parameters

    • f: ((data, index, list) => any)

      Function to execute for each element, taking up to three arguments.

        • (data, index, list): any
        • Parameters

          Returns any

    • reverse: boolean = false

      Indicates if the list should be walked in reverse order, default is false

    Returns void

  • 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
    

    Parameters

    • index: number

      The index to insert the new node at

    • val: NodeData

      Data to be stored on the new node

    Returns SinglyLinkedList<NodeData>

  • 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
    

    Parameters

    • f: ((data, index, list) => any)

      Function that produces an node of the new list, taking up to three arguments

        • (data, index, list): any
        • Parameters

          Returns any

    • reverse: boolean = false

      Indicates if the list should be mapped in reverse order, default is false

    Returns SinglyLinkedList<{} | NodeData>

  • 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

    Parameters

    Returns void

  • Reduce over each node in the list

    new LinkedList(1, 2, 3).reduce(n => n += 1, 0); // 3
    

    Parameters

    • f: ((accumulator, currentNode, index, list) => any)

      A reducer function

        • (accumulator, currentNode, index, list): any
        • Parameters

          • accumulator: any
          • currentNode: NodeData
          • index: number
          • list: SinglyLinkedList<NodeData>

          Returns any

    • Optional start: any

      An initial value

    • reverse: boolean = false

    Returns any

    The final state of the accumulator

  • 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

    Parameters

    • start: number

      Start index

    • Optional end: number

      End index, optional

    Returns SinglyLinkedList<{} | NodeData>

  • Sorts the linked list using the provided compare function

    Parameters

    • compare: ((a, b) => boolean)

      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.

        • (a, b): boolean
        • Parameters

          • a: NodeData
          • b: NodeData

          Returns boolean

    Returns SinglyLinkedList<NodeData>

  • Convert a linked list to string

    new LinkedList('one', 'two', 'three').toString(' <=> ') === 'one <=> two <=> three';
    

    Parameters

    • separator: string = ' '

      Optional string to be placed in between val nodes, default is one space

    Returns string

Generated using TypeDoc