📖 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. To help the user know how many tasks are pending, you need to find the length of this linked list.
🎯 Goal: Build a program that creates a linked list with three tasks, then counts and prints the number of tasks in the list.
📋 What You'll Learn
Create a linked list node class called Node with an integer data and a next pointer.
Create a linked list with exactly three nodes containing the values 10, 20, and 30.
Write code to count the number of nodes in the linked list using a while loop.
Print the length of the linked list.
💡 Why This Matters
🌍 Real World
Linked lists are used in many applications like task scheduling, undo functionality, and managing playlists where dynamic data size is needed.
💼 Career
Understanding linked lists and how to traverse them is a fundamental skill for software developers, especially in roles involving data structure optimization and memory management.
Progress0 / 4 steps
1
Create the Node class and linked list nodes
Create a class called Node with an integer data and a Node reference called next. Then create three nodes named head, second, and third with data values 10, 20, and 30 respectively. Link them so that head.next is second and second.next is third. Set third.next to null.
DSA C#
Hint
Remember to create a class with a constructor to set the data and next pointer. Then create three nodes and link them in order.
2
Create a counter variable to track length
Inside the Main method, create an integer variable called length and set it to 0. This will count the number of nodes in the linked list.
DSA C#
Hint
Just create an integer variable named length and set it to zero.
3
Count the nodes using a while loop
Use a Node variable called current and set it to head. Then write a while loop that runs as long as current is not null. Inside the loop, increase length by 1 and move current to current.next.
DSA C#
Hint
Start from the head node and move through each node until you reach the end, counting as you go.
4
Print the length of the linked list
Add a Console.WriteLine statement to print the text "Length of linked list: " followed by the value of length.
DSA C#
Hint
Use Console.WriteLine to show the length with a descriptive message.
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: Count nodes to get length quickly [OK]
Common Mistakes:
Confusing length with node values
Thinking length is memory size
Assuming length is sum of values
2. Which of the following C# code snippets correctly initializes a variable to count the length of a linked list?
easy
A. int length = 0;
B. int length = null;
C. string length = "0";
D. bool length = false;
Solution
Step 1: Identify correct data type for counting
Length is a number, so it should be an integer type.
Step 2: Check initialization value
Start counting from zero before traversing nodes.
Final Answer:
int length = 0; -> Option A
Quick Check:
Use int and start at 0 [OK]
Hint: Use int zero to start counting length [OK]
Common Mistakes:
Using null for int variable
Using string instead of int
Using bool for counting
3. What will be the output of the following C# code?
public class Node {
public int data;
public Node next;
public Node(int d) { data = d; next = null; }
}
public class LinkedList {
public Node head;
public int GetLength() {
int count = 0;
Node current = head;
while (current != null) {
count++;
current = current.next;
}
return count;
}
}
LinkedList list = new LinkedList();
list.head = new Node(5);
list.head.next = new Node(10);
list.head.next.next = new Node(15);
Console.WriteLine(list.GetLength());
medium
A. 0
B. 2
C. 3
D. Error
Solution
Step 1: Count nodes in the linked list
Nodes are 5 -> 10 -> 15, total 3 nodes.
Step 2: Trace GetLength method
Loop increments count for each node until null is reached.
Final Answer:
3 -> Option C
Quick Check:
3 nodes counted = 3 [OK]
Hint: Count each node until null to get length [OK]
Common Mistakes:
Counting only head node
Returning zero without counting
Confusing next pointer with data
4. Identify the error in this C# method to get linked list length:
public int GetLength() {
int count = 0;
Node current = head;
while (current.next != null) {
count++;
current = current.next;
}
return count;
}
medium
A. It misses counting the last node
B. It causes infinite loop
C. It returns count plus one extra
D. It throws null reference exception
Solution
Step 1: Analyze loop condition
Loop runs while current.next is not null, so last node is skipped.
Step 2: Understand counting effect
Count increments only for nodes before last, missing last node.
Final Answer:
It misses counting the last node -> Option A
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
Not incrementing count for last node
Assuming loop covers all nodes
5. Given a linked list where each node contains an integer, how would you modify the length calculation to count only nodes with even values?
public int GetEvenLength() {
int count = 0;
Node current = head;
while (current != null) {
// Your code here
current = current.next;
}
return count;
}
hard
A. Add count++ unconditionally inside the loop
B. Add if (current.data % 2 == 0) count++; inside the loop
C. Add if (current.data % 2 != 0) count++; inside the loop
D. Return head.data instead of count
Solution
Step 1: Check node value for evenness
Use modulo operator to test if data is even (data % 2 == 0).
Step 2: Increment count only if node is even
Inside loop, increment count only when condition is true.
Final Answer:
Add if (current.data % 2 == 0) count++; inside the loop -> Option B
Quick Check:
Count only even nodes [OK]
Hint: Use modulo to filter even nodes before counting [OK]
Common Mistakes:
Counting all nodes without condition
Counting odd nodes instead of even
Returning node data instead of count
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.