How to count nodes in a simple linked list in C++

linked list is a linear data structure in which elements, called nodes, are connected through pointers. Every node in the series has data as well as a reference (pointer) to the next node. The first node is called the head node. The last node’s pointer points to null, indicating the end of the list.

A linked list
A linked list

Problem statement

Demonstrate the creation of a singly linked list, and the calculation of the total number of nodes in the linked list.

Solution

  1. Initialize count = 0 and a temporary variable curr with the head of the linked list start.

  2. Traverse the list and count nodes.

  3. Stop the loop when the temporary variable becomes equal to None.

  4. Return the count.

#include <iostream>
class Node
{
public: int data;
Node * next;
Node(int data)
{
this->data = data;
this->next = nullptr;
}
};
int countNodes(Node *start)
{
int count = 0;
Node *curr = start;
while (curr)
{
count++;
curr = curr->next;
}
return count;
}
int main()
{
Node *start = new Node(19);
start->next = new Node(22);
start->next->next = new Node(33);
int nodeCount = countNodes(start);
std::cout << "Count of nodes is " << nodeCount << std::endl;
// Don't forget to free memory used by the linked list
Node *curr = start;
while (curr != nullptr)
{
Node *temp = curr;
curr = curr->next;
delete temp;
}
return 0;
}

Explanation

  • Lines 3–13: It defines a class called Node. It’s meant to represent a node in a singly linked list. Each node has an integer data value and a pointer to the next node in the list. The constructor initializes data and sets the next pointer to nullptr.

  • Line 15: It declares a function called countNodes. It takes a pointer to a Node called start as its argument and returns an integer.

  • Line 17: It initializes an integer variable named count and sets it to 0. This variable will be used to keep track of the number of nodes in the linked list.

  • Line 18: It initializes a pointer to a Node named curr and sets it to the same node as the start node. This pointer is used to traverse the linked list.

  • Line 20: It starts a while loop that continues as long as the curr pointer isn’t pointing to nullptr, which means there are more nodes to traverse in the linked list.

  • Line 22: It increments the count variable by 1 for each node encountered. This effectively counts the nodes in the linked list.

  • Line 23: It updates the curr pointer to point to the next node in the linked list. It follows the next pointer of the current node to move to the next node in the list for the next iteration.

  • Line 26: It returns the count variable, which holds the count of nodes in the linked list.

  • Lines 31–33: It creates a linked list. Three nodes with data values 19, 22, and 33 are created, and their “next” pointers are set to form a linked list.

  • Line 35: The countNodes function is called to count the number of nodes in the linked list, and the result is stored in the nodeCount variable.

  • Line 37: It prints the count of nodes to the standard output.

  • Lines 40–46: It’s used for cleaning up the dynamically allocated memory used by the linked list. It iterates through the list, deleting each node and updating the curr pointer until the entire list is deallocated.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved