In this article, we will learn how to find the length of a doubly linked list. Actually finding length means we will count the total number of nodes present in a doubly linked list.
To count the node, we will traverse the doubly linked list till the end node.
For Example:
As given in the diagram, there are a total 3 nodes, so we can say that the length of a doubly linked is 3.

Algorithm:
- Initialize the variable count = 0
- Continue to the next node until the doubly linked list nodes are exhausted.
- As we move to the next node, continue to increase the count variable by 1.
- Once the nodes are exhausted, return the count variable.
Example: Find the length of Doubly Linked List in Java
// Class representing a node in a doubly linked list
class Node {
public int data; // Data stored in the node
public Node prev; // Reference to the next node in the list
public Node next; // Reference to the previous node in the list
// Constructor to initialize a new node with data
public Node(int new_data) {
data = new_data;
prev = null;
next = null;
}
}
// Java Program to calculate the length of a Doubly Linked List
public class FindLengthOfDDL {
// Function to find and print the length of Doubly Linked List
static void findLength(Node node) {
int length = 0;
while (node != null) {
length++;
node = node.next;
}
System.out.println("The Length of Doubly Linked List is: " + length);
}
public static void main(String[] args) {
// Create a doubly linked list
Node head = new Node(10);
head.next = new Node(20);
head.next.prev = head;
head.next.next = new Node(30);
head.next.next.prev = head.next;
head.next.next.next = new Node(40);
head.next.next.next.prev = head.next.next;
// Find and print the length of a Doubly Linked List
findLength(head);
}
}
The Length of Doubly Linked List is: 4
Time Complexity: O(n), here n represent the total number of nodes in a doubly linked list.
Auxiliary Space: O(1)