Linked List in C++

Last Updated : 30 May, 2026

A linked list in C++ is a linear data structure used to store data in non-contiguous memory locations. Unlike arrays, linked lists store elements dynamically and connect them using pointers.

  • Linked lists store elements dynamically in memory.
  • Each element is called a node.
  • Nodes are connected using pointers.

Linked List Representation

A linked list is represented by a pointer called head, which points to the first node of the list. Each node typically contains:

  • Data: Stores the actual value.
  • Next Pointer: Stores the address of the next node.

Node Structure:

C++
struct Node
{
    int data;
    Node* next;
};

Explanation

  • data stores the value of the node.
  • next stores the address of the next node.
  • The last node points to NULL.

Types of Linked Lists

Based on their structure, linked lists are classified into the following types:

1. Singly Linked List in C++

A Singly linked List is the simplest form of linked list. Each node contains data and a pointer to the next node. The last node's next pointer points to NULL, indicating the end of the list.

singly-linked-list-in-c

Singly Linked List Representation

Singly linked list can be represented as a pointer to the first node, where each node contains:

  • Data: Actual information is stored.
  • Next: Pointer to the next node.
C++
struct Node {
  
  // Data field - can be of 
  // any type and count
  int data;
  
  // Pointer to the next node
  struct Node* next;
}

Basic Operations for Singly Linked List

Operation

Operation TypeDescriptionTime ComplexitySpace Complexity

Insertion

At BeginningInsert a new node at the start of a linked list.O (1)O (1)
At the EndInsert a new node at the end of the linked list.O (N)O (1)
At Specific PositionInsert a new node at a specific position in a linked list.O (N)O (1)

Deletion

From BeginningDelete a node from the start of a linked listO (1)O (1)
From the EndDelete a node at the end of a linked list.O (N)O (1)
A Specific NodeDelete a node from a specific position of a linked list. O (N)O (1)

Traversal

Traverse the linked list from start to end.O (N)O (1)

2. Doubly Linked List in C++

A doubly linked list is an extension of a singly linked list where each node contains pointers to both the next and previous nodes. This allows traversal in both forward and backward directions.

doubly-linked-list-in-c

Doubly Linked List Representation

Doubly linked list can be represented as pointer to the first node where each node contains:

  • Data: Actual information is stored.
  • Next: Pointer stores the address of next node.
  • prev: Pointer stores the address of previous node.
C++
struct Node {
  
  // Data field - can be of any type and count
  int data;
  
  // Pointer to the next node
  struct Node* next;
  
  // Pointer to the previous node
  struct Node* prev;
}

Basic Operations for Doubly Linked List

Operation

Operation TypeDescriptionTime ComplexitySpace Complexity

Insertion

At BeginningInsert a new node at the start of a linked list.O (1)O (1)
At the EndInsert a new node at the end of the linked list.O (N)O (1)
At Specific PositionInsert a new node at a specific position in a linked list.O (N)O (1)

Deletion

From BeginningDelete a node from the start of a linked listO (1)O (1)
From the EndDelete a node at the end of a linked list.O (N)O (1)
A Specific NodeDelete a node from a specific position of a linked list. O (N)O (1)

Traversal

Traverse the linked list from start to end or vice versa.O (N)O (1)

3. Circular Linked List in C++

A circular linked list is similar to a singly linked list, but the last node points back to the first node instead of pointing to NULL. This creates a circular structure.

Circular-linked-list-in-c

Circular Linked List Representation

Circular linked list can be represented as pointer to the first node where each node contains:

  • Data: Actual information is stored.
  • Next: Pointer to the next node and last node Next is pointed to the first node of the linked list.
C++
// Structure to represent the circular linked list
struct Node {
  
  // Data field - can be of any type and count
  int data;
  
  // Pointer to the next node
  struct Node* next;
}

Basic Operations for Circular Linked List

Operation

Operation TypeDescriptionTime ComplexitySpace Complexity

Insertion

At BeginningInsert a new node at the start of a linked list.O (N)O (1)
At the EndInsert a new node at the end of the linked list.O (N)O (1)
At Specific PositionInsert a new node at a specific position in a linked list.O (N)O (1)

Deletion

From BeginningDelete a node from the start of a linked listO (N)O (1)
From the EndDelete a node at the end of a linked list.O (N)O (1)
A Specific NodeDelete a node from a specific position of a linked list. O (N)O (1)

Traversal

Traverse the linked list from start to end or vice versa.O (N)O (1)

Applications of Linked Lists

Following are some common applications of the linked list data structure:

Advantages of Linked List

Advantages of linked list are mentioned below:

  • Inserting and deleting node is efficient because no need to shifting like in array.
  • Linked lists have dynamic size, which allows them to grow or shrink during runtime.
  • Memory is utilized more efficiently as linked lists do not require a pre-allocated size, reducing wasted space.
  • Efficient for those operations where we need large or frequently changing datasets.
  • Linked list used non-contiguous memory blocks, so it is useful for those application where memory is needed.

Limitations of Linked List

Despite its flexibility and dynamic nature, a linked list has some limitations:

  • No Direct Access: Elements cannot be accessed directly using an index. To reach a specific node, the list must be traversed from the beginning.
  • Additional Memory Requirement: Each node requires extra memory to store one or more pointer variables.
  • Sequential Searching: Searching for an element can be slower because nodes must be visited one by one.
  • Complex Implementation: Linked lists are generally more complex to implement and maintain compared to arrays.
Comment