# SinglyLinkedList | data-structure-typed
Documentation for data-structure-typed

# 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

- Defined in src/data-structures/linked-list/singly-linked-list.ts:96

#####
Index

### Constructors

constructor

### Properties

head
size
tail

### Accessors

length

### Methods

[iterator]
append
clear
filter
find
findIndex
findNode
findNodeIndex
forEach
get
getNode
insertAfter
insertAt
insertBefore
map
merge
pop
prepend
push
reduce
removeAt
removeFromAnyEnd
removeNode
reverse
shift
slice
sort
toArray
toString
from

## Constructors

### constructor

- new SinglyLinkedList<NodeData>(...args): SinglyLinkedList <NodeData>

- #### Type Parameters

  - #### NodeData = any

  #### Parameters

  - ##### `Rest` ...args: NodeData[]

  #### Returns SinglyLinkedList <NodeData>

  - Defined in src/data-structures/linked-list/singly-linked-list.ts:105

## Properties

### head

head: null | SinglyLinkedListNode <NodeData>

The head of the list, the first node

- Defined in src/data-structures/linked-list/singly-linked-list.ts:99

### `Private` size

size: number

Internal size reference

- Defined in src/data-structures/linked-list/singly-linked-list.ts:103

### tail

tail: null | SinglyLinkedListNode <NodeData>

The tail of the list, the last node

- Defined in src/data-structures/linked-list/singly-linked-list.ts:101

## Accessors

### length

- get length(): number

- The length of the list

  #### Returns number

  - Defined in src/data-structures/linked-list/singly-linked-list.ts:118

## Methods

### [iterator]

- [iterator](): IterableIterator<NodeData>

- The iterator implementation

      const list = new LinkedList(1, 2, 3);
      for (const val of list) { log(val); } // 1 2 3

  #### Returns IterableIterator<NodeData>

  - Defined in src/data-structures/linked-list/singly-linked-list.ts:754

### append

- append(...args): SinglyLinkedList <NodeData>

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

  - Defined in src/data-structures/linked-list/singly-linked-list.ts:263

### clear

- clear(): SinglyLinkedList <NodeData>

- Removes all nodes from a list

      list.clear();

  #### Returns SinglyLinkedList <NodeData>

  - Defined in src/data-structures/linked-list/singly-linked-list.ts:555

### filter

- filter(f, reverse?): 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

        - ##### data: NodeData

        - ##### index: number

        - ##### list: SinglyLinkedList <NodeData>

        #### Returns boolean

  - ##### reverse: boolean = false

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

  #### Returns SinglyLinkedList <{} | NodeData>

  - Defined in src/data-structures/linked-list/singly-linked-list.ts:669

### find

- find(f): undefined | 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

        - ##### data: NodeData

        - ##### index: number

        - ##### list: SinglyLinkedList <NodeData>

        #### Returns boolean

  #### Returns undefined | NodeData

  - Defined in src/data-structures/linked-list/singly-linked-list.ts:228

### findIndex

- findIndex(f): number

- 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

        - ##### data: NodeData

        - ##### index: number

        - ##### list: SinglyLinkedList <NodeData>

        #### Returns boolean

  #### Returns number

  - Defined in src/data-structures/linked-list/singly-linked-list.ts:245

### findNode

- findNode(f): undefined | SinglyLinkedListNode <NodeData>

- 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

        - ##### data: NodeData

        - ##### index: number

        - ##### list: SinglyLinkedList <NodeData>

        #### Returns boolean

  #### Returns undefined | SinglyLinkedListNode <NodeData>

  - Defined in src/data-structures/linked-list/singly-linked-list.ts:211

### findNodeIndex

