πŸ‘‰Singly Linked List

What is a Singly Linked List?

The first node in a linked list is referred to as the head of the linked list, while the last node in the linked list, whose next property points to the null value, is known as the tail of the linked list.

Singly Linked List

Key characteristics include:

  • Dynamic Size: Nodes can be added or removed as needed, making the list flexible in size.

  • Efficient Insertion/Deletion: Adding or removing nodes at any position typically takes constant time (), as you only need to update a few pointers.

  • No Random Access: Accessing a specific element requires traversing the list from the beginning, as there's no direct indexing like in arrays.

  • Sequential Access: Nodes are accessed sequentially, one after the other, following the pointers.

Common use cases:

  • Implementing stacks and queues

  • Representing graphs

  • Implementing undo/redo functionality

  • Dynamic memory allocation

Big O Notation of Singly Linked List Operations

Operation
Time Complexity
Explanation

Insertion (head)

O(1)O(1)

Only needs to update a few pointers. You only need to update the head pointer.

Insertion (tail)

O(1)O(1) (average)

Assuming a tail pointer, it's also constant time. You can directly access the tail and update its next pointer.

Insertion (middle)

O(n)O(n)

Requires finding the insertion point by traversing nodes.

Deletion (head)

O(1)O(1)

Updating pointers for the first node is constant time.

Deletion (tail)

O(n)O(n)

Finding the tail node without a tail pointer requires traversal.

Deletion (middle)

O(n)O(n)

Finding the node to delete involves traversal.

Search

O(n)O(n)

Elements aren't indexed, so you need to check each one.

Access by index

O(n)O(n)

No direct access; you must traverse from the beginning.

Traversal

O(n)O(n)

Visiting each node takes time proportional to the list's length.

Implementation of Singly Linked List In JavaScript

Last updated