Get Length of Linked List in DSA Go - 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.
func getLength(head *Node) int {
length := 0
current := head
for current != nil {
length++
current = current.Next
}
return length
}
This code counts how many nodes are in a linked list by moving from the first node to the end.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The loop that moves through each node one by one.
- How many times: Once for every node in the list until the end.
As the list gets longer, the number of steps grows directly with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 steps |
| 100 | 100 steps |
| 1000 | 1000 steps |
Pattern observation: The steps increase evenly as the list size increases.
Time Complexity: O(n)
This means the time to find the length grows in direct proportion to the number of nodes in the list.
[X] Wrong: "Getting the length is a constant time operation because we just count once."
[OK] Correct: We must visit each node to count it, so the time grows with the list size, not fixed.
Understanding this helps you explain how simple list operations scale and shows you can analyze basic loops well.
"What if the linked list stored its length as a variable updated on insert/delete? 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 counts how many nodes are present in the list.Final Answer:
The number of nodes in the linked list -> Option CQuick Check:
Length = count of nodes [OK]
- Confusing length with sum of values
- Thinking length is memory size
- Using first node value as length
Solution
Step 1: Identify correct variable type for counting
Length is a number, so it should be an integer starting at zero.Step 2: Check Go syntax for initialization
Using length := 0 correctly declares an int variable with zero value.Final Answer:
length := 0 -> Option AQuick Check:
Count variable starts at 0 [OK]
- Using nil instead of zero
- Using string "0" instead of int 0
- Using boolean false instead of number
type Node struct {
value int
next *Node
}
func getLength(head *Node) int {
length := 0
current := head
for current != nil {
length++
current = current.next
}
return length
}
// Linked list: 10 -> 20 -> 30 -> nil
head := &Node{10, &Node{20, &Node{30, nil}}}
fmt.Println(getLength(head))Solution
Step 1: Trace the loop through nodes
Start at head (10), count 1, move to next (20), count 2, move to next (30), count 3, next is nil, stop.Step 2: Return the count
The loop counted 3 nodes, so length is 3.Final Answer:
3 -> Option BQuick Check:
Nodes counted = 3 [OK]
- Stopping loop too early
- Not incrementing length inside loop
- Confusing nil check
func getLength(head *Node) int {
length := 0
for head.next != nil {
length++
head = head.next
}
return length
}Solution
Step 1: Analyze loop condition
The loop runs while head.next != nil, so it stops before the last node.Step 2: Check counting logic
Since last node's next is nil, it is not counted, so length is one less than actual.Final Answer:
It misses counting the last node -> Option AQuick Check:
Loop condition excludes last node [OK]
- Using head.next != nil instead of head != nil
- Not counting last node
- Assuming loop covers all nodes
func getLength(head *Node) int {
length := 0
current := head
for current != nil {
// Your condition here
current = current.next
}
return length
}Solution
Step 1: Add condition to count only even values
Inside the loop, check if current.value is divisible by 2 (even).Step 2: Increment length only if condition is true
Use if current.value%2 == 0 { length++ } to count even nodes.Final Answer:
Add if current.value%2 == 0 { length++ } inside the loop -> Option DQuick Check:
Count only even nodes [OK]
- Counting all nodes without condition
- Checking wrong condition like > 0
- Skipping loop entirely
Ready to prove it?
Pass the test and this pattern moves unknown
