Get Length of Linked List in DSA C++ - 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.
int getLength(Node* head) {
int length = 0;
Node* current = head;
while (current != nullptr) {
length++;
current = current->next;
}
return length;
}
This code counts how many nodes are in a linked list by moving from the start to the end.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that visits each node once.
- How many times: Exactly once per node, so as many times as the list length.
As the list gets longer, the number of steps grows directly with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps |
| 100 | About 100 steps |
| 1000 | About 1000 steps |
Pattern observation: The steps increase in a straight line as the list size grows.
Time Complexity: O(n)
This means the time to find the length grows directly with the number of nodes in the list.
[X] Wrong: "The length can be found instantly without checking each node."
[OK] Correct: Since nodes are linked one by one, you must visit each to count them; there is no shortcut.
Knowing how to analyze simple linked list operations like this builds a strong base for more complex problems.
"What if the linked list was doubly linked? Would the time complexity to get its length change?"
Practice
getLength do when given the head of a linked list?Solution
Step 1: Understand the function purpose
The functiongetLengthcounts nodes starting from the head until the end.Step 2: Identify the output
It returns the total count of nodes, which is the length of the linked list.Final Answer:
Returns the number of nodes in the linked list -> Option CQuick Check:
Counting nodes = length [OK]
- Confusing length with node value
- Trying to delete nodes instead of counting
- Returning the last node instead of count
Solution
Step 1: Understand pointer initialization
In C++, a pointer to a node is declared asNode*and can be initialized toNULLornullptr.Step 2: Check each option
Node* head = NULL; usesNode* head = NULL;which is correct pointer initialization syntax.Final Answer:
Node* head = NULL; -> Option AQuick Check:
Pointer declaration uses * and NULL [OK]
- Declaring pointer without *
- Assigning NULL to non-pointer variable
- Using 0 instead of NULL (less clear)
struct Node {
int data;
Node* next;
};
int getLength(Node* head) {
int count = 0;
while (head != NULL) {
count++;
head = head->next;
}
return count;
}
int main() {
Node n3 = {30, NULL};
Node n2 = {20, &n3};
Node n1 = {10, &n2};
int length = getLength(&n1);
std::cout << length;
return 0;
}Solution
Step 1: Trace the linked list nodes
The list has nodes n1 -> n2 -> n3 -> NULL, so total 3 nodes.Step 2: Count nodes in getLength
The function counts each node until NULL, so count becomes 3.Final Answer:
3 -> Option AQuick Check:
3 nodes counted = 3 [OK]
- Counting only first node
- Off-by-one error in loop
- Confusing data values with count
int getLength(Node* head) {
int count = 0;
while (head->next != NULL) {
count++;
head = head->next;
}
return count;
}Solution
Step 1: Analyze the loop condition
The loop runs whilehead->next != NULL, so it stops before the last node.Step 2: Check counting logic
Since last node is not counted, the returned count is one less than actual length.Final Answer:
It misses counting the last node -> Option BQuick Check:
Loop condition excludes last node [OK]
- Using head->next in loop condition
- Not counting last node
- Confusing node data with count
getLength function to return the length only if all node values are positive? If any node has zero or negative value, return -1.Solution
Step 1: Traverse each node checking value
While moving through the list, check if node's data is <= 0; if yes, return -1 immediately.Step 2: Count nodes if all values positive
If all nodes have positive values, count them and return the count as length.Final Answer:
Traverse nodes, if value <= 0 return -1, else count nodes and return count -> Option DQuick Check:
Check values during count, return -1 if any non-positive [OK]
- Ignoring node values during count
- Checking only first node value
- Returning length even if negative values exist
Ready to prove it?
Pass the test and this pattern moves unknown
