Python Program Find the Length of Linked List without Recursion

This is a Python program to find the length of a linked list without 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 method length.
5. The method length uses a loop to iterate over the nodes of the list to calculate its length.
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.

Program/Source Code

Here is the source code of a Python program to find the length of a linked list without 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):
        current = self.head
        length = 0
        while current:
            length = length + 1
            current = current.next
        return length
 
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: 8 1
The length of the linked list is 2.
 
Case 2:
Please enter the elements in the linked list: 
The length of the linked list is 0.
 
Case 3:
Please enter the elements in the linked list: 7 5 9 12
The length of the linked list is 4.

Sanfoundry Global Education & Learning Series – 1000 Python Programs.

If you wish to look at all Python Programming examples, go to 1000 Python Programs.

Free 30-Day Java Certification Bootcamp is Live. Join Now!

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.