Class LinkedList<T>

A singly linked list data structure. Each element points to the next element in the sequence.

Typeparam

T - The type of elements stored in the list

Type Parameters

  • T

Implements

Constructors

Properties

head: SingleNode<T> = null

Methods

  • Adds an element to the end of the list. This is an alias for addLast.

    Parameters

    • val: T

      The value to add

    Returns void

  • Adds an element to the end of the list. Traverses the entire list to add the element at the end.

    Parameters

    • val: T

      The value to add

    Returns void

  • Checks if the list contains a specific value.

    Parameters

    • val: T

      The value to search for

    Returns boolean

    True if the value exists in the list, false otherwise

  • Returns the element at the specified index.

    Parameters

    • index: number

      The index of the element to retrieve

    Returns T

    The element at the specified index

    Throws

    Error if the list is empty or index is out of bounds

  • Returns the last element in the list. Traverses the entire list to find the last element.

    Returns T

    The last element

    Throws

    Error if the list is empty

  • Returns the index of the first occurrence of the specified value.

    Parameters

    • val: T

      The value to search for

    Returns number

    The index of the value, or -1 if not found

  • Removes the element at the specified index.

    Parameters

    • index: number

      The index of the element to remove

    Returns void

    Throws

    Error if the list is empty or index is out of bounds

  • Removes the last element from the list. Traverses the entire list to find and remove the last element.

    Returns void

    Throws

    Error if the list is empty

  • Replaces the element at the specified index with a new value.

    Parameters

    • index: number

      The index of the element to replace

    • val: T

      The new value

    Returns T

    The previous value at the specified index

    Throws

    Error if the index is out of bounds