A linked list is a linear collection of data elements, whose order is not given by their physical placement in memory. Instead, each element points to the next. It is a data structure consisting of a collection of nodes which together represent a sequence. In its most basic form, each node contains: data, and a reference (in other words, a link) to the next node in the sequence.
Linked lists are among the simplest and most common data structures. They can be used to implement several other common abstract data types, including lists, stacks, queues, and S-expressions.
Install with npm
npm i javascript-algo-ds
Import in your javascript file as:
const LinkedList = require('javascript-algo-ds/src/data-structure/linked-list/linkedList')Create empty linked list:
const list = new LinkedList()new LinkedList(compareFunction)- compareFunction - function
- Default value - undefined
- head - node of type LinkedListNode pointing to head of the list
- tail - node of type LinkedListNode pointing to tail of the list
- size - number of elements in the list
- compare - comparator of type Comparator
- UNDEFINED_HEAD - Symbol representing "head is not defined"
- list.prepend(value)
- list.append(value)
- list.delete(value)
- list.find(value, index)
- list.deleteHead()
- list.deleteTail()
- list.get(index)
- list.traverse(callback)
- list.toArray()
- list.toString()
Adds value at the start of the list.
- value - T
- The value to be added.
Returns this
Adds value at the end of the list.
- value - T
- The value to be added.
Returns this
Deletes value from the list.
- value - T
- The value to be deleted.
Returns this
Finds value in the list.
- value - T
- Value to find.
- index - boolean
- Default value - true
- If true, returns the index of
valuein the list (0-indexing) if found, -1 if not found. - If false, returns the object itself if found,
nullif not found. - Optional
Type - number | LinkedListNode
Returns index or the object itself
Deletes head from the list.
Type - LinkedListNode | null
Returns removed head of the list if list is not empty or null otherwise.
Deletes tail from the list.
Type - LinkedListNode | null
Returns removed tail of the list if list is not empty or null otherwise.
Get a node at index index.
- index - number
- The index of the node.
Type - LinkedListNode | symbol
Returns the node at index, null if index is greater than size of list, LinkedList.UNDEFINED_HEAD if list is empty
Traverses the list from head to tail.
- callback - TraverseCallback
- The callback to which the current node and its index is passed.
- Arguments -
- curr - LinkedListNode --- the current node
- index - number --- index of current node (0-indexing)
- size - number --- number of elements in the list
- list - LinkedList --- the list itself
- Throws error if callback is not defined
Returns the object returned by the callback
Converts the list into an array and returns it.
Type - Array
Converts the list into string of form "1 -> 2 -> 3" and returns it.
Type - String
@static
Creates the list from an array
- array - T[]
- Array of objects
- compareFunction - function
- Default value - undefined
Type - LinkedList