🖇️

Linked List

Definition:

A linked list is a linear data structure in which elements are stored in nodes, and each node points to the next node in the sequence, forming a chain-like structure. Unlike arrays, linked lists do not have a fixed size in memory, and their size can be dynamically adjusted during runtime.

Key Components of a Linked List:

  1. Node:
    • The basic building block of a linked list is a node.
    • Each node contains two fields:
      • Data: The actual information stored in the node.
      • Next: A reference (or link) to the next node in the sequence.
  1. Head:
    • The first node in the linked list is called the head.
    • The head points to the beginning of the list.
  1. Tail:
    • The last node in the linked list is called the tail.
    • The tail's "next" reference is typically set to null, indicating the end of the list.

Types of Linked Lists:

  1. Singly Linked List:
    • Each node points to the next node in the sequence.
    • It only allows for traversing the list in one direction (forward).
  1. Doubly Linked List:
    • Each node has two references: one to the next node and another to the previous node.
    • Allows for traversal in both forward and backward directions.
  1. Circular Linked List:
    • The last node in the list points back to the first node, creating a circular structure.

Time Complexity:

OperationSingly Linked List (SLL)Doubly Linked List (DLL)
DisplayO(n)O(n)
SearchO(n)O(n)
Insertion at the Beginning (SLL/DLL)O(1)O(1)
Insertion at the End (SLL/DLL)O(n)O(1)
Insertion After Node (SLL/DLL)O(n)O(1)
Insertion Before Node (DLL)N/AO(1)
Insertion at Position (SLL/DLL)O(n)O(n)
Deletion at the Beginning (SLL/DLL)O(1)O(1)
Deletion at the End (SLL/DLL)O(n)O(1)
Deletion After Node (SLL/DLL)O(n)O(1)
Deletion Before Node (DLL)N/AO(1)
Deletion at Position (SLL/DLL)O(n)O(n)

Difference Between Single Linked List and Double Linked List:

FeatureSingle Linked ListDouble Linked List
Node StructureEach node contains data and a pointer to the next node.Each node contains data, a pointer to the next node, and a pointer to the previous node.
TraversalCan only be traversed in the forward direction.Can be traversed in both the forward and backward direction.
InsertionRequires the location of the node before the insertion point.Only requires the location of the insertion point.
DeletionRequires the location of the node before the deletion point.Only requires the location of the node to be deleted.
Memory UsageUses less memory as it requires only one pointer per node.Uses more memory as it requires two pointers per node.
ApplicationsSuitable for applications where only forward traversal is needed, such as stacks.Suitable for applications where both forward and backward traversal is needed, such as implementing a Music Player Playlist functionality.

Drawbacks of Single Linked List:

  1. Unidirectional Traversal
  1. Inefficient Removal of Nodes
  1. No Direct Access to Previous Node
  1. Extra Pointer for Tail
  1. Limited Application in Certain Algorithms
  1. Difficulty in Finding the kth Element from the End
  1. Not Suitable for Some Algorithms
  1. Difficulty in Detecting Loop

Differences between Linked Lists and Arrays:

FeatureLinked List (LL)Array
Memory AllocationDynamic, non-contiguousStatic, contiguous
Size FlexibilityDynamic, can change during runtimeFixed, predefined size
Insertion/DeletionEfficient for insertions/deletions at any positionInefficient for insertions/deletions in the middle
Random AccessInefficient, O(n) time complexity for accessEfficient, O(1) time complexity for access
Memory OverheadHigher due to storage of pointersLower, as no additional pointers
Memory UsageMay use more memory due to pointersEfficient usage of memory
ContiguityNon-contiguous, scattered in memoryContiguous, stored in a single block
Implementation ComplexityGenerally simpler to implementStraightforward implementation
Size DeterminationNot required to specify size initiallyRequires specifying size at declaration
Application FlexibilityWell-suited for dynamic data structuresSuitable for scenarios with fixed-size data

Advantages and Disadvantages of Linked Lists:

Advantages of Linked Lists:

  1. Dynamic Size:
    • Advantage: Easily accommodates varying data sizes as memory is dynamically allocated.
    • Use Case: Ideal for situations where the size of the data is not known in advance.
  1. Efficient Insertion and Deletion:
    • Advantage: Adding or removing elements is efficient, requiring adjustments to pointers.
    • Use Case: Suitable for scenarios involving frequent insertions and deletions.
  1. No Wastage of Memory:
    • Advantage: Memory utilization is efficient as each element can be allocated precisely as needed.
    • Use Case: Useful when optimizing memory usage is a critical concern.
  1. Ease of Implementation:
    • Advantage: Simple to implement and understand, making it accessible for various applications.
    • Use Case: Commonly used in educational settings and introductory programming courses.
  1. Versatility:
    • Advantage: Supports various data structures, including stacks, queues, and symbol tables.
    • Use Case: Versatile for implementing different algorithms and data structures.

Disadvantages of Linked Lists:

  1. Sequential Access:
    • Disadvantage: Accessing elements requires sequential traversal from the beginning.
    • Impact: Inefficient for scenarios requiring random access or searching.
  1. Extra Memory Usage:
    • Disadvantage: Additional memory is needed for storing pointers, increasing overhead.
    • Impact: Higher space complexity compared to arrays in certain cases.
  1. Complexity of Implementation:
    • Disadvantage: Implementing linked lists can be more complex than arrays.
    • Impact: May pose challenges in scenarios where simplicity is a priority.
  1. Lack of Cache Locality:
    • Disadvantage: Poor cache locality can result in suboptimal performance.
    • Impact: May affect performance in applications with high memory access patterns.
  1. Memory Overhead:
    • Disadvantage: Requires additional memory for storing pointers, impacting efficiency.
    • Impact: Increases memory overhead, particularly in resource-constrained environments.

Applications of Single Linked List:

1. Dynamic Memory Allocation

2. Implementation of Stacks

3. Implementation of Queues

4. Traversal and Search Operations

5. Undo Mechanisms