Traversal Forward and Backward in DSA Java - Time & Space Complexity
We want to understand how long it takes to move through a list from start to end and then back from end to start.
How does the time needed change as the list gets bigger?
Analyze the time complexity of the following code snippet.
// Assume a doubly linked list with head and tail nodes
Node current = head;
// Traverse forward
while (current != null) {
System.out.print(current.data + " -> ");
current = current.next;
}
System.out.print("null\n");
// Traverse backward
current = tail;
while (current != null) {
System.out.print(current.data + " -> ");
current = current.prev;
}
System.out.print("null\n");
This code moves through a doubly linked list first from the start to the end, then from the end back to the start, printing each element.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Two separate loops that visit each node once.
- How many times: Each loop runs once for every node in the list.
As the list grows, the time to go forward and backward grows in a simple way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 steps (10 forward + 10 backward) |
| 100 | About 200 steps (100 forward + 100 backward) |
| 1000 | About 2000 steps (1000 forward + 1000 backward) |
Pattern observation: The total steps double as the list size doubles, growing directly with the size.
Time Complexity: O(n)
This means the time to traverse forward and backward grows in a straight line with the number of nodes.
[X] Wrong: "Since we traverse twice, the time complexity is O(n²)."
[OK] Correct: Each traversal is separate and runs once over all nodes, so total steps add up, not multiply.
Understanding how simple traversals scale helps you explain and optimize list operations clearly in interviews.
"What if we only traversed forward once but printed each node twice? How would the time complexity change?"
Practice
Solution
Step 1: Understand linked list structure
A linked list has nodes connected from head to tail in one direction.Step 2: Define forward traversal
Forward traversal means visiting nodes starting at the head and moving to the tail in order.Final Answer:
Visiting nodes from the head to the tail in order -> Option CQuick Check:
Forward traversal = head to tail [OK]
- Confusing forward with backward traversal
- Thinking traversal is random
- Assuming skipping nodes is traversal
Solution
Step 1: Identify forward traversal in singly linked list
Forward traversal uses the next pointer starting from head.Step 2: Check code correctness
for(Node curr = head; curr != null; curr = curr.next) { System.out.print(curr.data + " "); } uses curr = head; curr != null; curr = curr.next which is correct for forward traversal.Final Answer:
for(Node curr = head; curr != null; curr = curr.next) { System.out.print(curr.data + " "); } -> Option BQuick Check:
Use next pointer from head for forward traversal [OK]
- Using prev pointer in singly linked list
- Starting from tail in singly linked list
- Using wrong loop conditions
Node tail = thirdNode; // node with data 3
Node curr = tail;
while(curr != null) {
System.out.print(curr.data + " ");
curr = curr.prev;
}Solution
Step 1: Understand doubly linked list backward traversal
Backward traversal starts at tail and moves using prev pointer to head.Step 2: Trace the loop output
Starting at node 3, print 3, move to 2, print 2, move to 1, print 1, then stop.Final Answer:
3 2 1 -> Option AQuick Check:
Backward traversal prints 3 2 1 [OK]
- Confusing forward and backward traversal
- Printing nodes in wrong order
- Using next pointer instead of prev
Node curr = head;
while(curr != null) {
System.out.print(curr.data + " ");
curr = curr.prev;
}Solution
Step 1: Analyze traversal start and direction
Traversal starts at head but moves using prev pointer which is null at head.Step 2: Identify error cause
Using prev from head causes immediate null pointer or no traversal backward possible.Final Answer:
Starting traversal from head but using prev pointer causes null pointer error -> Option DQuick Check:
Backward traversal must start at tail, not head [OK]
- Starting backward traversal at head
- Confusing prev and next pointers
- Incorrect loop conditions
Solution
Step 1: Understand traversal without extra space
Using the doubly linked list pointers allows forward and backward traversal without extra storage.Step 2: Apply forward then backward traversal
First traverse from head to tail printing nodes, then from tail to head printing nodes using prev pointers.Final Answer:
Traverse forward from head to tail printing nodes, then traverse backward from tail to head printing nodes -> Option AQuick Check:
Use head and tail pointers for forward and backward traversal [OK]
- Using extra space unnecessarily
- Traversing only forward twice
- Confusing traversal directions
Ready to prove it?
Pass the test and this pattern moves unknown
