In this Linked List tutorial, we will learn how to find the length of a Linked List. The length of a linked list is the total number of nodes present in it.
Examples:
Linked List: 80 -> 20 -> 70 -> 50 -> 30 Length of Linked List: 5
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:
- Iterative Approach
- Recursive Approach
1. Find length of a linked list using an Iterative Approach
Follow these steps to find the length of the Linked List using the iterative approach:
- Create a count variable and initialize it with 0.
- Initialize the temp variable with head, tempNode = head.
- Do the following while tempNode is not NULL:
- tempNode = tempNode → next
- Increment count by 1
- Once tempNode is NULL, return count.
In the example below, we will see the implementation of the iterative approach in 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));
}
}
Length of the linked list is: 5 After adding two new nodes, length of the linked list is: 7
Time complexity: O(N), where N is the length of the linked list.
Auxiliary Space: O(1), as constant extra space is 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.
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));
}
}
Length of the linked list is: 5 After adding two new nodes, length of the linked list is: 7
Time complexity: O(N), where N is the length of the linked list.
Auxiliary Space: O(N), as extra space is used by the recursion call stack.