Get Length of Linked List in DSA C# - Time & Space Complexity
We want to know how long it takes to find the length of a linked list as the list grows.
How does the time needed change when the list gets bigger?
Analyze the time complexity of the following code snippet.
public int GetLength(Node head) {
int count = 0;
Node current = head;
while (current != null) {
count++;
current = current.next;
}
return count;
}
This code counts how many nodes are in a linked list by moving through each node once.
- Primary operation: Looping through each node in the linked list.
- How many times: Once for every node in the list (n times).
As the list gets longer, the time to count nodes 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 evenly as the list size increases.
Time Complexity: O(n)
This means the time to get the length grows in a straight line with the number of nodes.
[X] Wrong: "Getting the length is instant because we just need one number."
[OK] Correct: We must visit each node to count them, so time grows with list size.
Knowing how to find the length of a linked list efficiently is a basic skill that shows you understand list traversal and loops.
"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 node values
- Thinking length is memory size
- Assuming length is sum of values
Solution
Step 1: Identify correct data type for counting
Length is a number, so it should be an integer type.Step 2: Check initialization value
Start counting from zero before traversing nodes.Final Answer:
int length = 0; -> Option AQuick Check:
Use int and start at 0 [OK]
- Using null for int variable
- Using string instead of int
- Using bool for counting
public class Node {
public int data;
public Node next;
public Node(int d) { data = d; next = null; }
}
public class LinkedList {
public Node head;
public int GetLength() {
int count = 0;
Node current = head;
while (current != null) {
count++;
current = current.next;
}
return count;
}
}
LinkedList list = new LinkedList();
list.head = new Node(5);
list.head.next = new Node(10);
list.head.next.next = new Node(15);
Console.WriteLine(list.GetLength());Solution
Step 1: Count nodes in the linked list
Nodes are 5 -> 10 -> 15, total 3 nodes.Step 2: Trace GetLength method
Loop increments count for each node until null is reached.Final Answer:
3 -> Option CQuick Check:
3 nodes counted = 3 [OK]
- Counting only head node
- Returning zero without counting
- Confusing next pointer with data
public int GetLength() {
int count = 0;
Node current = head;
while (current.next != null) {
count++;
current = current.next;
}
return count;
}Solution
Step 1: Analyze loop condition
Loop runs while current.next is not null, so last node is skipped.Step 2: Understand counting effect
Count increments only for nodes before last, missing last node.Final Answer:
It misses counting the last node -> Option AQuick Check:
Loop condition excludes last node [OK]
- Using current.next != null instead of current != null
- Not incrementing count for last node
- Assuming loop covers all nodes
public int GetEvenLength() {
int count = 0;
Node current = head;
while (current != null) {
// Your code here
current = current.next;
}
return count;
}Solution
Step 1: Check node value for evenness
Use modulo operator to test if data is even (data % 2 == 0).Step 2: Increment count only if node is even
Inside loop, increment count only when condition is true.Final Answer:
Add if (current.data % 2 == 0) count++; inside the loop -> Option BQuick Check:
Count only even nodes [OK]
- Counting all nodes without condition
- Counting odd nodes instead of even
- Returning node data instead of count
Ready to prove it?
Pass the test and this pattern moves unknown
