Get Length of Linked List in DSA Python - Time & Space Complexity
We want to understand how the time to find the length of a linked list changes as the list grows.
How does the number of steps increase when the list gets longer?
Analyze the time complexity of the following code snippet.
class Node:
def __init__(self, value):
self.value = value
self.next = None
def get_length(head):
count = 0
current = head
while current is not None:
count += 1
current = current.next
return count
This code counts how many nodes are in a linked list by moving from the first node to the last.
- Primary operation: The while loop that visits each node once.
- How many times: Exactly once per node in the list, so as many times as the list length.
Each new node adds one more step to count it, so the steps grow directly with the list size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 steps |
| 100 | 100 steps |
| 1000 | 1000 steps |
Pattern observation: The number of steps grows in a straight line with the list size.
Time Complexity: O(n)
This means the time to find the length grows directly with how many nodes are in the list.
[X] Wrong: "The length can be found instantly without looking at each node."
[OK] Correct: Because a linked list does not store its size, you must check each node to count them.
Knowing how to analyze simple linked list operations like this builds a strong base for more complex problems.
"What if the linked list stored its length as a property updated on insertions? How would the time complexity change?"
Practice
Solution
Step 1: Understand linked list structure
A linked list is made of nodes connected one after another.Step 2: Define length meaning
The length is how many nodes are present in the list.Final Answer:
The number of nodes in the linked list -> Option CQuick Check:
Length counts nodes = number of nodes [OK]
- Confusing length with node values
- Thinking length is memory size
- Counting only first or last node
Solution
Step 1: Identify counting variable type
Length should be a number starting at zero before counting nodes.Step 2: Check each option
length = 0 sets length to 0, ready to increment. Others assign wrong types.Final Answer:
length = 0 -> Option BQuick Check:
Initialize count as 0 before loop [OK]
- Initializing length as None or list
- Assigning length to head node
- Using wrong data types for count
class Node:
def __init__(self, data):
self.data = data
self.next = None
def get_length(head):
count = 0
current = head
while current:
count += 1
current = current.next
return count
# Creating linked list: 10 -> 20 -> 30 -> None
head = Node(10)
head.next = Node(20)
head.next.next = Node(30)
print(get_length(head))Solution
Step 1: Trace linked list nodes
List has nodes with values 10, 20, and 30, total 3 nodes.Step 2: Follow get_length function
Function counts nodes by moving current pointer until None, increments count each step.Final Answer:
3 -> Option AQuick Check:
Count nodes 10,20,30 = 3 [OK]
- Stopping count early
- Not incrementing count inside loop
- Returning None instead of count
def get_length(head):
count = 0
while head.next:
count += 1
head = head.next
return countSolution
Step 1: Analyze loop condition
Loop runs while head.next exists, so it stops before last node.Step 2: Check counting logic
Last node is not counted because loop ends early, count misses one node.Final Answer:
It misses counting the last node -> Option AQuick Check:
Loop condition excludes last node [OK]
- Using head.next in loop condition
- Not counting last node
- Assuming loop covers all nodes
Solution
Step 1: Traverse and check values simultaneously
Move through each node, check if value is positive.Step 2: Return 0 immediately if value is zero or negative, else keep counting
If any node fails condition, stop and return 0; otherwise, return total count.Final Answer:
Traverse nodes, if value <= 0 return 0, else count nodes till end -> Option DQuick Check:
Check values while counting, stop early if invalid [OK]
- Counting first, then checking values
- Ignoring node values
- Checking only first node value
Ready to prove it?
Pass the test and this pattern moves unknown
