Traversal Forward and Backward in DSA C# - Time & Space Complexity
We want to understand how long it takes to move through a list from start to end and then back again.
How does the time grow when the list gets bigger?
Analyze the time complexity of the following code snippet.
// Traverse forward and backward in a doubly linked list
void TraverseForwardBackward(Node head) {
Node current = head;
while (current.Next != null) {
// Move forward
current = current.Next;
}
while (current != null) {
// Move backward
current = current.Prev;
}
}
This code moves through the list from the first node to the last, then back from the last to the first.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Two while loops that each traverse the list nodes.
- How many times: Each loop runs once over all nodes, so each runs n-1 times if the list has n nodes.
As the list gets bigger, the time to go forward and backward grows in a simple way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 18 steps (9 forward + 9 backward) |
| 100 | About 198 steps |
| 1000 | About 1998 steps |
Pattern observation: The total steps grow roughly twice the size of the list minus 2, so it grows linearly.
Time Complexity: O(n)
This means the time to traverse forward and backward grows directly with the number of nodes.
[X] Wrong: "Because there are two loops, the time is O(n²)."
[OK] Correct: Each loop runs separately over the list once, so their times add up, not multiply.
Understanding how simple traversals scale helps you explain and optimize list operations clearly in interviews.
"What if we combined the forward and backward traversal into one loop? How would the time complexity change?"
Practice
Traversal Forward and Backward mean in the context of a doubly linked list?Solution
Step 1: Understand traversal directions
Traversal forward means visiting nodes from the first node to the last node. Traversal backward means visiting nodes from the last node back to the first node.Step 2: Apply to doubly linked list
A doubly linked list allows moving in both directions because each node has links to the next and previous nodes.Final Answer:
Visiting nodes from the start to the end and then from the end back to the start -> Option AQuick Check:
Forward and backward traversal = visiting nodes both ways [OK]
- Confusing traversal with random access
- Thinking traversal is only forward
- Ignoring backward links in doubly linked list
Solution
Step 1: Identify forward traversal
Forward traversal starts at the head and moves to the next node repeatedly until null.Step 2: Check syntax correctness
for(Node current = head; current != null; current = current.Next) { /* process current */ } correctly starts at head and moves using current = current.Next. Other options either start at tail or move backward.Final Answer:
for(Node current = head; current != null; current = current.Next) { /* process current */ } -> Option BQuick Check:
Forward traversal uses Next pointer from head [OK]
- Starting at tail for forward traversal
- Using Prev pointer for forward traversal
- Confusing Next and Prev pointers
Node current = head;
while(current != null) {
Console.Write(current.Value + " -> ");
current = current.Next;
}
Console.WriteLine("null");Solution
Step 1: Understand the list structure
The list nodes are 1 linked to 2, then 3, then 4, in order.Step 2: Trace the forward traversal
Starting at head (1), print 1, move to 2, print 2, move to 3, print 3, move to 4, print 4, then current becomes null and loop ends.Final Answer:
1 -> 2 -> 3 -> 4 -> null -> Option CQuick Check:
Forward traversal prints nodes in order [OK]
- Printing backward order instead of forward
- Missing last node in output
- Printing null before nodes
Node current = head;
while(current != null) {
Console.Write(current.Value + " <- ");
current = current.Prev;
}
Console.WriteLine("null");Solution
Step 1: Understand backward traversal
Backward traversal should start from the tail node, not the head.Step 2: Analyze given code
The code starts at head and tries to move backward using Prev, but head.Prev is null, so loop ends immediately.Final Answer:
Starting traversal from head instead of tail -> Option DQuick Check:
Backward traversal must start at tail [OK]
- Starting backward traversal at head
- Confusing Prev and Next pointers
- Ignoring list ends (null pointers)
Solution
Step 1: Traverse forward from head to tail
Start at head (10), print each node's value moving via Next until null: 10 -> 20 -> 30 -> 40 -> null.Step 2: Traverse backward from tail to head
After reaching tail (40), move backward using Prev pointer, printing: 40 -> 30 -> 20 -> 10 -> null.Final Answer:
10 -> 20 -> 30 -> 40 -> null 40 -> 30 -> 20 -> 10 -> null -> Option AQuick Check:
Forward then backward traversal prints nodes in both directions [OK]
- Trying to traverse backward from head
- Printing null before nodes
- Using extra data structures unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
