Start from the head node, check if current node exists, count it, move to next, repeat until no nodes left.
Execution Sample
DSA Java
int length = 0;
Node current = head;
while (current != null) {
length++;
current = current.next;
}
Counts nodes by moving through the list from head until the end.
Execution Table
Step
Operation
Current Node
Length Count
Pointer Changes
Visual State
1
Start at head
Node(1)
0
current = head
1 -> 2 -> 3 -> null
2
Check current != null
Node(1)
0
Yes
1 -> 2 -> 3 -> null
3
Increment length
Node(1)
1
length = 1
1 -> 2 -> 3 -> null
4
Move to next node
Node(2)
1
current = current.next
1 -> 2 -> 3 -> null
5
Check current != null
Node(2)
1
Yes
1 -> 2 -> 3 -> null
6
Increment length
Node(2)
2
length = 2
1 -> 2 -> 3 -> null
7
Move to next node
Node(3)
2
current = current.next
1 -> 2 -> 3 -> null
8
Check current != null
Node(3)
2
Yes
1 -> 2 -> 3 -> null
9
Increment length
Node(3)
3
length = 3
1 -> 2 -> 3 -> null
10
Move to next node
null
3
current = current.next
1 -> 2 -> 3 -> null
11
Check current != null
null
3
No
1 -> 2 -> 3 -> null
12
Stop
null
3
Length found
1 -> 2 -> 3 -> null
💡 current becomes null, meaning end of list reached, length is final
Variable Tracker
Variable
Start
After Step 3
After Step 6
After Step 9
Final
length
0
1
2
3
3
current
Node(1)
Node(1)
Node(2)
Node(3)
null
Key Moments - 3 Insights
Why do we check current != null before counting?
Because if current is null, it means we reached the end of the list and there are no more nodes to count. This is shown in execution_table rows 2, 5, 8, and 11 where the check decides to continue or stop.
Why does length increment happen before moving to next node?
We count the current node first because it exists, then move forward. If we moved first, we would skip counting the first node. See execution_table rows 3, 6, 9 for increments before pointer changes in rows 4, 7, 10.
What happens if the list is empty (head is null)?
The loop never runs because current starts as null. The length stays 0. This is implied by the exit condition at step 11 where current is null immediately.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the length count at step 6?
A1
B2
C3
D0
💡 Hint
Check the 'Length Count' column at step 6 in the execution_table.
At which step does the current pointer become null?
AStep 12
BStep 11
CStep 10
DStep 9
💡 Hint
Look at the 'Current Node' column to see when it changes to null.
If the list had only one node, what would be the length after step 3?
A1
B2
C0
Dnull
💡 Hint
Refer to the variable_tracker for length changes after first increment.
Concept Snapshot
Get Length of Linked List:
- Start at head node
- While current node is not null:
- Increment length
- Move to next node
- Stop when current is null
- Length holds total nodes count
Full Transcript
To find the length of a linked list, start at the head node. Check if the current node exists. If yes, increase the length count by one. Then move to the next node. Repeat this until the current node is null, which means the end of the list. The length variable then holds the total number of nodes. If the list is empty, length remains zero because the loop never runs.
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.