- findNodeIndex(f): undefined | {
  index: number;
  node: 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

        - ##### data: NodeData

        - ##### index: number

        - ##### list: SinglyLinkedList <NodeData>

        #### Returns boolean

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

  - Defined in src/data-structures/linked-list/singly-linked-list.ts:179

### forEach

- forEach(f, reverse?): void

- 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

        - ##### data: any

        - ##### index: number

        - ##### list: SinglyLinkedList <NodeData>

        #### Returns any

  - ##### reverse: boolean = false

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

  #### Returns void

  - Defined in src/data-structures/linked-list/singly-linked-list.ts:623

### get

- get(index): undefined | NodeData

- Get the node val at a specified index, zero based

      new LinkedList(1, 2, 3).get(0); // 1

  #### Parameters

  - ##### index: number

    to retrieve val at

  #### Returns undefined | NodeData

  - Defined in src/data-structures/linked-list/singly-linked-list.ts:141

### getNode

- getNode(index): undefined | SinglyLinkedListNode <NodeData>

- Get the node at index, zero based

      new LinkedList(1, 2, 3).getNode(0);
      // { prev: null, val: 1, next: SinglyLinkedListNode }

  #### Parameters

  - ##### index: number

  #### Returns undefined | SinglyLinkedListNode <NodeData>

  - Defined in src/data-structures/linked-list/singly-linked-list.ts:153

### insertAfter

- insertAfter(referenceNode, val): SinglyLinkedList <NodeData>

- Insert a new node after this one

      const list = new LinkedList(2, 3);
      list.insertAfter(list.head, 1); // 1 <=> 2 <=> 3

  #### Parameters

  - ##### referenceNode: SinglyLinkedListNode <NodeData>

    The reference node

  - ##### val: NodeData

    Data to be saved in the node

  #### Returns SinglyLinkedList <NodeData>

  - Defined in src/data-structures/linked-list/singly-linked-list.ts:484

### insertAt

- insertAt(index, val): SinglyLinkedList <NodeData>

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

  - Defined in src/data-structures/linked-list/singly-linked-list.ts:324

### insertBefore

- insertBefore(referenceNode, val): SinglyLinkedList <NodeData>

- Insert a new node before the reference node

      const list = new LinkedList(1, 3);
      list.insertBefore(list.tail, 2); // 1 <=> 2 <=> 3

  #### Parameters

  - ##### referenceNode: SinglyLinkedListNode <NodeData>

    The node reference

  - ##### val: NodeData

    Data to save in the node

  #### Returns SinglyLinkedList <NodeData>

  - Defined in src/data-structures/linked-list/singly-linked-list.ts:400

### map

- map(f, reverse?): 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

        - ##### data: any

        - ##### index: number

        - ##### list: SinglyLinkedList <NodeData>

        #### Returns any

  - ##### reverse: boolean = false

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

  #### Returns SinglyLinkedList <{} | NodeData>

  - Defined in src/data-structures/linked-list/singly-linked-list.ts:649

### merge

- merge(list): void

- 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

  - ##### list: SinglyLinkedList <NodeData>

    The list to be merged

  #### Returns void

  - Defined in src/data-structures/linked-list/singly-linked-list.ts:533

### pop

- pop(): undefined | NodeData

- Remove the last node from the list and return the val of the removed node or undefined if the list was empty

      new LinkedList(1, 2, 3).pop(); // 3

  #### Returns undefined | NodeData

  - Defined in src/data-structures/linked-list/singly-linked-list.ts:518

### prepend

- prepend(...args): SinglyLinkedList <NodeData>

- Prepend any number of val arguments to the list. The argument list is prepended as a block to reduce confusion:

      new LinkedList(3, 4).prepend(0, 1, 2); // [0, 1, 2, 3, 4]

  #### Parameters

  - ##### `Rest` ...args: NodeData[]

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

  #### Returns SinglyLinkedList <NodeData>

  - Defined in src/data-structures/linked-list/singly-linked-list.ts:298

### push

- push(...args): number

- Synonym for append

      new LinkedList(1).push(2).push(3, 4); // 1 <=> 2 <=> 3 <=> 4

  #### Parameters

  - ##### `Rest` ...args: NodeData[]

    Data to be stored, takes any number of arguments

  #### Returns number

  - Defined in src/data-structures/linked-list/singly-linked-list.ts:285

### reduce

- reduce(f, start?, reverse?): any

- 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

  - Defined in src/data-structures/linked-list/singly-linked-list.ts:692

### removeAt

- removeAt(index): undefined | SinglyLinkedListNode <NodeData>

- Remove the node at the specified index

      new LinkedList(1, 2, 3).removeAt(2); // { prev: null, val: 3, next: null, list: null }

  #### Parameters

  - ##### index: number

    Index at which to remove

  #### Returns undefined | SinglyLinkedListNode <NodeData>

  - Defined in src/data-structures/linked-list/singly-linked-list.ts:386

### `Private` removeFromAnyEnd

- removeFromAnyEnd(node): undefined | NodeData

- Private helper function to reduce duplication of pop() and shift() methods

  #### Parameters

  - ##### node: null | SinglyLinkedListNode <NodeData>

  #### Returns undefined | NodeData

  - Defined in src/data-structures/linked-list/singly-linked-list.ts:764

### removeNode

- removeNode(node): SinglyLinkedListNode <NodeData>

- Remove the specified node from the list and return the removed node afterwards.

      const list = new LinkedList(1, 2, 3);
      list.removeNode(list.tail); // { prev: null, val: 3, next: null, list: null }

  #### Parameters

  - ##### node: SinglyLinkedListNode <NodeData>

    The node to be removed

  #### Returns SinglyLinkedListNode <NodeData>

  - Defined in src/data-structures/linked-list/singly-linked-list.ts:351

### reverse

- reverse(): SinglyLinkedList <NodeData>

- The reverse() function reverses the list in place and returns the list itself.

      new LinkedList(1, 2, 3).reverse(); // 3 <=> 2 <=> 1

  #### Returns SinglyLinkedList <NodeData>

  - Defined in src/data-structures/linked-list/singly-linked-list.ts:601

### shift

- shift(): undefined | NodeData

- Remove the first node from the list and return the val of the removed node or undefined

      new LinkedList(1, 2, 3).shift(); // 1

  #### Returns undefined | NodeData

  - Defined in src/data-structures/linked-list/singly-linked-list.ts:507

### slice

- slice(start, end?): SinglyLinkedList <{} | NodeData>

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

  - Defined in src/data-structures/linked-list/singly-linked-list.ts:575

### sort

- sort(compare): 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>

  - Defined in src/data-structures/linked-list/singly-linked-list.ts:423

### toArray

- toArray(): NodeData[]

- Convert the linked list to an array

      new LinkedList(1, 2, 3).toArray(); // [1, 2, 3]

  #### Returns NodeData[]

  - Defined in src/data-structures/linked-list/singly-linked-list.ts:732

### toString

- toString(separator?): string

- 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

  - Defined in src/data-structures/linked-list/singly-linked-list.ts:743

### `Static` from

- from<T>(iterable): SinglyLinkedList <T>

- Convert any iterable to a new linked list

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

  #### Type Parameters

  - #### T

  #### Parameters

  - ##### iterable: Iterable<T>

    Any iterable datatype like Array or Map

  #### Returns SinglyLinkedList <T>

  - Defined in src/data-structures/linked-list/singly-linked-list.ts:130

Generated using TypeDoc