📖 Scenario: You are working with a simple linked list that stores numbers. You want to find out how many nodes are in the list.
🎯 Goal: Build a program that creates a linked list, then counts and prints the number of nodes it contains.
📋 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 length of the linked list
Write a loop to traverse the linked list and count the nodes
Print the length of the linked list
💡 Why This Matters
🌍 Real World
Linked lists are used in many programs to store data that can grow or shrink. Counting nodes helps understand the size of the data.
💼 Career
Knowing how to work with linked lists and count their length is a basic skill for software developers working with data structures.
Progress0 / 4 steps
1
Create the linked list with 4 nodes
Define a struct called Node with an int data and a Node* next. Then create 4 nodes named node1, node2, node3, and node4 with values 10, 20, 30, and 40 respectively. Link them so that node1.next = &node2, node2.next = &node3, node3.next = &node4, and node4.next = NULL. Finally, create a pointer head that points to node1.
DSA C
Hint
Remember to define the struct before using it. Initialize each node's data and set next to NULL initially. Then link the nodes by setting the next pointers.
2
Create a variable to count the length
Inside main(), create an integer variable called length and set it to 0. This will keep track of how many nodes are in the linked list.
DSA C
Hint
Use int length = 0; to start counting from zero.
3
Traverse the linked list and count nodes
Write a while loop that uses a pointer current starting at head. Loop while current != NULL. Inside the loop, increase length by 1 and move current to current->next.
DSA C
Hint
Use a pointer current to move through the list. Increase length each time you visit a node.
4
Print the length of the linked list
Use printf to print the text "Length of linked list: " followed by the value of length and a newline.
DSA C
Hint
Use printf("Length of linked list: %d\n", length); to show the count.
Practice
(1/5)
1. What does the function to get the length of a linked list typically do?
easy
A. Reverses the linked list
B. Deletes all nodes in the linked list
C. Finds the maximum value stored in the linked list
D. Counts the number of nodes by traversing from head to end
Solution
Step 1: Understand the linked list traversal
The function starts at the head node and moves through each node until it reaches the end (null).
Step 2: Count each node visited
Each time the function moves to the next node, it increases a counter by one.
Final Answer:
Counts the number of nodes by traversing from head to end -> Option D
Quick Check:
Length = number of nodes visited [OK]
Hint: Count nodes while moving from head to null [OK]
Common Mistakes:
Confusing length with maximum value
Trying to reverse the list instead
Not traversing all nodes
2. Which of the following is the correct C syntax to declare a pointer to the head of a linked list node named head?
easy
A. struct Node* head;
B. struct Node &head;
C. Node head*;
D. struct Node head;
Solution
Step 1: Recall pointer declaration in C
To declare a pointer to a struct Node, use struct Node* variableName;.
Step 2: Match with options
struct Node* head; correctly declares head as a pointer to struct Node.
Final Answer:
struct Node* head; -> Option A
Quick Check:
Pointer syntax uses * after type [OK]
Hint: Pointer declaration uses * after type name [OK]
Common Mistakes:
Forgetting the * for pointer
Using & which is reference in C++ not C
Incorrect order of * and variable name
3. What will be the output of the following C code snippet?
struct Node {
int data;
struct Node* next;
};
int getLength(struct Node* head) {
int count = 0;
while (head != NULL) {
count++;
head = head->next;
}
return count;
}
int main() {
struct Node n3 = {30, NULL};
struct Node n2 = {20, &n3};
struct Node n1 = {10, &n2};
printf("%d", getLength(&n1));
return 0;
}
medium
A. 3
B. 2
C. 1
D. 0
Solution
Step 1: Analyze linked list nodes
There are three nodes: n1 -> n2 -> n3 -> NULL.
Step 2: Count nodes in getLength function
The function counts each node until it reaches NULL, so count = 3.
Final Answer:
3 -> Option A
Quick Check:
3 nodes counted from head to end [OK]
Hint: Count nodes until next is NULL [OK]
Common Mistakes:
Counting only head node
Off-by-one error in counting
Confusing pointer addresses with count
4. Find the error in this C function to get the length of a linked list:
int getLength(struct Node* head) {
int count = 0;
while (head->next != NULL) {
count++;
head = head->next;
}
return count;
}
medium
A. It causes a segmentation fault immediately
B. It misses counting the last node
C. It counts nodes twice
D. It returns the data of the last node
Solution
Step 1: Understand the loop condition
The loop runs while head->next != NULL, so it stops before the last node.
Step 2: Check counting logic
Because it stops early, the last node is not counted, so 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 handling empty list (NULL head)
Incrementing count after loop ends
5. You have a linked list where some nodes may have next pointers forming a cycle (loop). How would you modify the length function to avoid infinite counting?
hard
A. Stop counting after 10 nodes to avoid infinite loop
B. Count nodes normally without any change
C. Use two pointers moving at different speeds to detect a cycle before counting
D. Set all next pointers to NULL before counting
Solution
Step 1: Detect cycle using two pointers
Use Floyd's cycle detection: one pointer moves one step, another moves two steps to find a loop.
Step 2: If cycle detected, handle counting carefully
Once a cycle is found, count nodes up to the cycle start to avoid infinite counting.
Final Answer:
Use two pointers moving at different speeds to detect a cycle before counting -> Option C
Quick Check:
Cycle detection prevents infinite loops [OK]
Hint: Detect cycle first with slow and fast pointers [OK]
Common Mistakes:
Ignoring cycle causes infinite loop
Stopping count arbitrarily without detection
Modifying list structure destructively
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.