📖 Scenario: You are working on a simple program that manages a list of tasks. Each task is stored as a node in a linked list. You want to see all the tasks in order by traversing the linked list and printing each task.
🎯 Goal: Build a linked list with three tasks, then write code to traverse the linked list and print all tasks in order.
📋 What You'll Learn
Create a linked list with three nodes containing the tasks: 'Buy groceries', 'Clean room', and 'Pay bills'.
Create a variable to start traversal from the head of the linked list.
Use a while loop to traverse the linked list until the end.
Print each task followed by ' -> ' and end with 'null' to show the list structure.
💡 Why This Matters
🌍 Real World
Linked lists are used in many applications like task scheduling, undo functionality in software, and managing playlists where order and dynamic size matter.
💼 Career
Understanding linked list traversal is fundamental for software developers, especially when working with data structures that require dynamic memory management and efficient insertions or deletions.
Progress0 / 4 steps
1
Create the linked list nodes
Create three linked list nodes named node1, node2, and node3. Set their task properties to 'Buy groceries', 'Clean room', and 'Pay bills' respectively. Link them by setting node1.next = node2 and node2.next = node3. Set node3.next = null.
DSA Javascript
Hint
Think of each node as a box holding a task and a pointer to the next box. Link them in order.
2
Set the head pointer for traversal
Create a variable called current and set it to node1 to start traversing from the first node.
DSA Javascript
Hint
The current variable will help you move through the list starting at the first node.
3
Traverse the linked list
Use a while loop with the condition current !== null. Inside the loop, print the current task followed by ' -> '. Then update current to current.next to move to the next node.
DSA Javascript
Hint
Keep moving from one node to the next until you reach the end (null).
4
Print the end of the linked list
After the loop, print 'null' to show the end of the linked list.
DSA Javascript
Hint
Print 'null' after the loop to clearly mark the list's end.
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.