Linked List in C++

Last Updated : 31 Aug, 2026

A linked list is a linear data structure in which elements are stored in nodes located at non-contiguous memory locations. Each node contains data and a pointer that connects it to another node.

  • Nodes are dynamically allocated and connected using pointers.
  • The first node is accessed through a pointer called head.
  • Linked lists can grow or shrink dynamically during program execution.
C++
#include <iostream>
using namespace std;

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

int main() {
    Node* head = new Node{10, nullptr};
    head->next = new Node{20, nullptr};
    head->next->next = new Node{30, nullptr};

    Node* temp = head;

    while (temp != nullptr) {
        cout << temp->data << " ";
        temp = temp->next;
    }

    return 0;
}

Output
10 20 30 

Explanation: Each node stores a value and a pointer to the next node. The head pointer stores the address of the first node, while the last node points to nullptr.

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.
  • The last node points to NULL.

struct Node {

int data;

Node* next;

};

Types of Linked Lists

Linked lists are mainly classified based on how their nodes are connected:

1. Singly Linked List

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
C++
#include <iostream>
using namespace std;

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

int main() {
    Node* head = new Node{10, nullptr};
    head->next = new Node{20, nullptr};
    head->next->next = new Node{30, nullptr};

    Node* temp = head;

    while (temp != nullptr) {
        cout << temp->data << " ";
        temp = temp->next;
    }

    return 0;
}

Output
10 20 30 

Explanation: head points to the first node, and each node points to the next node. The last node points to nullptr, marking the end of the list.

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)

Note: Insertion or deletion at a known node can be O(1), but finding that node may require O(N) traversal.

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
  • Data: Actual information is stored.
  • Next: Pointer stores the address of next node.
  • prev: Pointer stores the address of previous node.
C++
#include <iostream>
using namespace std;

struct Node {
    int data;
    Node* prev;
    Node* next;
};

int main() {
    Node* head = new Node{10, nullptr, nullptr};
    Node* second = new Node{20, head, nullptr};
    Node* third = new Node{30, second, nullptr};

    head->next = second;
    second->next = third;

    Node* temp = head;

    while (temp != nullptr) {
        cout << temp->data << " ";
        temp = temp->next;
    }

    return 0;
}

Output
10 20 30 

Explanation: Each node stores data along with prev and next pointers, allowing the list to be traversed in both directions.

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)

Note: If a pointer to the node is already available, insertion or deletion can be performed in O(1) time.

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
  • 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++
#include <iostream>
using namespace std;

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

int main() {
    Node* head = new Node{10, nullptr};
    Node* second = new Node{20, nullptr};
    Node* third = new Node{30, nullptr};

    head->next = second;
    second->next = third;
    third->next = head;

    Node* temp = head;

    do {
        cout << temp->data << " ";
        temp = temp->next;
    } while (temp != head);

    return 0;
}

Output
10 20 30 

Explanation: Each node points to the next node, and the last node points back to head, forming a circular structure.

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)

Note: With a tail pointer, insertion at the beginning or end of a circular singly linked list can be performed in O(1) time.

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.

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