Traversal and Printing a Linked List in DSA Typescript - Time & Space Complexity
We want to understand how long it takes to go through a linked list and print its values.
The question is: how does the time needed change when the list gets bigger?
Analyze the time complexity of the following code snippet.
class Node {
value: number;
next: Node | null;
constructor(value: number) {
this.value = value;
this.next = null;
}
}
function printList(head: Node | null): void {
let current = head;
while (current !== null) {
console.log(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 per node, so as many times as there are nodes (n times).
As the list gets longer, the number of 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 time grows in a straight line with the number of nodes.
Time Complexity: O(n)
This means the time to print the list grows directly with the number of nodes.
[X] Wrong: "The printing takes constant time because it just prints values."
[OK] Correct: Each node must be visited and printed, so time grows with the list size, not fixed.
Understanding this helps you explain how simple list operations scale, a key skill in many coding interviews.
"What if we changed the linked list to a doubly linked list? How would the time complexity of printing change?"
Practice
Solution
Step 1: Understand traversal meaning
Traversal means going through each element in order to access or process it.Step 2: Apply to linked list context
In a linked list, traversal means visiting each node starting from the head until the end (null).Final Answer:
Visit each node in the list one by one from start to end -> Option AQuick Check:
Traversal = Visit nodes sequentially [OK]
- Confusing traversal with insertion or deletion
- Thinking traversal changes the list order
- Assuming traversal skips nodes
Solution
Step 1: Identify starting point for traversal
Traversal starts from the first node, called head, to visit all nodes.Step 2: Check options for correct initialization
Setting current to head allows moving through nodes; tail or null won't start traversal properly.Final Answer:
let current = head; -> Option AQuick Check:
Start traversal at head node [OK]
- Starting traversal from tail which is the last node
- Initializing current to null which skips traversal
- Creating a new node instead of using existing head
class Node {
value: number;
next: Node | null;
constructor(value: number) {
this.value = value;
this.next = null;
}
}
const head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
let current = head;
while (current !== null) {
console.log(current.value);
current = current.next;
}Solution
Step 1: Understand list structure
The list nodes are linked as 1 -> 2 -> 3 -> null.Step 2: Trace the while loop output
The loop prints current.value starting at head (1), then moves to 2, then 3, then stops at null.Final Answer:
1 2 3 -> Option BQuick Check:
Print nodes in order: 1 2 3 [OK]
- Printing in reverse order by mistake
- Skipping nodes by incorrect pointer update
- Confusing node values order
let current = head;
while (current.next !== null) {
console.log(current.value);
current = current.next;
}Solution
Step 1: Analyze loop condition
The loop stops when current.next is null, so it skips printing the last node.Step 2: Fix condition to include last node
Changing condition to current !== null ensures all nodes, including last, are printed.Final Answer:
Change condition to current !== null -> Option DQuick Check:
Loop until current is null to print all nodes [OK]
- Using current.next !== null misses last node
- Not updating current inside loop causes infinite loop
- Printing after loop instead of inside
Solution
Step 1: Traverse from head to end
Start at head (5), then move to 10, then 15, collecting values in order.Step 2: Collect values in array during traversal
Push each node's value into an array as you visit it, resulting in [5, 10, 15].Final Answer:
[5, 10, 15] -> Option CQuick Check:
Values collected in order visited [OK]
- Collecting values in reverse order
- Skipping nodes by wrong pointer update
- Returning array before traversal completes
Ready to prove it?
Pass the test and this pattern moves unknown
