Open In App

Find Length of a Linked List (Iterative and Recursive)

Last Updated : 18 Feb, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Given a Singly Linked List, the task is to find the Length of the Linked List.

Examples:

Input: LinkedList = 1->3->1->2->1
Output: 5
Explanation: The linked list has 5 nodes.

Input: LinkedList = 2->4->1->9->5->3->6
Output: 7
Explanation: The linked list has 7 nodes.

Input: LinkedList = 10->20->30->40->50->60
Output: 6
Explanation: The linked list has 6 nodes.

Iterative Approach to Find the Length of a Linked List

The idea is similar to traversal of Linked List with an additional variable to count the number of nodes in the Linked List.

Steps to find the length of the Linked List:

  • Initialize count as 0. 
  • Initialize a node pointer, curr = head.
  • Do following while curr is not NULL
    • curr = curr -> next
    • Increment count by 1.
  • Return count.
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;

    // Constructor to initialize a new node with data
    Node(int new_data) {
        data = new_data;
        next = nullptr;
    }
};

// Counts number of nodes in linked list
int countNodes(Node* head) {
      
    // Initialize count with 0
    int count = 0;

    // Initialize curr with head of Linked List
    Node* curr = head;

    // Traverse till we reach nullptr
    while (curr != nullptr) {
        
          // Increment count by 1
        count++;
        
          // Move pointer to next node
        curr = curr->next;
    }
      
      // Return the count of nodes
    return count;
}

// Driver code
int main() {
      
    // Create a hard-coded linked list:
    // 1 -> 3 -> 1 -> 2 -> 1
    Node* head = new Node(1);
    head->next = new Node(3);
    head->next->next = new Node(1);
    head->next->next->next = new Node(2);
    head->next->next->next->next = new Node(1);

    // Function call to count the number of nodes
    cout << "Count of nodes is " << countNodes(head);
    return 0;
}
C Java Python C# JavaScript

Output
Count of nodes is 5

Time complexity: O(n), Where n is the size of the linked list
Auxiliary Space: O(1), As constant extra space is used.

Recursive Approach to Find the Length of a Linked List:

The idea is to use recursion by maintaining a function, say countNodes(node) which takes a node as an argument and calls itself with the next node until we reach the end of the Linked List. Each of the recursive call returns 1 + count of remaining nodes.

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;

    // Constructor to initialize a new node with data
    Node(int new_data) {
        data = new_data;
        next = nullptr;
    }
};

// Recursively count number of nodes in linked list
int countNodes(Node* head) {

    // Base Case
    if (head == NULL) {
        return 0;
    }

    // Count this node plus the rest of the list
    return 1 + countNodes(head->next);
}

// Driver code
int main() {
  
    // Create a hard-coded linked list:
    // 1 -> 3 -> 1 -> 2 -> 1
    Node* head = new Node(1);
    head->next = new Node(3);
    head->next->next = new Node(1);
    head->next->next->next = new Node(2);
    head->next->next->next->next = new Node(1);

    // Function call to count the number of nodes
    cout << "Count of nodes is " << countNodes(head);
    return 0;
}
C Java Python C# JavaScript

Output
Count of nodes is 5

Time Complexity: O(n), where n is the length of Linked List.
Auxiliary Space: O(n), Extra space is used in the recursion call stack.




Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg