Traversal and Printing a Linked List in DSA C# - Time & Space Complexity
When we walk through a linked list to print its values, we want to know how the time it takes grows as the list gets bigger.
We ask: How does the work increase when the list has more items?
Analyze the time complexity of the following code snippet.
public void PrintList(Node head)
{
Node current = head;
while (current != null)
{
Console.WriteLine(current.Value);
current = current.Next;
}
}
This code goes through each node in the linked list and prints its value one by one.
- Primary operation: The while loop that visits each node once.
- How many times: Exactly once for each node in the list, so n times if there are n nodes.
As the list gets longer, the number of print 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 work grows in a straight line with the number of nodes.
Time Complexity: O(n)
This means the time to print grows directly with the number of nodes in the list.
[X] Wrong: "Since we only print, it takes constant time no matter the list size."
[OK] Correct: Even though printing one node is quick, we must do it for every node, so the total time grows with the list size.
Understanding how simple list traversal scales helps you explain basic data structure operations clearly and confidently in interviews.
"What if we changed the linked list to a doubly linked list? How would the time complexity of traversal and printing change?"
Practice
Solution
Step 1: Understand traversal
Traversal means going through each node in the linked list starting from the first node.Step 2: Identify what happens during traversal
We visit each node one by one until we reach the end (null).Final Answer:
Visiting each node one by one from start to end -> Option BQuick Check:
Traversal = Visit nodes sequentially [OK]
- Confusing traversal with adding or deleting nodes
- Thinking traversal changes the list order
- Assuming traversal skips nodes
head?Solution
Step 1: Check variable assignment syntax in C#
In C#, to assign a value, use a single equals sign (=), not double (==) or arrows.Step 2: Identify correct assignment for traversal start
We assign the head node to a variable to start traversal:Node current = head;Final Answer:
Node current = head; -> Option CQuick Check:
Assignment uses =, not == or arrows [OK]
- Using == instead of = for assignment
- Using arrow symbols which are invalid
- Confusing assignment with comparison
class Node {
public int data;
public Node next;
public Node(int d) { data = d; next = null; }
}
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
Node current = head;
while (current != null) {
Console.Write(current.data + " -> ");
current = current.next;
}
Console.WriteLine("null");Solution
Step 1: Understand the linked list structure
The list has nodes with data 1, 2, and 3 linked in order.Step 2: Trace the while loop output
The loop prints each node's data followed by ' -> ', then moves to next node until null.Final Answer:
1 -> 2 -> 3 -> null -> Option AQuick Check:
Print nodes in order with arrows and end with null [OK]
- Printing nodes in reverse order
- Missing arrows or null in output
- Printing null at start instead of end
Node current = head;
while (current.next != null) {
Console.Write(current.data + " -> ");
current = current.next;
}
Console.WriteLine("null");Solution
Step 1: Analyze the loop condition
The loop runs while current.next is not null, so it stops before the last node.Step 2: Check what gets printed
Since the last node's data is printed only inside the loop, it is skipped.Final Answer:
It skips printing the last node's data -> Option DQuick Check:
Loop condition excludes last node [OK]
- Using current.next != null instead of current != null
- Assuming last node prints automatically
- Confusing loop condition with node data access
Solution
Step 1: Understand the problem constraints
We cannot change the list structure, so reversing the list is not allowed.Step 2: Use recursion to print in reverse
Recursion visits nodes forward, then prints after returning, effectively printing backwards.Final Answer:
Use recursion to print nodes after recursive call returns -> Option AQuick Check:
Recursion prints in reverse without modifying list [OK]
- Trying to use index-based loops on linked list
- Reversing list which changes structure
- Printing nodes immediately during traversal
Ready to prove it?
Pass the test and this pattern moves unknown
