📖 Scenario: Imagine you have a chain of boxes connected one after another. Each box holds a number and points to the next box. This chain is called a linked list.We want to find out how many boxes are in this chain.
🎯 Goal: You will create a linked list with some numbers, then write code to count how many boxes (nodes) are in the list.
📋 What You'll Learn
Create a linked list with exactly three nodes containing the values 10, 20, and 30 in that order.
Create a variable called length to count the nodes.
Use a while loop to traverse the linked list using a variable called current.
Print the length of the linked list.
💡 Why This Matters
🌍 Real World
Linked lists are used in many programs where data needs to be stored in order but can grow or shrink dynamically, like music playlists or undo history.
💼 Career
Understanding linked lists helps in software development jobs where efficient data storage and manipulation are needed, such as backend development or systems programming.
Progress0 / 4 steps
1
Create a linked list with three nodes
Create three linked list nodes named node1, node2, and node3 with values 10, 20, and 30 respectively. Connect them so that node1.next points to node2, and node2.next points to node3. Set node3.next to null.
DSA Javascript
Hint
Each node is an object with value and next properties. Connect nodes by setting next to the next node or null if last.
2
Set up variables to count nodes
Create a variable called length and set it to 0. Create another variable called current and set it to node1 to start traversing from the first node.
DSA Javascript
Hint
Use let to create variables that will change. length starts at zero because we haven't counted nodes yet.
3
Traverse the linked list to count nodes
Use a while loop with the condition current !== null. Inside the loop, increase length by 1 and move current to the next node by setting current = current.next.
DSA Javascript
Hint
The loop keeps going until current is null, meaning the end of the list.
4
Print the length of the linked list
Write console.log(length) to print the number of nodes in the linked list.
DSA Javascript
Hint
The output should be the number 3 because there are three nodes in the list.
Practice
(1/5)
1. What does the length of a linked list represent?
easy
A. The memory size of the linked list
B. The value of the first node
C. The number of nodes in the linked list
D. The value of the last node
Solution
Step 1: Understand linked list structure
A linked list is made of nodes connected one after another.
Step 2: Define length meaning
The length counts how many nodes are in the list, regardless of their values.
Final Answer:
The number of nodes in the linked list -> Option C
Quick Check:
Length = number of nodes [OK]
Hint: Length counts nodes, not values or memory size [OK]
Common Mistakes:
Confusing length with node values
Thinking length is memory size
Counting only first or last node
2. Which of the following JavaScript code snippets correctly initializes a variable to count the length of a linked list?
easy
A. let length = 0;
B. let length = null;
C. let length = undefined;
D. let length = '0';
Solution
Step 1: Identify correct initialization for counting
Counting starts from zero, so length should be a number 0.
Step 2: Check variable types
Using null, undefined, or string '0' is incorrect for numeric counting.
Final Answer:
let length = 0; -> Option A
Quick Check:
Initialize count with 0 [OK]
Hint: Start counting from zero using a number variable [OK]
Common Mistakes:
Using null or undefined instead of 0
Using string '0' instead of number 0
Not initializing length variable
3. What will be the output of the following code?
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 length = 0;
let current = head;
while (current !== null) {
length++;
current = current.next;
}
console.log(length);
medium
A. 3
B. 2
C. 1
D. 0
Solution
Step 1: Trace linked list nodes
Nodes are 1 -> 2 -> 3 -> null, so 3 nodes total.
Step 2: Count nodes in while loop
Loop increments length for each node until current is null, so length becomes 3.
Final Answer:
3 -> Option A
Quick Check:
Count nodes = 3 [OK]
Hint: Count nodes by traversing until null [OK]
Common Mistakes:
Stopping loop early
Not incrementing length
Incorrectly checking current node
4. Find the error in this code that tries to get the length of a linked list:
function getLength(head) {
let length = 0;
let current = head;
while (current.next !== null) {
length++;
current = current.next;
}
return length;
}
medium
A. The length variable should start at 1, not 0
B. The loop condition should check current !== null, not current.next !== null
C. The function should return current instead of length
D. The current variable should be initialized to null
Solution
Step 1: Analyze loop condition
Checking current.next !== null misses counting the last node.
Step 2: Correct loop condition
Loop should run while current !== null to count all nodes including last.
Final Answer:
The loop condition should check current !== null, not current.next !== null -> Option B
Quick Check:
Loop until current is null to count all nodes [OK]
Hint: Loop until current is null, not current.next [OK]
Common Mistakes:
Stopping before last node
Starting length at 1 incorrectly
Returning wrong variable
5. Given a linked list where each node has a value and a next pointer, how would you modify the length calculation to skip nodes with value 0 and count only nodes with non-zero values?
hard
A. Count all nodes and then subtract the number of zeros found
B. Skip the first node if its value is 0 before starting the loop
C. Set length to the total nodes minus the number of zeros after counting all nodes
D. Increment length only if current.value !== 0 inside the traversal loop
Solution
Step 1: Traverse all nodes
Loop through each node from head to null as usual.
Step 2: Count only non-zero value nodes
Inside loop, increment length only if current.value !== 0 to skip zeros.
Final Answer:
Increment length only if current.value !== 0 inside the traversal loop -> Option D
Quick Check:
Count nodes with value != 0 during traversal [OK]
Hint: Count nodes only if value is not zero during traversal [OK]
Common Mistakes:
Skipping nodes incorrectly
Counting all then subtracting zeros
Modifying list structure
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.