Course lesson
Return the Length of a Linked List
We'll learn how to calculate the number of Nodes in a Linked List.
- Duration
- 1 min
- Access
- Free
- Transcript
- Available
We'll learn how to calculate the number of Nodes in a Linked List.
Course lesson
We'll learn how to calculate the number of Nodes in a Linked List.
We'll learn how to calculate the number of Nodes in a Linked List.
The last method we'll implement is 'length'. It doesn't take any arguments and it will simply return the total number of 'nodes' in our linked list. Just like before, we're using a while loop. We initialize length to 0 and for each node we find in the linked list, we increase length by 1. At the end of the function, we simply return length. Let's go check out our tests. Here, we create a new linked list, we insert 3 elements into it, and we check that the length isn't what we expect it to be. Now all of our tests pass.
Course lesson
We'll learn how to calculate the number of Nodes in a Linked List.