Concept Flow - Traversal and Printing a Linked List
Start at head node
↓
Is current node null?
Yes→Stop traversal
No↓
Print current node data
↓
Move to next node
↩Back to check current node
Start from the head node, check if current node exists, print its data, then move to the next node until the end.
Execution Sample
DSA Javascript
function printList(head) {
let current = head;
while (current !== null) {
console.log(current.data);
current = current.next;
}
}
This code visits each node in the linked list from head to end and prints the data stored in each node.
Execution Table
Step
Operation
Current Node Data
Pointer Changes
Visual State
1
Start at head
1
current = head (node with data 1)
1 -> 2 -> 3 -> null
2
Check current != null
1
No change
1 -> 2 -> 3 -> null
3
Print current.data
1
No change
1 -> 2 -> 3 -> null
4
Move to next node
2
current = current.next (node with data 2)
1 -> 2 -> 3 -> null
5
Check current != null
2
No change
1 -> 2 -> 3 -> null
6
Print current.data
2
No change
1 -> 2 -> 3 -> null
7
Move to next node
3
current = current.next (node with data 3)
1 -> 2 -> 3 -> null
8
Check current != null
3
No change
1 -> 2 -> 3 -> null
9
Print current.data
3
No change
1 -> 2 -> 3 -> null
10
Move to next node
null
current = current.next (null)
1 -> 2 -> 3 -> null
11
Check current != null
null
No change
1 -> 2 -> 3 -> null
12
End traversal
null
Traversal stops because current is null
1 -> 2 -> 3 -> null
💡 Traversal stops when current node is null, meaning end of list reached.
Variable Tracker
Variable
Start
After Step 4
After Step 7
After Step 10
Final
current
head (data=1)
node with data=2
node with data=3
null
null
Key Moments - 3 Insights
Why do we check if current is null before printing?
Because if current is null, it means we reached the end of the list and there is no data to print. This is shown in steps 2, 5, 8, and 11 in the execution table.
Why do we move current to current.next after printing?
To visit the next node in the list. Without moving current, the loop would print the same node repeatedly. This is shown in steps 4, 7, and 10.
What does the visual state '1 -> 2 -> 3 -> null' mean?
It shows the linked list nodes in order, with arrows pointing to the next node, ending with null which means no more nodes. This state stays the same during traversal as nodes are not changed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of current after step 7?
ANode with data 3
BNode with data 2
CNull
DHead node
💡 Hint
Check the 'Pointer Changes' column at step 7 in the execution table.
At which step does the traversal stop because current is null?
AStep 11
BStep 12
CStep 10
DStep 9
💡 Hint
Look for the step with 'End traversal' in the 'Operation' column.
If the linked list had only one node, how many times would the print operation run?
ATwice
BZero times
COnce
DThree times
💡 Hint
Refer to the execution table and think about how many nodes are printed.
Concept Snapshot
Traversal and Printing a Linked List:
- Start at head node
- While current node is not null:
- Print current node data
- Move to next node
- Stop when current is null (end of list)
Full Transcript
Traversal and printing a linked list means starting at the head node and visiting each node one by one. At each node, we print the data stored there. We keep moving to the next node until we reach the end, which is when the current node is null. This process ensures all nodes are visited and their data shown in order.
Practice
(1/5)
1. What does it mean to traverse a linked list?
easy
A. To add a new node at the end of the list
B. To delete the first node of the list
C. To visit each node in the list one by one from start to end
D. To reverse the order of nodes in the list
Solution
Step 1: Understand traversal meaning
Traversal means going through each element in a data structure in order.
Step 2: Apply to linked list
For a linked list, traversal means visiting each node from the first to the last.
Final Answer:
To visit each node in the list one by one from start to end -> Option C
Quick Check:
Traversal = Visit nodes in order [OK]
Hint: Traversal means visiting nodes one by one [OK]
Common Mistakes:
Confusing traversal with insertion or deletion
Thinking traversal changes the list order
Assuming traversal skips nodes
2. Which of the following is the correct way to start traversing a singly linked list in JavaScript?
easy
A. let current = head;
B. let current = tail;
C. let current = null;
D. let current = head.next.next;
Solution
Step 1: Identify the start of the list
The traversal starts from the first node, called head.
Step 2: Assign current pointer
Setting current = head means we start visiting from the first node.
Final Answer:
let current = head; -> Option A
Quick Check:
Start traversal at head [OK]
Hint: Traversal always starts at head node [OK]
Common Mistakes:
Starting at tail which is the last node
Starting at null which means no node
Skipping nodes by starting at head.next.next
3. What will be the output of the following code snippet?
class Node {
constructor(value) {
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;
let result = '';
while (current !== null) {
result += current.value + ' -> ';
current = current.next;
}
result += 'null';
console.log(result);
medium
A. null -> 1 -> 2 -> 3
B. 1 -> 2 -> 3 -> null
C. 1 -> 2 -> 3
D. 3 -> 2 -> 1 -> null
Solution
Step 1: Trace the linked list nodes
The list has nodes with values 1, 2, and 3 linked in order.
Step 2: Follow the while loop
The loop appends each node's value followed by ' -> ' until current is null, then adds 'null'.
Final Answer:
1 -> 2 -> 3 -> null -> Option B
Quick Check:
Print nodes in order with arrow and null [OK]
Hint: Follow each node and append value with arrow [OK]
Common Mistakes:
Forgetting to add 'null' at the end
Printing nodes in reverse order
Missing arrows between nodes
4. The following code is intended to print all values in a linked list. What is the error?
let current = head;
while (current.next !== null) {
console.log(current.value);
current = current.next;
}
medium
A. It will print undefined values
B. It will cause an infinite loop
C. It will throw a syntax error
D. It will skip printing the last node's value
Solution
Step 1: Analyze the loop condition
The loop runs while current.next is not null, so it stops before last node.
Step 2: Check which nodes get printed
It prints current.value inside loop, so last node (where current.next is null) is not printed.
Final Answer:
It will skip printing the last node's value -> Option D
Quick Check:
Loop condition excludes last node [OK]
Hint: Use current !== null to include last node [OK]
Common Mistakes:
Using current.next !== null instead of current !== null
Assuming last node is printed
Confusing loop condition with syntax error
5. You want to print a linked list in reverse order without modifying the list or using extra arrays. Which approach is best?
hard
A. Use recursion to traverse to the end, then print on return
B. Traverse normally and print values as you go
C. Reverse the list first, then print
D. Use a loop with current.next !== null condition
Solution
Step 1: Understand the problem constraints
We cannot modify the list or use extra arrays, so reversing or storing values is not allowed.
Step 2: Use recursion to print in reverse
Recursion goes to the end of the list, then prints values on the way back, achieving reverse order without extra storage.
Final Answer:
Use recursion to traverse to the end, then print on return -> Option A
Quick Check:
Recursion prints reverse without extra space [OK]
Hint: Recursion prints reverse by returning from end [OK]
Common Mistakes:
Trying to reverse list which modifies it
Using loops that print forward only
Using extra arrays which is disallowed
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.