- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Python Program to Find the Length of the Linked List using Recursion

When it is required to find the length of a linked list with the help of recursion, a method to add elements to the linked list, and a method to calculate the length of the linked list is defined. A helper function is defined that is called with the help of the previously defined length calculation method.

Below is a demonstration for the same −

## Example

class Node: def __init__(self, data): self.data = data self.next = None class my_linked_list: def __init__(self): self.head = None self.last_node = None def add_value(self, my_data): if self.last_node is None: self.head = Node(my_data) self.last_node = self.head else: self.last_node.next = Node(my_data) self.last_node = self.last_node.next def calculate_length(self): return self.length_helper_fun(self.head) def length_helper_fun(self, curr): if curr is None: return 0 return 1 + self.length_helper_fun(curr.next) my_instance = my_linked_list() my_data = input('Enter elements of the linked list ').split() for elem in my_data: my_instance.add_value(int(elem)) print('The length of the linked list is ' + str(my_instance.calculate_length()))

## Output

Enter elements of the linked list 12 45 32 67 88 0 99 The length of the linked list is 7

## Explanation

The ‘Node’ class is created.

Another ‘my_linked_list’ class with required attributes is created.

It has an ‘init’ function that is used to initialize the first element, i.e the ‘head’ to ‘None’ and last node to ‘None’.

Another method named ‘add_value’ is defined, that is used to add data to the linked list.

Another method named ‘calculate_length’ is defined that is used to call the helper function to find the length of the linked list.

The helper function is defined, since recursion needs to be used here.

It checks for the current value of node, and returns the length of the list.

An object of the ‘my_linked_list’ class is created.

The user input is taken for the elements in the linked list.

Methods are called on it to add data.

The calculate_length method is called, and output is displayed on the console.

- Related Questions & Answers
- Python Program to Find the Length of the Linked List without using Recursion
- Python Program to Find the Length of a List Using Recursion
- C program to find the length of linked list
- Python Program to Display the Nodes of a Linked List in Reverse using Recursion
- Python Program to Display all the Nodes in a Linked List using Recursion
- Python Program to Print the Alternate Nodes in a Linked List using Recursion
- Python Program to Count the Number of Occurrences of an Element in the Linked List using Recursion
- Python Program to Display the Nodes of a Linked List in Reverse without using Recursion
- Python Program to Find the Total Sum of a Nested List Using Recursion
- Python Program to Search for an Element in the Linked List without using Recursion
- Python Program to Print the Alternate Nodes in a Linked List without using Recursion
- Python Program to Count the Number of Occurrences of an Element in the Linked List without using Recursion
- Python Program to Find the Fibonacci Series Using Recursion
- Python Program to Find the Product of two Numbers Using Recursion
- Length of a Linked List in Python