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

«
»

This is a Python program to find the length of a linked list using recursion.

Problem Description

The program prompts the user to enter the elements of the linked list to create and then displays its length.

Problem Solution

1. Create a class Node.
2. Create a class LinkedList.
3. Define method append inside the class LinkedList to append data to the linked list.
4. Define methods length and length_helper.
5. length calls length_helper to find the length of the linked list recursively.
6. Create an instance of LinkedList and prompt the user for its elements.
7. Display the length of the list by calling the method length.

advertisement
Program/Source Code

Here is the source code of a Python program to find the length of a linked list using recursion. The program output is shown below.

class Node:
    def __init__(self, data):
       self.data = data
       self.next = None
 
class LinkedList:
    def __init__(self):
        self.head = None
        self.last_node = None
 
    def append(self, data):
        if self.last_node is None:
            self.head = Node(data)
            self.last_node = self.head
        else:
            self.last_node.next = Node(data)
            self.last_node = self.last_node.next
 
    def length(self):
        return self.length_helper(self.head)
 
    def length_helper(self, current):
        if current is None:
            return 0
        return 1 + self.length_helper(current.next)
 
a_llist = LinkedList()
data_list = input('Please enter the elements in the linked list: ').split()
for data in data_list:
    a_llist.append(int(data))
 
print('The length of the linked list is ' + str(a_llist.length()) + '.', end = '')
Program Explanation

1. An instance of LinkedList is created.
2. The user is prompted for the elements of the list.
3. The elements are then appended to the linked list.
4. The method length calculates the length of the list.
5. The length is then displayed.

advertisement
Runtime Test Cases
Case 1:
Please enter the elements in the linked list: 3 4 10
The length of the linked list is 3.
 
Case 2:
Please enter the elements in the linked list: 7
The length of the linked list is 1.
 
Case 3:
Please enter the elements in the linked list: 3 4 1 -1 3 9
The length of the linked list is 6.

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

advertisement

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn