Length of a Linked List (Iterative and Recursive)

Last Updated : 9 Oct, 2026

Given head of a singly linked list. Find the length of the linked list, where length is defined as the number of nodes in the linked list.

Examples:

Input: head: 1 -> 2 -> 3 -> 4 -> 5

WhatsApp-Image-2026-10-


Output: 5
Explanation: The linked list has 5 nodes.

Input: head: 2 -> 4 -> 6 -> 7 -> 5 -> 1 -> 0

WhatsApp-Image


Output: 7
Explanation: The linked list has 7 nodes.

Try It Yourself
redirect icon

Iterative Approach - O(n) Time and O(1) Space

The idea is to traverse the linked list from the head node and count each node until the end of the list is reached.

  • Initialize count as 0 and curr as head.
  • Traverse the list while curr is not null.
  • Increment count and move curr to the next node.
  • Return count.
C++
#include <bits/stdc++.h>
using namespace std;

class Node {
public:
    int data;
    Node* next;

    Node(int new_data) {
        data = new_data;
        next = nullptr;
    }
};

int getCount(Node* head) {
  	
    int count = 0;

    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 count;
}

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);

    cout<< getCount(head);
    return 0;
}
C
#include <stdio.h>
#include <stdlib.h>

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

struct Node *createNode(int new_data)
{
    struct Node *new_node = (struct Node *)malloc(sizeof(struct Node));
    new_node->data = new_data;
    new_node->next = NULL;
    return new_node;
}

int getCount(struct Node *head)
{

    int count = 0;
    struct Node *curr = head;

    // Traverse till we reach NULL
    while (curr != NULL)
    {

        // Increment count by 1
        count++;

        // Move pointer to next node
        curr = curr->next;
    }

    return count;
}
int main()
{

    // Create a hard-coded linked list:
    // 1 -> 3 -> 1 -> 2 -> 1
    struct Node *head = createNode(1);
    head->next = createNode(3);
    head->next->next = createNode(1);
    head->next->next->next = createNode(2);
    head->next->next->next->next = createNode(1);
    
    printf("%d", getCount(head));
    return 0;
}
Java
class Node {
    int data;
    Node next;
    Node(int newData) {
        data = newData;
        next = null;
    }
}

public class GFG {
    public static int getCount(Node head) {
        int count = 0;
        Node curr = head;

        // Traverse till we reach null
        while (curr != null) {

            // Increment count by 1
            count++;

            // Move pointer to next node
            curr = curr.next;
        }

        return count;
    }

    public static void main(String[] args) {

        // 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);

        System.out.println(getCount(head));
    }
}
Python
class Node:
    def __init__(self, new_data):
        self.data = new_data
        self.next = None


def getCount(head):

    count = 0
    curr = head

    # Traverse till we reach None
    while curr is not None:
        
        # Increment count by 1
        count += 1

        # Move pointer to next node
        curr = curr.next

    return count

if __name__ == "__main__":

    # Create a hard-coded linked list:
    # 1 -> 3 -> 1 -> 2 -> 1
    head = Node(1)
    head.next = Node(3)
    head.next.next = Node(1)
    head.next.next.next = Node(2)
    head.next.next.next.next = Node(1)
    print(getCount(head))
C#
using System;

class Node {
    public int data;
    public Node next;
    public Node(int newData) {
        data = newData;
        next = null;
    }
}

class GFG {
    static int getCount(Node head) {
        int count = 0;
        Node curr = head;

        // Traverse till we reach null
        while (curr != null) {
            
            // Increment count by 1
            count++;

            // Move pointer to next node
            curr = curr.next;
        }

        // Return the count of nodes
        return count;
    }

    static void 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);
        Console.WriteLine(getCount(head));
    }
}
JavaScript
class Node {
    constructor(newData) {
        this.data = newData;
        this.next = null;
    }
}
function getCount(head) {
    let count = 0;
    let curr = head;

    // Traverse till we reach null
    while (curr !== null) {

        // Increment count by 1
        count++;

        // Move pointer to next node
        curr = curr.next;
    }
    return count;
}

// Driver code

// Create a hard-coded linked list:
// 1 -> 3 -> 1 -> 2 -> 1
let 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);
console.log(getCount(head));

Output
5

Recursive Approach - O(n) Time and O(n) Space

The idea is to count the nodes recursively, starting from the head of the linked list.

  • If the current node is null, return 0.
  • Otherwise, return 1 + getCount(node.next), where 1 counts the current node.
C++
#include <bits/stdc++.h>
using namespace std;

class Node {
public:
    int data;
    Node* next;

    Node(int new_data) {
        data = new_data;
        next = nullptr;
    }
};

// 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
    return 1 + getCount(head->next);
}

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);
    cout << getCount(head);
    return 0;
}
C
#include <stdio.h>
#include <stdlib.h>

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

struct Node* createNode(int new_data) {
    struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
    new_node->data = new_data;
    new_node->next = NULL;
    return new_node;
}

// Recursively count number of nodes in linked list
int getCount(struct Node* head) {
  	
    // Base Case
    if (head == NULL) {
        return 0;
    }

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

int main() {
  	
    // Create a hard-coded linked list:
    // 1 -> 3 -> 1 -> 2 -> 1
    struct Node* head = createNode(1);
    head->next = createNode(3);
    head->next->next = createNode(1);
    head->next->next->next = createNode(2);
    head->next->next->next->next = createNode(1);
    printf("%d\n", getCount(head));
    return 0;
}
Java
class Node {
    int data;
    Node next;

    Node(int new_data) {
        data = new_data;
        next = null;
    }
}

// Recursively count number of nodes in linked list
public class GFG {
    public static int getCount(Node head) {
      	
        // Base Case
        if (head == null) {
            return 0;
        }

        // Count this node plus the rest of the list
        return 1 + getCount(head.next);
    }

    public static void main(String[] args) {
      	
        // 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);

        System.out.println(getCount(head));
    }
}
Python
class Node:
    def __init__(self, new_data):
        self.data = new_data
        self.next = None

# Recursively count number of nodes in linked list
def getCount(head):
    # Base Case
    if head is None:
        return 0

    # Count this node plus the rest of the list
    return 1 + getCount(head.next)
    
if __name__ == "__main__":
  	
    # Create a hard-coded linked list:
    # 1 -> 3 -> 1 -> 2 -> 1
    head = Node(1)
    head.next = Node(3)
    head.next.next = Node(1)
    head.next.next.next = Node(2)
    head.next.next.next.next = Node(1)

    print(getCount(head))
C#
using System;

class Node {
    public int data;
    public Node next;
    
    public Node(int newData) {
        data = newData;
        next = null;
    }
}

class GFG {
    
    // Recursively count number of nodes in linked list
    static int getCount(Node head) {
        
        // Base Case
        if (head == null) {
            return 0;
        }

        // Count this node plus the rest of the list
        return 1 + getCount(head.next);
    }
    static void 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);
        Console.WriteLine(getCount(head));
    }
}
JavaScript
class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}

// Recursively count number of nodes in linked list
function getCount(head) {

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

    // Count this node plus the rest of the list
    return 1 + getCount(head.next);
}

// Driver code

// Create a hard-coded linked list:
// 1 -> 3 -> 1 -> 2 -> 1
let 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);
console.log(getCount(head));

Output
5
Comment