Collection Kit

LinkedList

Linear

A LinkedList is a linear data structure where each element (node) contains a reference (link) to the next node in the sequence. This allows for efficient insertion and deletion of elements, as nodes can be added or removed without shifting other elements.

Import Statement
import { LinkedList } from "collection-kit";

Key Features

  • Dynamic Size: Can grow and shrink in size as needed.
  • Efficient Insertions/Deletions: O(1) time complexity for adding or removing elements at the beginning or end.
  • Sequential Access: O(n) time complexity for accessing elements by index.

Common Operations

Add
Insert an element at the beginning, end, or a specific index.
Remove
Delete an element by value or index.
Get
Retrieve an element at a specific index.
Size
Return the number of elements in the list.

Example Code

import { LinkedList } from "collection-kit";

const list = new LinkedList();
list.add(10);
list.add(20);
list.add(30);

console.log("Size:", list.size());         // 3
console.log("First element:", list.get(0)); // 10
console.log("All elements:");
for (let i = 0; i < list.size(); i++) {
  console.log(`Element ${i}:`, list.get(i));
}