We're sorry this tutorial wasn't helpful. Please tell us what we can do better.
Find length of Doubly Linked List
Last updated:
The length of a doubly linked list is the number of nodes in it. A plain list does not store this number, so it is found by walking the list once and counting the nodes on the way.
Example: the list in the diagram has three nodes, so its length is 3.
Algorithm:
Set a counter to 0 and start at the head.
While the current node is not null, add 1 to the counter and move to next.
When the current node becomes null, return the counter. An empty list gives 0.
Example: Find the length of Doubly Linked List in Java
FindLengthOfDLL.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 previous node in the list
public Node next; // Reference to the next 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 FindLengthOfDLL {
// 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);
}
}
Output
The Length of Doubly Linked List is: 4
Time Complexity: O(n), where n is the number of nodes; each node is visited once.
Extra Space: O(1)
Frequently Asked Questions (FAQ)
Q1. How do you find the length of a doubly linked list iteratively?
LengthIterative.java
public class LengthIterative {
static class Node {
int data;
Node prev;
Node next;
Node(int data) {
this.data = data;
}
}
static int length(Node head) {
int length = 0;
Node current = head;
while (current != null) {
length++;
current = current.next;
}
return length;
}
}
It takes O(n) time and O(1) space.
Q2. How do you find the length recursively?
LengthRecursive.java
public class LengthRecursive {
static class Node {
int data;
Node prev;
Node next;
Node(int data) {
this.data = data;
}
}
static int length(Node node) {
if (node == null) {
return 0;
}
return 1 + length(node.next);
}
}
This uses O(n) stack space, so it can overflow for very long lists.
Q3. How can you get the length in O(1)?
Keep a size field in the list class and update it on every insert and delete:
SizedList.java
public class SizedList {
static class Node {
int data;
Node prev, next;
Node(int data) { this.data = data; }
}
private Node head, tail;
private int size;
void addLast(int value) {
Node node = new Node(value);
if (tail == null) {
head = tail = node;
} else {
tail.next = node;
node.prev = tail;
tail = node;
}
size++;
}
int size() {
return size; // O(1)
}
}
java.util.LinkedList.size() works this way.
Q4. Can you count from the tail instead of the head?
Yes. Following prev from the tail visits the same nodes and gives the same count. It is useful when you only hold a tail reference.
Q5. Write a Java method to find the middle node of a doubly linked list.
MiddleNode.java
public class MiddleNode {
static class Node {
int data;
Node prev;
Node next;
Node(int data) {
this.data = data;
}
}
static Node middle(Node head) {
Node slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
}
Q6. How do you check whether a doubly linked list has an even or odd length without counting?
IsEvenLength.java
public class IsEvenLength {
static class Node {
int data;
Node prev;
Node next;
Node(int data) {
this.data = data;
}
}
static boolean isEvenLength(Node head) {
Node fast = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
}
return fast == null; // null after jumping two at a time means even
}
}
Q7. What is the time complexity of finding the length?
Counting takes O(n) because every node is visited once. Keeping a size counter makes size()O(1) at the cost of updating the counter in every operation.
Q8. What is a common mistake when counting nodes?
Writing while (current.next != null) instead of while (current != null). That stops one node early, so the result is one less than the real length, and it throws a NullPointerException for an empty list.
Q9. Write a Java method that returns the k-th node from the end using the length.
KthFromEnd.java
public class KthFromEnd {
static class Node {
int data;
Node prev;
Node next;
Node(int data) {
this.data = data;
}
}
static Node kthFromEnd(Node head, int k) {
int length = 0;
for (Node n = head; n != null; n = n.next) {
length++;
}
if (k < 1 || k > length) {
return null;
}
Node current = head;
for (int i = 0; i < length - k; i++) {
current = current.next;
}
return current;
}
}
With a tail reference, you can instead walk k - 1 steps back from the tail.
Q10. Does the length change the time complexity of other operations?
Knowing the length lets you choose the shorter direction when accessing index i: walk forward from the head if i < size / 2, otherwise walk backward from the tail. This halves the number of steps on average, but the operation is still O(n).