Find Length of Linked List in Python (2 Examples)

 

Hi! This short tutorial will show you how to determine the length of a linked list in the Python programming language.

Here is an overview:

Let’s jump into the Python code!

 

Create Example List

Here, we will create the demo linked list that we will use in the examples in this tutorial.

Therefore, in your preferred Python programming IDE, run the code below to create the example linked list:

class Node:
    def __init__(self, data=None):
        self.data = data
        self.next = None
 
# Create a linked list
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(4)

With the linked list created, we will now examine ways to get its length.
 

Example 1: Get Length of Linked List Using Iterative Approach

In this first example, we will use an iterative approach to determine the length of the linked list:

def get_linked_list_length(head):
    length = 0
    current = head
    while current is not None:
        length += 1
        current = current.next
    return length
 
length = get_linked_list_length(head)
 
print(length)
# 4

Here, we first initialize a variable length to 0. We also set a reference current to the head of the linked list.

We then enter a loop that continues until we reach the end of the linked list, which is indicated by current being None.

In each iteration of the loop, we increment length by 1 to count the current node. Then, we move current to the next node by updating it as current.next.

Once the loop completes, we have traversed the entire linked list, and length holds the total number of nodes in the list.

Finally, we return the value of length as the length of the linked list.
 

Example 2: Get Length of Linked List Using Recursive Approach

In this next example, we will use a recursive approach to get the length of the linked list:

def get_linked_list_lengthB(head):
    if head is None:
        return 0
    return 1 + get_linked_list_length(head.next)
 
length = get_linked_list_lengthB(head)
 
print(length)
# 4

In the example above, we employed a recursive function, get_linked_list_length(), which takes the head of the linked list as a parameter.

The base case of the recursion is when the head is None, indicating an empty list. In this case, we return 0 since there are no nodes in the list.

If the base case is not met, we recursively call the get_linked_list_length() function, parsing head.next as the argument.

This recursive call navigates to the next node in the list and adds 1 to the result, representing the current node being counted.

This process continues until the base case is reached, and the recursive calls start unwinding. Each recursive call increments the count by 1, eventually resulting in the total length of the linked list.

 

Video, Further Resources & Summary

Do you need more explanations on how to get the length of a linked list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to get the length of a linked list in Python.

 

The YouTube video will be added soon.

 

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

This post has shown how to get the length of a linked list in Python. I do hope you found this tutorial helpful!

In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top