# JavaScript Program for Finding Length of a Linked List

A linked list is a linear data structure that can be a variable length and the length of the linked list can be changed, which was a problem in the array that the length of the array cannot be changed. In this article, we are going to find the length of a given linked list by implementing the code and going through the edge cases. We will use the while loop and the class concepts in this article.

## Introduction to Problem

In the given problem we are given a linked list and first, we have to create the linked list using the class, then after we have to find the length of the given linked list. As the length of the linked list can be changed, so we will find the length of the linked list at a particular point of code.

We are going to use two approaches first the direct iterative approach using the while loop and another the recursive approach to find the length of the given linked list.

## Iterative Approach

In this method, we are going to create a linked list first using the class to provide the structure to the linked list. We will define some functions like the push function to add the values to the linked list by simply passing the head and the data.

### Example

In the process, we will use the while loop, the head or the starting node to the linked list, and a variable to count the number of nodes in the linked list, that is the length of the given linked list.

// creating the linked list node
class Node{
constructor(data)  {
this.value = data;
this.next = null;
}
}
function push(tail, data){
var new_node = new Node(data);
tail.next = new_node;
tail = tail.next;
return tail
}
var count = 0
while(temp != null) {
count++;
temp = temp.next;
}
return count;
}
tail = push(tail, 2)
tail = push(tail, 3)
tail = push(tail, 4)
tail = push(tail, 5)
tail = push(tail, 6)


In the above-given method, we are not using any extra space and traversing over the linked list only once. So, the time complexity of the above-given method is O(N) where N is the size of the linked list and the space complexity of the above method is O(1).

## Recursive Approach

In this method, we are going to follow the same steps as we have gone through in the above method for creating the linked list, for the main task we will use the recursive approach.

### Example

Calling the same function with a different parameter from the function itself with a particular base condition is known as recursion. In this method, we are going to call the function with the head of the linked list, and then from the function, we will again call the function but with the parameter next node of the current node. As a return value, we will return 1 + recursive call return value and the result will be given at the first call. Let’s see the code −

// creating the linked list node
class Node {
constructor(data) {
this.value = data;
this.next = null;
}
}
function push(tail, data) {
var new_node = new Node(data);
tail.next = new_node;
tail = tail.next;
return tail
}

function length(cur) {
if(cur == null) return 0;
return 1 + length(cur.next);
}
tail = push(tail, 2)
tail = push(tail, 3)
tail = push(tail, 4)
tail = push(tail, 5)
tail = push(tail, 6)


### Time and Space complexity

The time complexity of the recursive approach is O(N), where N is the number of nodes present in the given linked list. The space complexity of the above code is O(N) because there are total N calls and for each call, we have to maintain the current node stack.

## Conclusion

In this tutorial, we have learned how to find the length of a given linked list by implementing the code and going through the edge cases. We have used the while loop and the class concepts in this article in the first approach and the recursive approach to finding the length in the second approach. The time complexity of both approaches is O(N) where N is the length of the linked list while the space complexity of the recursive approach is O(N) due to stack size.

Updated on: 24-Mar-2023

178 Views