📖 Scenario: You are working with a simple linked list that stores numbers. You want to find out how many numbers are in the list.
🎯 Goal: Build a Java program that creates a linked list, then counts and prints the number of nodes in it.
📋 What You'll Learn
Create a linked list with exactly 4 nodes containing the values 10, 20, 30, and 40
Create a variable to count the nodes
Use a loop to go through the linked list and count each node
Print the total count of nodes
💡 Why This Matters
🌍 Real World
Linked lists are used in many applications like music playlists, undo features in apps, and managing tasks in order.
💼 Career
Understanding linked lists and how to find their length is a basic skill for software developers working with data structures and memory management.
Progress0 / 4 steps
1
Create the Linked List
Create a class called Node with an int data field and a Node next field. Then create 4 Node objects named head, second, third, and fourth with values 10, 20, 30, and 40 respectively. Link them so that head points to second, second points to third, and third points to fourth. The fourth node should point to null.
DSA Java
Hint
Remember to create a Node class with a constructor that sets the data and initializes next to null. Then create each node and link them by setting the next field.
2
Create a Counter Variable
Inside the main method, create an integer variable called count and set it to 0. This will keep track of how many nodes are in the linked list.
DSA Java
Hint
Just write int count = 0; inside the main method after creating the linked list.
3
Count the Nodes in the Linked List
Use a Node variable called current and set it to head. Then use a while loop that runs as long as current is not null. Inside the loop, increase count by 1 and move current to the next node by setting current = current.next.
DSA Java
Hint
Start with current = head; then use while (current != null) to loop. Inside the loop, add 1 to count and move current to current.next.
4
Print the Length of the Linked List
Add a System.out.println statement to print the value of count after the loop ends. This will show the total number of nodes in the linked list.
DSA Java
Hint
Use System.out.println(count); to print the number of nodes after counting them.
Practice
(1/5)
1. What does the length of a linked list represent?
easy
A. The sum of all node values
B. The value of the first node
C. The memory size of the linked list
D. The number of nodes in the linked list
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 is how many nodes are present in the list.
Final Answer:
The number of nodes in the linked list -> Option D
Quick Check:
Length = count of nodes [OK]
Hint: Length counts nodes, not values or memory [OK]
Common Mistakes:
Confusing length with sum of values
Thinking length is memory size
Using first node value as length
2. Which of the following Java code snippets correctly initializes a variable to count the length of a linked list named head?
easy
A. int length = head; Node current = 0;
B. int length = 0; Node current = head;
C. Node length = 0; int current = head;
D. int length = null; Node current = head;
Solution
Step 1: Identify correct variable types
Length should be an integer starting at 0; current should be a Node starting at head.
Step 2: Check each option
int length = 0; Node current = head; correctly declares int length = 0; and Node current = head;. Others mix types or assign invalid values.
Final Answer:
int length = 0; Node current = head; -> Option B
Quick Check:
Initialize count as int 0 and pointer as Node head [OK]
Hint: Count is int zero; pointer is Node starting at head [OK]
Common Mistakes:
Mixing variable types
Assigning head to int
Using null for int variable
3. What is the output of the following Java code snippet?
class Node {
int data;
Node next;
Node(int d) { data = d; next = null; }
}
public class Test {
public static int getLength(Node head) {
int count = 0;
Node current = head;
while (current != null) {
count++;
current = current.next;
}
return count;
}
public static void main(String[] args) {
Node n1 = new Node(5);
Node n2 = new Node(10);
Node n3 = new Node(15);
n1.next = n2;
n2.next = n3;
System.out.println(getLength(n1));
}
}
medium
A. 0
B. 2
C. 3
D. NullPointerException
Solution
Step 1: Trace linked list nodes
Nodes are n1 -> n2 -> n3, so total 3 nodes linked.
Step 2: Count nodes in getLength
Loop increments count for each node until current is null, so count = 3.
Final Answer:
3 -> Option C
Quick Check:
3 nodes counted in list [OK]
Hint: Count increments once per node until null [OK]
Common Mistakes:
Counting nodes incorrectly
Expecting 0 for non-empty list
Confusing null pointer errors
4. Identify the error in the following code that tries to get the length of a linked list:
public int getLength(Node head) {
int count = 0;
while (head.next != null) {
count++;
head = head.next;
}
return count;
}
medium
A. It does not count the last node
B. It causes infinite loop
C. It throws NullPointerException immediately
D. It counts nodes twice
Solution
Step 1: Analyze loop condition
The loop runs while head.next != null, so it stops before last node.
Step 2: Check counting logic
Since last node's next is null, it is never counted, so count misses last node.
Final Answer:
It does not count the last node -> Option A
Quick Check:
Loop condition excludes last node [OK]
Hint: Loop on current != null to count all nodes [OK]
Common Mistakes:
Using head.next != null instead of head != null
Missing last node count
Confusing loop conditions
5. Given a linked list where each node contains an integer, write a method to find the length of the list but only count nodes with even values. Which approach is correct?
hard
A. Traverse all nodes, increment count only if node's data % 2 == 0
B. Count nodes until first odd value is found
C. Count all nodes and then subtract odd nodes count
D. Only count nodes if next node is even
Solution
Step 1: Understand problem requirement
We must count nodes with even values only, ignoring odd nodes.
Step 2: Choose correct counting method
Traverse entire list, check each node's data; increment count if even (data % 2 == 0).
Final Answer:
Traverse all nodes, increment count only if node's data % 2 == 0 -> Option A
Quick Check:
Count even nodes by checking each node [OK]
Hint: Check each node's value; count only if even [OK]
Common Mistakes:
Stopping count at first odd node
Subtracting odd count after full count
Checking next node's value instead of current
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.