Options
All
  • Public
  • Public/Protected
  • All
Menu

Class SinglyLinkedList<T>

A linked list that has nodes which only has a reference to the next node.Inherits from LinkedList

Type parameters

  • T

Hierarchy

Index

Constructors

constructor

Properties

Protected _head

_head: null | SLLNode<T>

Protected _length

_length: number

Protected _tail

_tail: null | SLLNode<T>

Accessors

head

  • get head(): undefined | T
  • Returns the head's value of the list if the list is not empty or returns undefined

    example
    const list = new SinglyLinkedList<number>();
    list.insertAtEnd(1);
    list.insertAtEnd(2);
    console.log(list.head); // 1
    list.deleteFromStart();
    console.log(list.head); // 2

    Returns undefined | T

length

  • get length(): number
  • Returns the length of the list

    example
    const list = new SinglyLinkedList<number>();
    list.insertAtEnd(1);
    list.insertAtEnd(2);
    list.insertAtEnd(3);
    console.log(list.length); // 3
    list.deleteFromEnd();
    console.log(list.length); // 2

    Returns number

tail

  • get tail(): undefined | null | T
  • Returns the tail's value of the list if the list is not empty or returns undefined

    example
    const list = new SinglyLinkedList<number>();
    list.insertAtEnd(1);
    list.insertAtEnd(2);
    console.log(list.tail); // 2
    list.deleteFromEnd();
    console.log(list.tail); // 1

    Returns undefined | null | T

Methods

deleteAfter

  • deleteAfter(target: T): T
  • Delete the node after the given target value and return its value

    example
    const list = SinglyLinkedList.fromArray([1, 2, 3]);
    list.insertAfter(2, 4);
    console.log(list.deleteAfter(2)); // 3
    console.log(list.length); // 3
    console.log(list.toArray()); // [1, 2, 4]
    throws

    {@link Error} if the target value is not found

    example
    const list = new SinglyLinkedList<number>();
    list.deleteAfter(1); // throws an error

    Parameters

    • target: T

      The value to be searched for in the list to delete the node after it

    Returns T

deleteAtIndex

  • deleteAtIndex(index: number): T
  • Delete the node at the given index and return its value

    example
    const list = SinglyLinkedList.fromArray([1, 2, 3]);
    console.log(list.deleteAtIndex(1)); // 2
    console.log(list.length); // 2
    console.log(list.toArray()); // [1, 3]
    throws

    {@link Error} if the index is out of bounds

    example
    const list = new SinglyLinkedList<number>();
    list.deleteAtIndex(1); // throws an error

    Parameters

    • index: number

      The index of the node to be deleted

    Returns T

deleteBefore

  • deleteBefore(target: T): T
  • Delete the node before the given target value and return its value

    example
    const list = SinglyLinkedList.fromArray([1, 2, 3]);
    list.insertBefore(2, 4);
    console.log(list.deleteBefore(2)); // 1
    console.log(list.length); // 3
    console.log(list.toArray()); // [2, 4, 3]
    throws

    {@link Error} if the target value is not found

    example
    const list = new SinglyLinkedList<number>();
    list.deleteBefore(1); // throws an error

    Parameters

    • target: T

      The value to be searched for in the list to delete the node before it

    Returns T

deleteFromEnd

  • deleteFromEnd(): T
  • Delete the last node from the list and return its value

    example
    const list = SinglyLinkedList.fromArray([1, 2, 3]);
    console.log(list.deleteFromEnd()); // 3
    console.log(list.length); // 2
    console.log(list.toArray()); // [1, 2]
    throws

    {@link Error} if the list is empty

    example
    const list = new SinglyLinkedList<number>();
    list.deleteFromEnd(); // throws an error

    Returns T

deleteFromStart

  • deleteFromStart(): T
  • delete the first node from the list and return its value

    example
    const list = SinglyLinkedList.fromArray([1, 2, 3]);
    console.log(list.deleteFromStart()); // 1
    console.log(list.length); // 2
    console.log(list.toArray()); // [2, 3]
    throws

    {@link Error} if the list is empty

    example
    const list = new SinglyLinkedList<number>();
    list.deleteFromStart(); // throws an error

    Returns T

deleteValue

  • deleteValue(target: T): T
  • Delete the node containing the given value and return its value

    example
    const list = SinglyLinkedList.fromArray([1, 2, 3, 4]);
    console.log(list.deleteValue(3)); // 3
    console.log(list.length); // 3
    console.log(list.toArray()); // [1, 2, 4]
    throws

    {@link Error} if the target value is not found

    example
    const list = new SinglyLinkedList<number>();
    list.deleteValue(1); // throws an error

    Parameters

    • target: T

      The value to be searched for in the list to delete the node containing it

    Returns T

display

  • display(): void

forEachApply

  • forEachApply(callback: (value: SLLNode<T>) => void): void

insertAfter

  • insertAfter(target: T, value: T): void
  • Insert the given value after the given target value, throws an error if the target value is not found

    example
    const list = SinglyLinkedList.fromArray([1, 2, 3]);
    list.insertAfter(2, 4);
    console.log(list.length); // 4
    console.log(list.toArray()); // [1, 2, 4, 3]
    throws

    {@link Error} if the target value is not found

    example
    const list = new SinglyLinkedList<number>();
    list.insertAfter(1, 1); // throws an error

    Parameters

    • target: T

      The value to be searched for in the list to insert the value after it

    • value: T

      The value to be inserted after the target value

    Returns void

insertAtEnd

  • insertAtEnd(value: T): void

insertAtIndex

  • insertAtIndex(index: number, value: T): void
  • Insert the given value at the given index, throws an error if the index is out of bounds

    example
    const list = SinglyLinkedList.fromArray([1, 2, 3]);
    list.insertAtIndex(1, 4);
    console.log(list.length); // 4
    console.log(list.toArray()); // [1, 4, 2, 3]
    throws

    {@link Error} if the index is out of bounds

    example
    const list = new SinglyLinkedList<number>();
    list.insertAtIndex(1, 1); // throws an error

    Parameters

    • index: number

      index at which the value should be inserted

    • value: T

      value to be inserted at the given index, should be of type {T}

    Returns void

insertAtStart

  • insertAtStart(value: T): void

insertBefore

  • insertBefore(target: T, value: T): void
  • Insert the given value before the given target value, throws an error if the target value is not found

    example
    const list = SinglyLinkedList.fromArray([1, 2, 3]);
    list.insertBefore(2, 4);
    console.log(list.length); // 4
    console.log(list.toArray()); // [1, 4, 2, 3]
    throws

    {@link Error} if the target value is not found

    example
    const list = new SinglyLinkedList<number>();
    list.insertBefore(1, 1); // throws an error

    Parameters

    • target: T

      The value to be searched for in the list to insert the value before it

    • value: T

      The value to be inserted before the target value

    Returns void

reverse

  • reverse(): void

toArray

  • toArray(): T[]

Static fromArray

Generated using TypeDoc, the 1/4/2022 at 10:37:57 PM