Traversal and Printing a Linked List in DSA Python - Time & Space Complexity
When we traverse and print a linked list, we want to know how the time taken grows as the list gets longer.
We ask: How does the number of steps change when the list size increases?
Analyze the time complexity of the following code snippet.
class Node:
def __init__(self, data):
self.data = data
self.next = None
def print_linked_list(head):
current = head
while current is not None:
print(current.data)
current = current.next
This code goes through each node in the linked list and prints its data.
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 number of nodes (n).
As the list gets longer, the number of steps grows directly with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 steps (visiting each node once) |
| 100 | 100 steps |
| 1000 | 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 print the list grows directly with the number of nodes.
[X] Wrong: "Printing a linked list is always constant time because we just start at the head."
[OK] Correct: Even though we start at the head, we must visit every node to print it, so time grows with list size.
Understanding how traversing a linked list scales helps you explain simple but important operations clearly in interviews.
"What if we only printed every other node? How would the time complexity change?"
Practice
Solution
Step 1: Understand traversal meaning
Traversal means visiting each element in order to read or process it.Step 2: Apply to linked list context
In a linked list, traversal means starting at the head and moving through each node until the end.Final Answer:
To visit each node in the list one by one from start to end -> Option CQuick Check:
Traversal = Visit nodes sequentially [OK]
- Confusing traversal with insertion or deletion
- Thinking traversal changes the list order
- Assuming traversal skips nodes
head?Solution
Step 1: Understand traversal loop condition
We must continue while current node exists (not None) to visit all nodes.Step 2: Check each option's loop condition
current = head while current is not None: print(current.data) current = current.next useswhile current is not None, which correctly visits all nodes including the last one.Final Answer:
current = head while current is not None: print(current.data) current = current.next -> Option DQuick Check:
Loop until current is None [OK]
- Stopping before last node by checking current.next
- Using for loop with range on linked list
- Loop condition that never ends or skips nodes
class Node:
def __init__(self, data):
self.data = data
self.next = None
head = Node(10)
head.next = Node(20)
head.next.next = Node(30)
current = head
while current is not None:
print(current.data, end=' -> ')
current = current.next
print('None')Solution
Step 1: Trace the linked list nodes
Nodes contain 10, 20, 30 linked in order: 10 -> 20 -> 30 -> None.Step 2: Follow the print loop
Loop prints each node's data followed by ' -> ', then prints 'None' after loop ends.Final Answer:
10 -> 20 -> 30 -> None -> Option AQuick Check:
Print nodes with arrows ending with None [OK]
- Missing 'None' at the end
- Printing nodes without arrows
- Stopping before last node
current = head
while current.next is not None:
print(current.data)
current = current.next
print(current.data)Solution
Step 1: Analyze loop condition
The loop runs whilecurrent.next is not None, so it stops before the last node.Step 2: Check printing after loop
After loop, it prints the last node's data separately, so all nodes are printed but last node outside loop.Final Answer:
It misses printing the last node inside the loop -> Option BQuick Check:
Loop condition skips last node printing [OK]
- Using current.next in loop condition
- Expecting all nodes printed inside loop
- Confusing infinite loop with this error
def print_reverse(node):
if node is None:
return
# What goes here?
print(node.data, end=' -> ')Solution
Step 1: Understand recursion for reverse print
Recursively call function on next node first to reach end before printing.Step 2: Place recursive call before print
Callingprint_reverse(node.next)before printing current node prints in reverse order.Final Answer:
print_reverse(node.next) -> Option AQuick Check:
Recursive call before print reverses order [OK]
- Printing before recursive call (prints forward)
- Calling recursion on same node (infinite recursion)
- Calling recursion twice causing errors
Ready to prove it?
Pass the test and this pattern moves unknown
