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

When it is required to find the length of a linked list without using recursion, we define methods to add elements to the linked list and calculate its length using iterative approach. This approach uses a simple loop to traverse through all nodes and count them.

Below is a demonstration for the same −

Example

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 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):
        curr = self.head
        length_val = 0
        while curr:
            length_val = length_val + 1
            curr = curr.next
        return length_val

    def display(self):
        elements = []
        curr = self.head
        while curr:
            elements.append(str(curr.data))
            curr = curr.next
        return " -> ".join(elements)

# Create linked list instance
my_list = LinkedList()

# Add elements to the linked list
elements = [34, 12, 56, 86, 32, 99, 0, 6]
for elem in elements:
    my_list.add_value(elem)

print("Linked List:", my_list.display())
print("The length of the linked list is", my_list.calculate_length())
Linked List: 34 -> 12 -> 56 -> 86 -> 32 -> 99 -> 0 -> 6
The length of the linked list is 8

How It Works

The calculate_length() method works by −

  • Starting from the head node

  • Initializing a counter to 0

  • Traversing each node using a while loop

  • Incrementing the counter for each node visited

  • Moving to the next node until reaching the end (None)

Alternative Implementation

Here's a more concise version using a simpler approach −

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None
    
    def append(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
        else:
            current = self.head
            while current.next:
                current = current.next
            current.next = new_node
    
    def get_length(self):
        count = 0
        current = self.head
        while current:
            count += 1
            current = current.next
        return count

# Example usage
linked_list = LinkedList()
data = [10, 20, 30, 40, 50]

for value in data:
    linked_list.append(value)

print("Length of the linked list:", linked_list.get_length())
Length of the linked list: 5

Key Points

  • The iterative approach has O(n) time complexity where n is the number of nodes

  • Space complexity is O(1) as no extra space is used except for variables

  • No recursion means no risk of stack overflow for large lists

  • The algorithm simply counts nodes by traversing the entire list once

Conclusion

Finding the length of a linked list without recursion is straightforward using an iterative approach. We traverse through each node using a while loop and maintain a counter, making it memory-efficient and suitable for large datasets.

Updated on: 2026-03-25T18:36:46+05:30

276 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements