📖 Scenario: You are working on a simple program to manage a list of tasks. Each task is stored as a node in a linked list. To help with managing the tasks, you need to find out how many tasks are currently in the list.
🎯 Goal: Build a program that creates a linked list with three tasks, then calculates and prints the length of this linked list.
📋 What You'll Learn
Create a linked list with exactly three nodes with values 10, 20, and 30
Create a pointer variable called head that points to the first node
Create an integer variable called length to count the nodes
Use a while loop with a pointer called current to traverse the list
Print the length of the linked list using std::cout
💡 Why This Matters
🌍 Real World
Linked lists are used in many software systems to manage collections of items where size can change dynamically, such as task lists, playlists, or undo histories.
💼 Career
Understanding linked lists and how to traverse them is a fundamental skill for software developers, especially when working with low-level data structures or preparing for technical interviews.
Progress0 / 4 steps
1
Create the linked list nodes
Create three linked list nodes named node1, node2, and node3 of type Node. Set their data values to 10, 20, and 30 respectively. Link node1.next to node2, and node2.next to node3. Set node3.next to nullptr.
DSA C++
Hint
Remember to create each node as a variable of type Node. Set the data field and link the next pointers correctly.
2
Create the head pointer
Create a pointer variable called head of type Node* and set it to point to node1.
DSA C++
Hint
Use a pointer of type Node* and assign it the address of node1.
3
Calculate the length of the linked list
Create an integer variable called length and set it to 0. Create a pointer variable called current and set it to head. Use a while loop that runs while current is not nullptr. Inside the loop, increment length by 1 and move current to current->next.
DSA C++
Hint
Use a while loop to move through the list until current is nullptr. Increase length each time you visit a node.
4
Print the length of the linked list
Use std::cout to print the text "Length of linked list: " followed by the value of length and then a newline.
DSA C++
Hint
Use std::cout to print the message and the length variable, followed by std::endl for a new line.
Practice
(1/5)
1. What does the function getLength do when given the head of a linked list?
easy
A. Deletes the linked list
B. Returns the value of the first node
C. Returns the number of nodes in the linked list
D. Returns the last node of the linked list
Solution
Step 1: Understand the function purpose
The function getLength counts nodes starting from the head until the end.
Step 2: Identify the output
It returns the total count of nodes, which is the length of the linked list.
Final Answer:
Returns the number of nodes in the linked list -> Option C
Quick Check:
Counting nodes = length [OK]
Hint: Count nodes one by one until null [OK]
Common Mistakes:
Confusing length with node value
Trying to delete nodes instead of counting
Returning the last node instead of count
2. Which of the following is the correct syntax to initialize a pointer to the head of a linked list in C++?
easy
A. Node* head = NULL;
B. Node head = NULL;
C. Node* head = 0;
D. Node head = 0;
Solution
Step 1: Understand pointer initialization
In C++, a pointer to a node is declared as Node* and can be initialized to NULL or nullptr.
Step 2: Check each option
Node* head = NULL; uses Node* head = NULL; which is correct pointer initialization syntax.
Final Answer:
Node* head = NULL; -> Option A
Quick Check:
Pointer declaration uses * and NULL [OK]
Hint: Use * for pointers and NULL for empty [OK]
Common Mistakes:
Declaring pointer without *
Assigning NULL to non-pointer variable
Using 0 instead of NULL (less clear)
3. What will be the output of the following code snippet?
struct Node {
int data;
Node* next;
};
int getLength(Node* head) {
int count = 0;
while (head != NULL) {
count++;
head = head->next;
}
return count;
}
int main() {
Node n3 = {30, NULL};
Node n2 = {20, &n3};
Node n1 = {10, &n2};
int length = getLength(&n1);
std::cout << length;
return 0;
}
medium
A. 3
B. 2
C. 1
D. 0
Solution
Step 1: Trace the linked list nodes
The list has nodes n1 -> n2 -> n3 -> NULL, so total 3 nodes.
Step 2: Count nodes in getLength
The function counts each node until NULL, so count becomes 3.
Final Answer:
3 -> Option A
Quick Check:
3 nodes counted = 3 [OK]
Hint: Count nodes by following next pointers until NULL [OK]
Common Mistakes:
Counting only first node
Off-by-one error in loop
Confusing data values with count
4. Identify the error in the following code that tries to get the length of a linked list:
int getLength(Node* head) {
int count = 0;
while (head->next != NULL) {
count++;
head = head->next;
}
return count;
}
medium
A. It causes a segmentation fault
B. It misses counting the last node
C. It counts nodes twice
D. It returns the data of the last node
Solution
Step 1: Analyze the loop condition
The loop runs while head->next != NULL, so it stops before the last node.
Step 2: Check counting logic
Since last node is not counted, the returned count is one less than actual length.
Final Answer:
It misses counting the last node -> Option B
Quick Check:
Loop condition excludes last node [OK]
Hint: Use while(head != NULL) to count all nodes [OK]
Common Mistakes:
Using head->next in loop condition
Not counting last node
Confusing node data with count
5. Given a linked list where each node contains an integer, how would you modify the getLength function to return the length only if all node values are positive? If any node has zero or negative value, return -1.
hard
A. Count nodes normally and ignore values
B. Return -1 if list is empty, else return length
C. Return length only if first node is positive
D. Traverse nodes, if value <= 0 return -1, else count nodes and return count
Solution
Step 1: Traverse each node checking value
While moving through the list, check if node's data is <= 0; if yes, return -1 immediately.
Step 2: Count nodes if all values positive
If all nodes have positive values, count them and return the count as length.
Final Answer:
Traverse nodes, if value <= 0 return -1, else count nodes and return count -> Option D
Quick Check:
Check values during count, return -1 if any non-positive [OK]
Hint: Check values while counting, stop early if non-positive [OK]
Common Mistakes:
Ignoring node values during count
Checking only first node value
Returning length even if negative values exist
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.