Traversal and Printing a Linked List in DSA Go - Time & Space Complexity
When we walk through a linked list to print its values, we want to know how long it takes as the list grows.
We ask: How does the time to print all nodes change when the list gets bigger?
Analyze the time complexity of the following code snippet.
// Node represents a linked list node
type Node struct {
value int
next *Node
}
func printList(head *Node) {
current := head
for current != nil {
fmt.Println(current.value)
current = current.next
}
}
This code walks through each node in the linked list and prints its value one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Visiting each node once to print its value.
- How many times: Exactly once per node, so as many times as there are nodes in the list.
As the list grows, the number of print operations grows directly with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 visits and prints |
| 100 | 100 visits and prints |
| 1000 | 1000 visits and prints |
Pattern observation: The operations increase in a straight line as the list size increases.
Time Complexity: O(n)
This means the time to print the list grows directly with the number of nodes.
[X] Wrong: "Printing a linked list is faster than O(n) because we just follow pointers."
[OK] Correct: Even though we follow pointers, we must visit every node once, so time grows with the list size.
Understanding this simple traversal helps build confidence for more complex linked list problems.
"What if we wanted to print only every second node? How would the time complexity change?"
Practice
Solution
Step 1: Understand traversal meaning
Traversal means going through each node in order, starting from the head.Step 2: Purpose of traversal
Visiting each node allows us to read or process data stored in the list.Final Answer:
To visit each node in the list one by one -> Option CQuick Check:
Traversal = visiting nodes [OK]
- Confusing traversal with deletion
- Thinking traversal creates a new list
- Assuming traversal reverses the list
Solution
Step 1: Identify correct loop start and condition
Traversal starts at head and continues while node is not nil.Step 2: Correct pointer update
Move to next node by node = node.next each iteration.Final Answer:
for node := head; node != nil; node = node.next {} -> Option AQuick Check:
Start at head, loop until nil, move next [OK]
- Using node == nil as loop condition (wrong)
- Starting from nil instead of head
- Using node.prev in singly linked list
type Node struct {
value int
next *Node
}
func main() {
n3 := &Node{3, nil}
n2 := &Node{2, n3}
n1 := &Node{1, n2}
for node := n1; node != nil; node = node.next {
fmt.Print(node.value, " -> ")
}
fmt.Print("nil")
}Solution
Step 1: Understand list structure
n1 points to n2, n2 points to n3, n3 points to nil. Values are 1, 2, 3 in order.Step 2: Trace the loop output
Loop prints node.value followed by ' -> ' for each node: 1 -> 2 -> 3 -> then prints 'nil'.Final Answer:
1 -> 2 -> 3 -> nil -> Option DQuick Check:
Print order matches list order [OK]
- Printing values in reverse order
- Missing '->' arrows
- Printing nil at start instead of end
type Node struct {
value int
next *Node
}
func printList(head *Node) {
node := head
for node != nil {
fmt.Println(node.value)
node = node.prev
}
}Solution
Step 1: Check struct fields
Node struct has 'next' pointer but no 'prev' pointer.Step 2: Identify incorrect pointer usage
Code uses 'node = node.prev' which is invalid and causes compile error.Final Answer:
Using 'node.prev' which doesn't exist in singly linked list -> Option AQuick Check:
prev pointer missing in singly linked list [OK]
- Using prev pointer in singly linked list
- Assuming fmt.Println can't print ints
- Thinking loop condition is wrong
func printEven(head *Node) {
for node := head; node != nil; node = node.next {
// Your code here
}
}Solution
Step 1: Use conditional inside traversal
Check if node.value is even using modulo operator inside the loop.Step 2: Print only even values
Print node.value with arrow only if condition is true.Final Answer:
Add 'if node.value%2 == 0 { fmt.Print(node.value, " -> ") }' inside the loop -> Option BQuick Check:
Condition inside loop filters even nodes [OK]
- Changing loop condition incorrectly
- Checking equality to zero instead of evenness
- Printing all nodes then filtering later
Ready to prove it?
Pass the test and this pattern moves unknown
