Open In App

Linked List in C++

Last Updated : 02 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++, a linked list is a linear data structure that allows the users to store data in non-contiguous memory locations. A linked list is defined as a collection of nodes where each node consists of two members which represents its value and a next/previous pointer which stores the address for the next/previous node.

Linked List Representation in C++

In C++, linked lists are basically represented by a pointer to the first node, which is commonly referred to as the "head" of the list. Each node in the list is defined by a structure that includes a data field and a pointer pointing to the same type of structure. This type of structure is known as a self-referential structure.

Types of Linked Lists

Based on the structure of linked lists, they can be classified into several types:

  1. Singly Linked List
  2. Doubly Linked List
  3. Circular Linked List

1. Singly Linked List in C++

The singly linked list is the simplest form of linked list in which the node contains two members data and a next pointer that stores the address of the next node. Each node is a singly linked list is connected through the next pointer and the next pointer of the last node points to NULL denoting the end of the linked list. The following diagram describes the structure of a singly linked 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++
// Structure to represent the 
// singly 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 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++

The doubly linked list is the modified version of the singly linked list where each node of the doubly linked consists of three data members data, next and prev. The prev is a pointer that stores the address of the previous node in the linked list sequence. Each node in a doubly linked list except the first and the last node is connected with each other through the prev and next pointer. The prev pointer of the first node and the next pointer of the last node points to NULL in the doubly linked list. The following diagram describes the structure of a doubly linked list:

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++
// Structure to represent the doubly linked list
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++

The circular linked list is almost same as the singly linked list but with a small change. In a circular linked list the next pointer of the last node points to the first node of the linked list rather than pointing to NULL, this makes this data structure circular in nature which is used in various applications like media players. The following diagram describes the structure of a circular linked list:

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.

Disadvantages of Linked List

Following points shows the disadvantages of linked list:

  • Direct access to element is not possible in a linked list; it requires traversing from the start to reach a specific position.
  • Each node requires extra memory for storing a pointer.
  • Linked lists are more complex to implement and manage.
  • Pointers can lead to bugs, memory leaks, or segmentation fault.

Next Article
Practice Tags :

Similar Reads