Get Length of Linked List in DSA Java - 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 grow when the list gets longer?
Analyze the time complexity of the following code snippet.
public int getLength(Node head) {
int length = 0;
Node current = head;
while (current != null) {
length++;
current = current.next;
}
return length;
}
This code counts how many nodes are in a linked list by moving through each node one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Moving from one node to the next in the linked list.
- How many times: Once for each node until the end of the list (n times).
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: "Getting the length is instant or constant time because we just count once."
[OK] Correct: We must visit each node to count it, so the time grows with the list size.
Knowing how to analyze simple linked list operations like this helps build confidence for more complex problems.
"What if the linked list stored its length as a variable 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 DQuick Check:
Length = count of nodes [OK]
- Confusing length with sum of values
- Thinking length is memory size
- Using first node value as length
head?Solution
Step 1: Identify correct variable types
Length should be an integer starting at 0; current should be a Node starting at head.Step 2: Check each option
int length = 0; Node current = head; correctly declaresint length = 0;andNode current = head;. Others mix types or assign invalid values.Final Answer:
int length = 0; Node current = head; -> Option BQuick Check:
Initialize count as int 0 and pointer as Node head [OK]
- Mixing variable types
- Assigning head to int
- Using null for int variable
class Node {
int data;
Node next;
Node(int d) { data = d; next = null; }
}
public class Test {
public static int getLength(Node head) {
int count = 0;
Node current = head;
while (current != null) {
count++;
current = current.next;
}
return count;
}
public static void main(String[] args) {
Node n1 = new Node(5);
Node n2 = new Node(10);
Node n3 = new Node(15);
n1.next = n2;
n2.next = n3;
System.out.println(getLength(n1));
}
}Solution
Step 1: Trace linked list nodes
Nodes are n1 -> n2 -> n3, so total 3 nodes linked.Step 2: Count nodes in getLength
Loop increments count for each node until current is null, so count = 3.Final Answer:
3 -> Option CQuick Check:
3 nodes counted in list [OK]
- Counting nodes incorrectly
- Expecting 0 for non-empty list
- Confusing null pointer errors
public int getLength(Node head) {
int count = 0;
while (head.next != null) {
count++;
head = head.next;
}
return count;
}Solution
Step 1: Analyze loop condition
The loop runs whilehead.next != null, so it stops before last node.Step 2: Check counting logic
Since last node'snextis null, it is never counted, so count misses last node.Final Answer:
It does not count the last node -> Option AQuick Check:
Loop condition excludes last node [OK]
- Using head.next != null instead of head != null
- Missing last node count
- Confusing loop conditions
Solution
Step 1: Understand problem requirement
We must count nodes with even values only, ignoring odd nodes.Step 2: Choose correct counting method
Traverse entire list, check each node's data; increment count if even (data % 2 == 0).Final Answer:
Traverse all nodes, increment count only if node's data % 2 == 0 -> Option AQuick Check:
Count even nodes by checking each node [OK]
- Stopping count at first odd node
- Subtracting odd count after full count
- Checking next node's value instead of current
Ready to prove it?
Pass the test and this pattern moves unknown
