00:00

Find length of a Singly Linked List

Last updated:

The length of a linked list is the number of nodes it contains. Unlike an array, a plain singly linked list does not store its length anywhere, so it has to be counted by walking from the head to the end. An empty list (head == null) has length 0.

Examples:

Output
Linked List: 80 -> 20 -> 70 -> 50 -> 30
Length of Linked List: 5
Output
Linked List: 80 -> 20 -> 70 -> 50 -> 30 -> 10 -> 90 -> 40
Length of Linked List: 8

There are two approaches to find the length of a linked list:

  1. Iterative Approach
  2. Recursive Approach

1. Find length of a linked list using an Iterative Approach

Walk a reference through the list and add one for every node it passes:

  1. Set count to 0 and point tempNode at head.
  2. While tempNode is not null, add 1 to count and move tempNode to tempNode.next.
  3. When tempNode becomes null, every node has been counted; return count.

In the example below, we will see the implementation of the iterative approach in Java.

FindLengthUsingIterativeApproachInSLL.java
class Node {
  public int data;
  public Node next;

  // Constructor
  public Node(int data) {
    this.data = data;
    this.next = null;
  }
}

public class FindLengthUsingIterativeApproachInSLL {
  // Counts number of nodes in linked list
  static int countTotalNodes(Node head) {

    // Initialize count with 0
    int count = 0;

    // Initialize tempNode with head of Linked List
    Node tempNode = head;

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

      // Increment count by 1
      count++;

      // Move pointer to next node
      tempNode = tempNode.next;
    }
    // Return the count of nodes
    return count;
  }

  public static void main(String[] args) {
    // Create a linked list
    Node head = new Node(10);
    head.next = new Node(20);
    head.next.next = new Node(30);
    head.next.next.next = new Node(40);
    head.next.next.next.next = new Node(50);

    // Linked list will be created like this
    // 10 -> 20 -> 30 -> 40 -> 50

    // Printing total number of nodes in the list
    System.out.println("Length of the linked list is: " + countTotalNodes(head));

    // Now adding two more nodes
    head.next.next.next.next.next = new Node(60);
    head.next.next.next.next.next.next = new Node(70);

    // Now, the linked list will be like this
    // 10 -> 20 -> 30 -> 40 -> 50 -> 60 -> 70

    System.out.println("After adding two new nodes, length of the linked list is: " + countTotalNodes(head));
  }
}
Output
Length of the linked list is: 5
After adding two new nodes, length of the linked list is: 7

Time complexity: O(n) for a list of n nodes, since every node is visited once.

Extra space: O(1); only a counter and one reference are used.

2. Find length of linked list using Recursive Approach

To implement the recursive approach, we define a function called countTotalNodes(node). This function takes a node as an argument and calls itself with the next node until we reach the end of the linked list. Each recursive call returns 1 + the count of the remaining nodes.

In the example below, we will see how to find the length of a linked list using the recursive approach in Java.

FindLengthUsingRecursiveApproachInSLL.java
class Node {
  public int data;
  public Node next;

  // Constructor
  public Node(int data) {
    this.data = data;
    this.next = null;
  }
}

public class FindLengthUsingRecursiveApproachInSLL {
  static int countTotalNodes(Node head) {
    // Returns 0, if list is empty
    if (head == null) {
      return 0;
    }

    // Adding 1 in the total number of nodes of the list
    return 1 + countTotalNodes(head.next);
  }

  public static void main(String[] args) {

    // Create a linked list
    Node head = new Node(10);
    head.next = new Node(20);
    head.next.next = new Node(30);
    head.next.next.next = new Node(40);
    head.next.next.next.next = new Node(50);

    // Linked list will be created like this
    // 10 -> 20 -> 30 -> 40 -> 50

    // Printing total number of nodes in the list
    System.out.println("Length of the linked list is: " + countTotalNodes(head));

    // Now adding two more nodes
    head.next.next.next.next.next = new Node(60);
    head.next.next.next.next.next.next = new Node(70);

    // Now, the linked list will be like this
    // 10 -> 20 -> 30 -> 40 -> 50 -> 60 -> 70

    System.out.println("After adding two new nodes, length of the linked list is: " + countTotalNodes(head));
  }
}
Output
Length of the linked list is: 5
After adding two new nodes, length of the linked list is: 7

Time complexity: O(n), one call per node.

Extra space: O(n), because each call stays on the call stack until the end of the list is reached.