Class SinglyLinkedList<E, R>

  1. Node Structure: Each node contains three parts: a data field, a pointer (or reference) to the previous node, and a pointer to the next node. This structure allows traversal of the linked list in both directions.
  2. Bidirectional Traversal: Unlike doubly linked lists, singly linked lists can be easily traversed forwards but not backwards.
  3. No Centralized Index: Unlike arrays, elements in a linked list are not stored contiguously, so there is no centralized index. Accessing elements in a linked list typically requires traversing from the head or tail node.
  4. High Efficiency in Insertion and Deletion: Adding or removing elements in a linked list does not require moving other elements, making these operations more efficient than in arrays. Caution: Although our linked list classes provide methods such as at, setAt, addAt, and indexOf that are based on array indices, their time complexity, like that of the native Array.lastIndexOf, is 𝑂(𝑛). If you need to use these methods frequently, you might want to consider other data structures, such as Deque or Queue (designed for random access). Similarly, since the native Array.shift method has a time complexity of 𝑂(𝑛), using an array to simulate a queue can be inefficient. In such cases, you should use Queue or Deque, as these data structures leverage deferred array rearrangement, effectively reducing the average time complexity to 𝑂(1).

Type Parameters

  • E = any
  • R = any

Hierarchy (view full)

Methods

  • The function _createInstance returns a new instance of SinglyLinkedList with the specified options.

    Parameters

    • Optionaloptions: SinglyLinkedListOptions<E, R>

      The options parameter in the _createInstance method is of type SinglyLinkedListOptions<E, R>, which is used to configure the behavior of the SinglyLinkedList instance being created. It is an optional parameter, meaning it can be omitted when calling the method.

    Returns this

    An instance of the SinglyLinkedList class with an empty array and the provided options is being returned.

  • The function _ensurePredicate in TypeScript ensures that the input is either a node, a predicate function, or a value to compare with the node's value.

    Parameters

    Returns ((node: SinglyLinkedListNode<E>) => boolean)

    A function is being returned. If the input elementNodeOrPredicate is already a node, a function is returned that checks if a given node is equal to the input node. If the input is a predicate function, it is returned as is. If the input is neither a node nor a predicate function, a function is returned that checks if a given node's value is equal to the input

  • The function _getIterator returns an iterable iterator that yields the values of a linked list.

    Returns IterableIterator<E, any, any>

  • The function _getPrevNode returns the node before a given node in a singly linked list.

    Parameters

    • node: SinglyLinkedListNode<E>

      The node parameter in the _getPrevNode method is a reference to a node in a singly linked list. The method is used to find the node that comes before the given node in the linked list.

    Returns undefined | SinglyLinkedListNode<E>

    The _getPrevNode method returns either the previous node of the input node in a singly linked list or undefined if the input node is the head of the list or if the input node is not found in the list.

  • The function returns an iterator that iterates over the elements of a collection in reverse order.

    Returns IterableIterator<E, any, any>

  • The _isPredicate function in TypeScript checks if the input is a function that takes a SinglyLinkedListNode as an argument and returns a boolean.

    Parameters

    Returns elementNodeOrPredicate is ((node: SinglyLinkedListNode<E>) => boolean)

    The _isPredicate method is returning a boolean value based on whether the elementNodeOrPredicate parameter is a function or not. If the elementNodeOrPredicate is a function, the method will return true, indicating that it is a predicate function. If it is not a function, the method will return false.

  • Time Complexity: O(n) Space Complexity: O(1)

    The function is an implementation of the Symbol.iterator method that returns an IterableIterator.

    Parameters

    • Rest...args: any[]

      The args parameter in the code snippet represents a rest parameter. It allows the function to accept any number of arguments as an array. In this case, the args parameter is used to pass any number of arguments to the _getIterator method.

    Returns IterableIterator<E, any, any>

  • Time Complexity: O(n) Space Complexity: O(1)

    The addAfter function in TypeScript adds a new element or node after an existing element or node in a singly linked list.

    Parameters

    • existingElementOrNode: E | SinglyLinkedListNode<E>

      existingElementOrNode can be either an element of type E or a SinglyLinkedListNode of type E.

    • newElementOrNode: E | SinglyLinkedListNode<E>

      The newElementOrNode parameter in the addAfter method represents the element or node that you want to add after the existing element or node in a singly linked list. This parameter can be either the value of the new element or a reference to a SinglyLinkedListNode containing

    Returns boolean

    The addAfter method returns a boolean value - true if the new element or node was successfully added after the existing element or node, and false if the existing element or node was not found.

  • Time Complexity: O(n) Space Complexity: O(1)

    The addAt function inserts a new element or node at a specified index in a singly linked list.

    Parameters

    • index: number

      The index parameter represents the position at which you want to add a new element or node in the linked list. It is a number that indicates the index where the new element or node should be inserted.

    • newElementOrNode: E | SinglyLinkedListNode<E>

      The newElementOrNode parameter in the addAt method can be either a value of type E or a SinglyLinkedListNode<E> object. This parameter represents the element or node that you want to add to the linked list at the specified index.

    Returns boolean

    The addAt method returns a boolean value - true if the element or node was successfully added at the specified index, and false if the index is out of bounds.

  • Time Complexity: O(n) Space Complexity: O(1)

    The function addBefore in TypeScript adds a new element or node before an existing element or node in a singly linked list.

    Parameters

    • existingElementOrNode: E | SinglyLinkedListNode<E>

      existingElementOrNode represents the element or node in the linked list before which you want to add a new element or node.

    • newElementOrNode: E | SinglyLinkedListNode<E>

      The newElementOrNode parameter in the addBefore method represents the element or node that you want to insert before the existing element or node in the linked list. This new element can be of type E or a SinglyLinkedListNode<E>.

    Returns boolean

    The addBefore method returns a boolean value - true if the new element or node was successfully added before the existing element or node, and false if the operation was unsuccessful.

  • Time Complexity: O(n) Space Complexity: O(1)

    The function at returns the value at a specified index in a linked list, or undefined if the index is out of range.

    Parameters

    • index: number

      The index parameter is a number that represents the position of the element we want to retrieve from the list.

    Returns undefined | E

    The method at(index: number): E | undefined returns the value at the specified index in the linked list, or undefined if the index is out of bounds.

  • Time Complexity: O(1) Space Complexity: O(1)

    The clear function resets the linked list by setting the head, tail, and length to undefined and 0 respectively.

    Returns void

  • Time Complexity: O(n) Space Complexity: O(n)

    The clone function returns a new instance of the SinglyLinkedList class with the same values as the original list.

    Returns this

    The clone() method is returning a new instance of the SinglyLinkedList class, which is a clone of the original list.

  • Parameters

    • Rest...items: LinearBase<E, R, LinkedListNode<E>>[]

      The concat method you provided takes in a variable number of arguments of type E or LinearBase<E, R>. The method concatenates these items to the current list and returns a new list with the concatenated items.

    Returns this

    The concat method is returning a new instance of the class that it belongs to, with the items passed as arguments concatenated to it.

  • Time Complexity: O(n) Space Complexity: O(1)

    The function countOccurrences iterates through a singly linked list and counts the occurrences of a specified element or nodes that satisfy a given predicate.

    Parameters

    Returns number

    The countOccurrences method returns the number of occurrences of the specified element, node, or predicate function in the singly linked list.

  • Time Complexity: O(n) Space Complexity: O(1)

    The delete function removes a node with a specific value from a singly linked list.

    Parameters

    • elementOrNode: undefined | E | SinglyLinkedListNode<E>

      The elementOrNode parameter can accept either a value of type E or a SinglyLinkedListNode<E> object.

    Returns boolean

    The delete method returns a boolean value. It returns true if the value or node is found and successfully deleted from the linked list, and false if the value or node is not found in the linked list.

  • Time Complexity: O(n) Space Complexity: O(1)

    The deleteAt function removes an element at a specified index from a linked list and returns the removed element.

    Parameters

    • index: number

      The index parameter represents the position of the element that needs to be deleted in the data structure. It is of type number.

    Returns undefined | E

    The method deleteAt returns the value of the node that was deleted, or undefined if the index is out of bounds.

  • Time Complexity: O(n) Space Complexity: O(1)

    The every function checks if every element in the array satisfies a given predicate.

    Parameters

    • predicate: ElementCallback<E, R, boolean>

      The predicate parameter is a callback function that takes three arguments: the current element being processed, its index, and the array it belongs to. It should return a boolean value indicating whether the element satisfies a certain condition or not.

    • OptionalthisArg: any

      The thisArg parameter is an optional argument that specifies the value to be used as this when executing the predicate function. If thisArg is provided, it will be passed as the this value to the predicate function. If thisArg is

    Returns boolean

    The every method is returning a boolean value. It returns true if every element in the array satisfies the provided predicate function, and false otherwise.

  • Time Complexity: O(n) Space Complexity: O(1)

    The fill function in TypeScript fills a specified range in an array-like object with a given value.

    Parameters

    • value: E

      The value parameter in the fill method represents the element that will be used to fill the specified range in the array.

    • Optionalstart: number = 0

      The start parameter specifies the index at which to start filling the array with the specified value. If not provided, it defaults to 0, indicating the beginning of the array.

    • end: number = ...

      The end parameter in the fill function represents the index at which the filling of values should stop. It specifies the end of the range within the array where the value should be filled.

    Returns this

    The fill method is returning the modified object (this) after filling the specified range with the provided value.

  • Time Complexity: O(n) Space Complexity: O(n)

    The filter function creates a new SinglyLinkedList by iterating over the elements of the current list and applying a callback function to each element to determine if it should be included in the filtered list.

    Parameters

    • callback: ElementCallback<E, R, boolean>

      The callback parameter is a function that will be called for each element in the list. It takes three arguments: the current element, the index of the current element, and the list itself. The callback function should return a boolean value indicating whether the current element should be included in the filtered list or not

    • OptionalthisArg: any

      The thisArg parameter is an optional argument that specifies the value to be used as this when executing the callback function. If thisArg is provided, it will be passed as the this value to the callback function. If thisArg is

    Returns SinglyLinkedList<E, R>

    The filter method is returning a new SinglyLinkedList object that contains the elements that pass the filter condition specified by the callback function.

  • Type Parameters

    • S

    Parameters

    • predicate: ElementCallback<E, R, S>
    • OptionalthisArg: any

    Returns undefined | S

  • Parameters

    • predicate: ElementCallback<E, R, unknown>
    • OptionalthisArg: any

    Returns undefined | E

  • Time Complexity: O(n) Space Complexity: O(1)

    The findIndex function iterates over an array and returns the index of the first element that satisfies the provided predicate function.

    Parameters

    • predicate: ElementCallback<E, R, boolean>

      The predicate parameter in the findIndex function is a callback function that takes three arguments: item, index, and the array this. It should return a boolean value indicating whether the current element satisfies the condition being checked for.

    • OptionalthisArg: any

      The thisArg parameter in the findIndex function is an optional parameter that specifies the value to use as this when executing the predicate function. If provided, the predicate function will be called with thisArg as its this value. If @returns ThefindIndex` method is returning the index of the first element in the array that satisfies the provided predicate function. If no such element is found, it returns -1.

    Returns number

  • Time Complexity: O(n) Space Complexity: O(1)

    The forEach function iterates over each element in an array-like object and calls a callback function for each element.

    Parameters

    • callbackfn: ElementCallback<E, R, void>

      The callbackfn parameter is a function that will be called for each element in the array. It takes three arguments: the current element being processed, the index of the current element, and the array that forEach was called upon.

    • OptionalthisArg: any

      The thisArg parameter is an optional argument that specifies the value to be used as this when executing the callbackfn function. If thisArg is provided, it will be passed as the this value to the callbackfn function. If `thisArg

    Returns void

  • Time Complexity: O(n) Space Complexity: O(1)

    The function getNode in TypeScript searches for a node in a singly linked list based on a given element, node, or predicate.

    Parameters

    • elementNodeOrPredicate:
          | undefined
          | E
          | SinglyLinkedListNode<E>
          | ((node: SinglyLinkedListNode<E>) => boolean)

      elementNodeOrPredicate - The elementNodeOrPredicate parameter in the getNode method can be one of the following types:

    Returns undefined | SinglyLinkedListNode<E>

    The getNode method returns either a SinglyLinkedListNode<E> if a matching node is found based on the provided predicate, or it returns undefined if no matching node is found or if the input parameter is undefined.

  • Time Complexity: O(n) Space Complexity: O(1)

    The function getNodeAt returns the node at a given index in a singly linked list.

    Parameters

    • index: number

      The index parameter is a number that represents the position of the node we want to retrieve from the linked list. It indicates the zero-based index of the node we want to access.

    Returns undefined | SinglyLinkedListNode<E>

    The method getNodeAt(index: number) returns a SinglyLinkedListNode<E> object if the node at the specified index exists, or undefined if the index is out of bounds.

  • Time Complexity: O(n) Space Complexity: O(1)

    The function checks if a given element exists in a collection.

    Parameters

    • element: E

      The parameter "element" is of type E, which means it can be any type. It represents the element that we want to check for existence in the collection.

    Returns boolean

    a boolean value. It returns true if the element is found in the collection, and false otherwise.

  • Time Complexity: O(n) Space Complexity: O(1)

    The function overrides the indexOf method to improve performance by searching for an element in a custom array implementation starting from a specified index.

    Parameters

    • searchElement: E

      The searchElement parameter is the element that you are searching for within the array. The indexOf method will return the index of the first occurrence of this element within the array.

    • OptionalfromIndex: number = 0

      The fromIndex parameter in the indexOf method specifies the index in the array at which to start the search for the searchElement. If provided, the search will begin at the specified index and continue to the end of the array. If not provided, the search will start at index

    Returns number

    The indexOf method is returning the index of the searchElement if it is found in the array starting from the fromIndex. If the searchElement is not found, it returns -1.

  • Time Complexity: O(1) Space Complexity: O(1)

    The function checks if the length of a data structure is equal to zero and returns a boolean value indicating whether it is empty or not.

    Returns boolean

    A boolean value indicating whether the length of the object is equal to 0.

  • Time Complexity: O(1) Space Complexity: O(1)

    The function isNode in TypeScript checks if the input is an instance of SinglyLinkedListNode.

    Parameters

    • elementNodeOrPredicate: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)

      elementNodeOrPredicate - The elementNodeOrPredicate parameter in the isNode function can be one of the following types:

    Returns elementNodeOrPredicate is SinglyLinkedListNode<E>

    The isNode function is checking if the elementNodeOrPredicate parameter is an instance of SinglyLinkedListNode<E>. If it is, the function returns true, indicating that the parameter is a SinglyLinkedListNode<E>. If it is not an instance of SinglyLinkedListNode<E>, the function returns false.

  • Time Complexity: O(n) Space Complexity: O(1)

    The join function in TypeScript returns a string by joining the elements of an array with a specified separator.

    Parameters

    • Optionalseparator: string = ','

      The separator parameter is a string that specifies the character or characters that will be used to separate each element when joining them into a single string. By default, the separator is set to a comma (,), but you can provide a different separator if needed.

    Returns string

    The join method is being returned, which takes an optional separator parameter (defaulting to a comma) and returns a string created by joining all elements of the array after converting it to an array.

  • Time Complexity: O(n) Space Complexity: O(1)

    The function overrides the lastIndexOf method in TypeScript to improve performance by searching for an element in reverse order starting from a specified index.

    Parameters

    • searchElement: E

      The searchElement parameter is the element that you want to find within the array. The lastIndexOf method searches the array for this element starting from the end of the array (or from the specified fromIndex if provided) and returns the index of the last occurrence of the element

    • fromIndex: number = ...

      The fromIndex parameter in the lastIndexOf method specifies the index at which to start searching for the searchElement in the array. If provided, the search will begin at this index and move towards the beginning of the array. If not provided, the search will start at the

    Returns number

    The lastIndexOf method is being overridden to search for the searchElement starting from the specified fromIndex (defaulting to the end of the array). It iterates over the array in reverse order using a custom iterator _getReverseIterator and returns the index of the last occurrence of the searchElement if found, or -1 if not found.

  • Time Complexity: O(n) Space Complexity: O(n)

    The map function takes a callback function and returns a new SinglyLinkedList with the results of applying the callback to each element in the original list.

    Type Parameters

    • EM
    • RM

    Parameters

    • callback: ElementCallback<E, R, EM>

      The callback parameter is a function that will be called for each element in the original list. It takes three arguments: current (the current element being processed), index (the index of the current element), and this (the original list). It should return a value

    • OptionaltoElementFn: ((rawElement: RM) => EM)

      The toElementFn parameter is an optional function that can be used to convert the raw element (RR) to the desired element type (T). It takes the raw element as input and returns the converted element. If this parameter is not provided, the raw element will be used as is.

        • (rawElement): EM
        • Parameters

          • rawElement: RM

          Returns EM

    • OptionalthisArg: any

      The thisArg parameter is an optional argument that allows you to specify the value of this within the callback function. It is used to set the context or scope in which the callback function will be executed. If thisArg is provided, it will be used as the value of

    Returns SinglyLinkedList<EM, RM>

    a new instance of the SinglyLinkedList class with the mapped elements.

  • Time Complexity: O(n) Space Complexity: O(1)

    The pop function removes and returns the value of the last element in a linked list.

    Returns undefined | E

    The method is returning the value of the element that is being popped from the end of the list.

  • Time Complexity: O(n) Space Complexity: O(n)

    The print function logs the elements of an array to the console.

    Returns void

  • Time Complexity: O(1) Space Complexity: O(1)

    The push function adds a new element or node to the end of a singly linked list.

    Parameters

    • elementOrNode: E | SinglyLinkedListNode<E>

      The elementOrNode parameter in the push method can accept either an element of type E or a SinglyLinkedListNode<E> object.

    Returns boolean

    The push method is returning a boolean value, specifically true.

  • Time Complexity: O(k) Space Complexity: O(k)

    The function pushMany iterates over elements and pushes them into a data structure, applying a transformation function if provided.

    Parameters

    • elements: Iterable<E, any, any> | Iterable<R, any, any> | Iterable<SinglyLinkedListNode<E>, any, any>

      The elements parameter in the pushMany function can accept an iterable containing elements of type E, R, or SinglyLinkedListNode<E>.

    Returns boolean[]

    The pushMany function returns an array of boolean values indicating whether each element was successfully pushed into the data structure.

  • Parameters

    • callbackfn: ReduceLinearCallback<E>

    Returns E

  • Parameters

    • callbackfn: ReduceLinearCallback<E>
    • initialValue: E

    Returns E

  • Type Parameters

    • U

    Parameters

    • callbackfn: ReduceLinearCallback<E, U>
    • initialValue: U

    Returns U

  • Time Complexity: O(n) Space Complexity: O(1)

    The reverse function reverses the order of the nodes in a singly linked list.

    Returns this

    The reverse() method does not return anything. It has a return type of void.

  • Time Complexity: O(n) Space Complexity: O(1)

    This function searches for a specific element in a singly linked list based on a given node or predicate.

    Parameters

    Returns undefined | E

    The get method returns the value of the first node in the singly linked list that satisfies the provided predicate function. If no such node is found, it returns undefined.

  • Time Complexity: O(n) Space Complexity: O(1)

    The function setAt(index, value) updates the value at a specified index in a data structure if the index exists.

    Parameters

    • index: number

      The index parameter in the setAt method refers to the position in the data structure where you want to set a new value.

    • value: E

      The value parameter in the setAt method represents the new value that you want to set at the specified index in the data structure.

    Returns boolean

    The setAt method returns a boolean value - true if the value at the specified index is successfully updated, and false if the index is out of bounds (i.e., the node at that index does not exist).

  • Time Complexity: O(1) Space Complexity: O(1)

    The shift() function removes and returns the value of the first element in a linked list.

    Returns undefined | E

    The value of the removed node.

  • Time Complexity: O(m) Space Complexity: O(m)

    The slice method is overridden to improve performance by creating a new instance and iterating through the array to extract a subset based on the specified start and end indices.

    Parameters

    • Optionalstart: number = 0

      The start parameter in the slice method specifies the index at which to begin extracting elements from the array. If no start parameter is provided, the default value is 0, indicating that extraction should start from the beginning of the array.

    • end: number = ...

      The end parameter in the slice method represents the index at which to end the slicing of the array. If not provided, it defaults to the length of the array.

    Returns this

    The slice method is returning a new instance of the array implementation with elements sliced from the original array based on the start and end parameters.

  • Time Complexity: O(n) Space Complexity: O(1)

    The "some" function checks if at least one element in a collection satisfies a given predicate.

    Parameters

    • predicate: ElementCallback<E, R, boolean>

      The predicate parameter is a callback function that takes three arguments: value, index, and array. It should return a boolean value indicating whether the current element satisfies the condition.

    • OptionalthisArg: any

      The thisArg parameter is an optional argument that specifies the value to be used as the this value when executing the predicate function. If thisArg is provided, it will be passed as the this value to the predicate function. If `thisArg

    Returns boolean

    a boolean value. It returns true if the predicate function returns true for any element in the collection, and false otherwise.

  • Time Complexity: O(n log n) Space Complexity: O(n)

    The sort function in TypeScript sorts the elements of a collection using a specified comparison function.

    Parameters

    • OptionalcompareFn: ((a: E, b: E) => number)

      The compareFn parameter is a function that defines the sort order. It takes two elements a and b as input and returns a number indicating their relative order. If the returned value is negative, a comes before b. If the returned value is positive, @returns Thesort` method is returning the instance of the object on which it is called (this), after sorting the elements based on the provided comparison function (compareFn).

        • (a, b): number
        • Parameters

          Returns number

    Returns this

  • Time Complexity: O(n) Space Complexity: O(1)

    The function splice in TypeScript overrides the default behavior to remove and insert elements in a singly linked list while handling boundary cases.

    Parameters

    • start: number

      The start parameter in the splice method indicates the index at which to start modifying the list. It specifies the position where elements will be added or removed.

    • OptionaldeleteCount: number = 0

      The deleteCount parameter in the splice method specifies the number of elements to remove from the array starting at the specified start index. If deleteCount is not provided, it defaults to 0, meaning no elements will be removed but new elements can still be inserted at

    • Rest...items: E[]

      The items parameter in the splice method represents the elements to be inserted into the list at the specified start index. These elements will be inserted in place of the elements that are removed from the list. The splice method allows you to add new elements to the list while

    Returns this

    The splice method is returning a SinglyLinkedList containing the elements that were removed from the original list during the splice operation.

  • Time Complexity: O(n) Space Complexity: O(n)

    The toArray function converts a linked list into an array.

    Returns E[]

    The toArray() method is returning an array of type E[].

  • Time Complexity: O(n) Space Complexity: O(n)

    The function toReversedArray takes an array and returns a new array with its elements in reverse order.

    Returns E[]

    The toReversedArray() function returns an array of elements of type E in reverse order.

  • Time Complexity: O(n) Space Complexity: O(n)

    The print function logs the elements of an array to the console.

    Returns E[]

  • Time Complexity: O(1) Space Complexity: O(1)

    The unshift function adds a new element or node to the beginning of a singly linked list in TypeScript.

    Parameters

    • elementOrNode: E | SinglyLinkedListNode<E>

      The elementOrNode parameter in the unshift method can be either an element of type E or a SinglyLinkedListNode containing an element of type E.

    Returns boolean

    The unshift method is returning a boolean value, specifically true.

  • Time Complexity: O(k) Space Complexity: O(k)

    The function unshiftMany iterates over elements and adds them to a data structure, optionally converting them using a provided function.

    Parameters

    • elements: Iterable<E, any, any> | Iterable<R, any, any> | Iterable<SinglyLinkedListNode<E>, any, any>

      The elements parameter in the unshiftMany function can accept an iterable containing elements of type E, R, or SinglyLinkedListNode<E>. The function iterates over each element in the iterable and performs an unshift operation on the linked list for each

    Returns boolean[]

    The unshiftMany function is returning an array of boolean values, where each value represents the result of calling the unshift method on the current instance of the class.

  • Time Complexity: O(n) Space Complexity: O(n)

    The function returns an iterator that yields all the values in the object.

    Returns IterableIterator<E, any, any>

  • Time Complexity: O(n) Space Complexity: O(n)

    The fromArray function creates a new SinglyLinkedList instance and populates it with the elements from the given array.

    Type Parameters

    • E

    Parameters

    • data: E[]

      The data parameter is an array of elements of type E.

    Returns SinglyLinkedList<E, any>

    The fromArray function returns a SinglyLinkedList object.