C++ Program For Finding Length Of A Linked List
Last Updated :
05 Jan, 2023
Write a function to count the number of nodes in a given singly linked list.

For example, the function should return 5 for linked list 1->3->1->2->1.
Iterative Solution:
1) Initialize count as 0
2) Initialize a node pointer, current = head.
3) Do following while current is not NULL
a) current = current -> next
b) count++;
4) Return count
Following is the Iterative implementation of the above algorithm to find the count of nodes in a given singly linked list.
C++
// Iterative C++ program to find length
// or count of nodes in a linked list
#include <bits/stdc++.h>
using namespace std;
// Link list node
class Node
{
public:
int data;
Node* next;
};
/* Given a reference (pointer to pointer)
to the head of a list and an int, push
a new node on the front of the list. */
void push(Node** head_ref, int new_data)
{
// Allocate node
Node* new_node =new Node();
// Put in the data
new_node->data = new_data;
// Link the old list of the
// new node
new_node->next = (*head_ref);
// Move the head to point to
// the new node
(*head_ref) = new_node;
}
// Counts no. of nodes in linked list
int getCount(Node* head)
{
// Initialize count
int count = 0;
// Initialize current
Node* current = head;
while (current != NULL)
{
count++;
current = current->next;
}
return count;
}
// Driver code
int main()
{
// Start with the empty list
Node* head = NULL;
// Use push() to construct list
// 1->2->1->3->1
push(&head, 1);
push(&head, 3);
push(&head, 1);
push(&head, 2);
push(&head, 1);
// Check the count function
cout << "count of nodes is " <<
getCount(head);
return 0;
}
// This is code is contributed by rathbhupendra
Output:
count of nodes is 5
Time Complexity: O(n), where n represents the length of the given linked list.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Recursive Solution:
int getCount(head)
1) If head is NULL, return 0.
2) Else return 1 + getCount(head->next)
Following is the Recursive implementation of the above algorithm to find the count of nodes in a given singly linked list.
C++
// Recursive C++ program to find length
// or count of nodes in a linked list
#include <bits/stdc++.h>
using namespace std;
// Link list node
class Node
{
public:
int data;
Node* next;
};
/* Given a reference (pointer to pointer)
to the head of a list and an int,
push a new node on the front of the list. */
void push(Node** head_ref, int new_data)
{
// Allocate node
Node* new_node = new Node();
// Put in the data
new_node->data = new_data;
// Link the old list of the
// new node
new_node->next = (*head_ref);
// Move the head to point to
// the new node
(*head_ref) = new_node;
}
// Recursively count number of
// nodes in linked list
int getCount(Node* head)
{
// Base Case
if (head == NULL)
{
return 0;
}
// Count this node plus the rest
// of the list
else
{
return 1 + getCount(head->next);
}
}
// Driver code
int main()
{
// Start with the empty list
Node* head = NULL;
// Use push() to construct list
// 1->2->1->3->1
push(&head, 1);
push(&head, 3);
push(&head, 1);
push(&head, 2);
push(&head, 1);
// Check the count function
cout << "Count of nodes is " <<
getCount(head);
return 0;
}
// This is code is contributed by rajsanghavi9
Output:
Count of nodes is 5
Time Complexity: O(n), where n represents the length of the given linked list.
Auxiliary Space: O(n), for recursive stack where n represents the length of the given linked list.
Please refer complete article on Find Length of a Linked List (Iterative and Recursive) for more details!
Similar Reads
C++ Program For Finding The Length Of Loop In Linked List Write a function detectAndCountLoop() that checks whether a given Linked List contains loop and if loop is present then returns count of nodes in loop. For example, the loop is present in below-linked list and length of the loop is 4. If the loop is not present, then the function should return 0. Re
3 min read
C++ Program For Detecting Loop In A Linked List Given a linked list, check if the linked list has loop or not. Below diagram shows a linked list with a loop. The following are different ways of doing this. Solution 1: Hashing Approach: Traverse the list one by one and keep putting the node addresses in a Hash Table. At any point, if NULL is reach
11 min read
C++ Program For Inserting A Node In A Linked List Inserting a node into a linked list can be done in several ways, depending on where we want to insert the new node. Here, we'll cover four common scenarios: inserting at the front of the list, after a given node, at a specific position, and at the end of the listTable of ContentInsert a Node at the
9 min read
C++ Program For Finding Intersection Point Of Two Linked Lists There are two singly linked lists in a system. By some programming error, the end node of one of the linked lists got linked to the second list, forming an inverted Y-shaped list. Write a program to get the point where two linked lists merge. Above diagram shows an example with two linked lists hav
9 min read
Find length of loop in a Linked List using Map Write a program that checks whether a given Linked List contains a loop and if a loop is present then returns the count of nodes in the loop. For example, a loop is present in the below-linked list and the length of the loop is 4. If the loop is not present, then the function should return 0. Approa
7 min read