Length
Explore how to determine the length of a singly linked list through both iterative and recursive methods. This lesson guides you step-by-step in implementing and verifying these methods in Python, helping you deepen your understanding of linked list mechanics.
We'll cover the following...
In this lesson, we’ll calculate the length or the number of nodes in a given linked list. We’ll be doing this in both an iterative and recursive manner.
Algorithm #
Let’s look at a linked list and recall how we managed to print out the elements of a linked list. We iterate through every element of the linked list. We start from the head node and while we don’t reach None, we print the data field of the node that we point to and increment the while loop by setting the current node equal to the next node.