You can also use this:

def len_link(list):
    temp=list.head
    count=0
    while(temp):
        count+=1
        temp=temp.next
    return count
Answer from Nitika Khurana on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-program-for-finding-length-of-a-linked-list-iterative-and-recursive-approach
Python Program For Finding Length Of A Linked List - GeeksforGeeks
July 23, 2025 - Return the size of the hash table as the length of the linked list. ... # Python program for the above approach # Linked List Node Class class Node: def __init__(self, data=None): self.data = data self.next = None # Linked List Class class LinkedList: def __init__(self): self.head = None # Function to insert into Linked List def insert(self, data): new_node = Node(data) if self.head is None: self.head = new_node else: current_node = self.head while current_node.next: current_node = current_node.next current_node.next = new_node # Function to find the length of # the Linked List def length(self
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ length-of-a-linked-list-in-python
Python Program to Find the Length of the Linked List without using Recursion
October 9, 2024 - 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())
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ returning the length of a linked list
r/learnpython on Reddit: Returning the length of a linked list
August 16, 2022 -

Hello!

We are creating Stacks implemented as a linked list. One of the methods of my stack should be to return its length.

My code currently works if the length of the stack is 1 or 0, but not for anything longer. I suspect the problem to be that once the head is checked, it is not moving onto the next Node correctly.

However, why would this be? (In a length of one item), Why is it that it can move from 1 item, to the None item and return a correct list of 1, but not anything more than that?

Please see my code below (let me know if you would need more):

def __len__(self):
  """ Returns the length --- calling len(s) will invoke this method ""
  inspector = self.head #inspector is what I've named the item being checked
  if inspector == None:  #the last element of the linked list is None, so length = 0 
      return 0
  counter = 0 
  while inspector != None:
      counter += 1
      inspector = self.head.next_node   #I assume this is the part which is causing the issue 
  return counter 

I'm going to catch some Zzz's (this has kept me up too late!) So i will reply in the morning, please let me know if you would like any more information.

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ dsa โ€บ find-length-of-a-linked-list-iterative-and-recursive
Length of a Linked List (Iterative and Recursive) - GeeksforGeeks
# Recursive Python program to find length # or count of nodes in a linked list # Linked List Node class Node: def __init__(self, new_data): self.data = new_data self.next = None # Recursively count number of nodes in linked list def count_nodes(head): # Base Case if head is None: return 0 # Count this node plus the rest of the list return 1 + count_nodes(head.next) # Driver code if __name__ == "__main__": # Create a hard-coded linked list: # 1 -> 3 -> 1 -> 2 -> 1 head = Node(1) head.next = Node(3) head.next.next = Node(1) head.next.next.next = Node(2) head.next.next.next.next = Node(1) # Function call to count the number of nodes print("Count of nodes is", count_nodes(head))
Published: September 10, 2025
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ find-length-of-a-linked-list
How to Find Length of a Linked List? | DigitalOcean
Technical tutorials, Q&A, events โ€” This is an inclusive place where developers can find or lend support and discover new ways to contribute to the community.
Find elsewhere
๐ŸŒ
LeetCode
leetcode.com โ€บ problems โ€บ insertion-sort-list โ€บ discuss โ€บ 46479 โ€บ how-can-we-measure-the-length-of-a-listnode โ€บ 46036
Insertion Sort List - LeetCode
May 28, 2019 - Can you solve this real interview question? Insertion Sort List - Given the head of a singly linked list, sort the list using insertion sort, and return the sorted list's head. The steps of the insertion sort algorithm: 1. Insertion sort iterates, consuming one input element each repetition ...
๐ŸŒ
Quora
quora.com โ€บ How-do-you-find-the-length-of-a-linked-list
How to find the length of a linked list - Quora
Answer (1 of 2): You can find out the length of a linked list in many ways For O(n) time: Initialize variable named โ€œcountโ€ with zero and Just iterate the list with the increment of โ€œcountโ€ variable by one until the nodeโ€™s next pointer points to โ€œNULLโ€ For O(1) time: Declare the ...
๐ŸŒ
Sanfoundry
sanfoundry.com โ€บ python-program-find-length-linked-list-without-using-recursion
Python Program Find the Length of Linked List without Recursion - Sanfoundry
May 30, 2022 - 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. ... Here is the source code of a Python program to find the length of a linked list without using recursion.
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ python-linked-lists
Python Linked Lists
August 25, 2023 - To have a data structure we can work with, we define a node. We'll implement a node as a class named ListNode. The class contains the definition to create an object instance, in this case, with two variables - data to keep the node value, and next to store the reference to the next node in the list.
๐ŸŒ
pytz
pythonhosted.org โ€บ llist
llist โ€” Linked list datatypes for Python โ€” llist 0.4 documentation
>>> from llist import sllist, sllistnode >>> empty_lst = sllist() # create an empty list >>> print(empty_lst) sllist() >>> print(len(empty_lst)) # display length of the list 0 >>> print(empty_lst.size) 0 >>> print(empty_lst.first) # display the first node (nonexistent) None >>> print(empty_lst.last) # display the last node (nonexistent) None >>> lst = sllist([1, 2, 3]) # create and initialize a list >>> print(lst) # display elements in the list sllist([1, 2, 3]) >>> print(len(lst)) # display length of the list 3 >>> print(lst.size) 3 >>> print(lst.nodeat(0)) # access nodes by index sllistnode(
๐ŸŒ
LeetCode
leetcode.com โ€บ problems โ€บ rotate-list โ€บ solutions โ€บ 22817 โ€บ count-the-length-of-linked-list-first-python
Rotate List - LeetCode
Example 1: [https://assets.lee... k = 4 Output: [2,0,1] Constraints: * The number of nodes in the list is in the range [0, 500]. * -100...
๐ŸŒ
Quora
quora.com โ€บ How-can-we-find-the-length-of-a-linked-list-without-traversing-its-elements-only-knowing-its-structure
How can we find the length of a linked list without traversing it's elements, only knowing its structure? - Quora
Answer: You canโ€™t, unless: 1. The list keeps track of its โ€˜lengthโ€™. 2. You make use of some kind of implementation detail. Perhaps, the list is not a list at all, but an array/vector that can grow or shrink? If you know the head, the tail and the size in bytes of each element, you can ...
๐ŸŒ
Built In
builtin.com โ€บ data-science โ€บ python-linked-list
An Introduction to Python Linked List and How to Create One
With the main functionality of a linked list created, we can start adding other methods that would make using the linked list simpler. Two supplementary methods that we can add include getting the length of the linked list and seeing if the list is empty. More on PythonHow to Write Nested List Comprehensions in Python
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-count-the-number-of-nodes-in-a-linked-list-in-python
How to count the number of nodes in a linked list in Python
A linked list is a linear data structure in which elements, called nodes, are connected through pointers. Each node contains data and a reference (pointer) to the next node in the sequence. The last nodeโ€™s pointer points to null, indicating the end of the list ยท Demonstrate the creation ...
๐ŸŒ
Python
docs.python.org โ€บ 2.4 โ€บ lib โ€บ dom-nodelist-objects.html
13.6.2.3 NodeList Objects
October 18, 2006 - Return the i'th item from the sequence, if there is one, or None. The index i is not allowed to be less then zero or greater than or equal to the length of the sequence.