Traversal and Printing a Linked List in DSA Java - Time & Space Complexity
We want to understand how long it takes to go through all items in a linked list and print them.
How does the time needed change when the list gets bigger?
Analyze the time complexity of the following code snippet.
class Node {
int data;
Node next;
}
void printList(Node head) {
Node current = head;
while (current != null) {
System.out.println(current.data);
current = current.next;
}
}
This code goes through each node in the linked list from start to end and prints its data.
- Primary operation: Visiting each node once to print its data.
- 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 steps grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 visits and prints |
| 100 | 100 visits and prints |
| 1000 | 1000 visits and prints |
Pattern observation: The work grows directly with the number of nodes. Double the nodes, double the work.
Time Complexity: O(n)
This means the time to print the list grows in a straight line with the number of nodes.
[X] Wrong: "Since we only print, the time is constant no matter the list size."
[OK] Correct: Each node must be visited and printed, so more nodes mean more steps, not a fixed time.
Knowing how to analyze simple list traversal helps you understand more complex list operations and shows you can reason about code efficiency clearly.
"What if we printed only every other node? How would the time complexity change?"
Practice
Solution
Step 1: Understand traversal meaning
Traversal means visiting each node one by one in the linked list.Step 2: Identify traversal purpose
Commonly, traversal is used to perform actions like printing or searching nodes.Final Answer:
To visit each node and perform an action like printing its value -> Option DQuick Check:
Traversal = Visit nodes [OK]
- Confusing traversal with deletion
- Thinking traversal creates a new list
- Assuming traversal sorts the list
head in Java?Solution
Step 1: Identify starting point of traversal
Traversal starts from the first node, which is referenced byhead.Step 2: Choose correct initialization
We assigncurrenttoheadto begin traversal at the first node.Final Answer:
Node current = head; -> Option CQuick Check:
Start traversal at head node [OK]
- Starting from head.next skips first node
- Initializing current to null causes no traversal
- Using head.prev in singly linked list causes error
class Node {
int data;
Node next;
Node(int d) { data = d; next = null; }
}
public class Main {
public static void main(String[] args) {
Node head = new Node(5);
head.next = new Node(10);
head.next.next = new Node(15);
Node current = head;
while (current != null) {
System.out.print(current.data + " -> ");
current = current.next;
}
System.out.print("null");
}
}Solution
Step 1: Trace node creation and linking
Nodes created with data 5, 10, 15 linked as 5 -> 10 -> 15 -> null.Step 2: Follow while loop printing
Loop prints each node's data followed by ' -> ', then ends with 'null'.Final Answer:
5 -> 10 -> 15 -> null -> Option AQuick Check:
Print nodes in order with arrows [OK]
- Printing null at start instead of end
- Missing arrows between nodes
- Stopping before last node
Node current = head;
while (current.next != null) {
System.out.print(current.data + " -> ");
current = current.next;
}
System.out.print("null");Solution
Step 1: Analyze loop condition
The loop stops when current.next is null, so last node is not printed.Step 2: Fix condition to include last node
Changing condition to current != null ensures last node is printed.Final Answer:
Change condition to current != null to print last node -> Option BQuick Check:
Loop until current is null to print all nodes [OK]
- Using current.next != null misses last node
- Skipping first node by wrong initialization
- Removing null print causes unclear output
Solution
Step 1: Understand limitation of singly linked list
Singly linked list nodes only point forward, so backward traversal is not direct.Step 2: Use recursion to print in reverse
Recursion goes to end first, then prints nodes on returning back, achieving reverse print without changing list.Final Answer:
Use recursion to traverse to the end, then print nodes on return -> Option AQuick Check:
Recursion prints reverse without modifying list [OK]
- Trying to loop backward in singly linked list
- Reversing list changes structure, not allowed here
- Printing twice forward and backward duplicates output
Ready to prove it?
Pass the test and this pattern moves unknown
