📖 Scenario: Imagine you have a chain of boxes connected one after another. Each box holds a number and points to the next box. This is like a linked list.You want to find out how many boxes are in the chain.
🎯 Goal: Build a simple linked list with three boxes (nodes) and write code to count how many boxes are in the chain.
📋 What You'll Learn
Create a linked list with exactly three nodes containing values 10, 20, and 30
Create a variable to count the number of nodes
Use a loop to go through each node in the linked list
Print the total count of nodes
💡 Why This Matters
🌍 Real World
Linked lists are used in many software systems to store data where the size can change dynamically, like playlists, undo history, or navigation paths.
💼 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 coding interviews.
Progress0 / 4 steps
1
Create a linked list with three nodes
Create a class called Node with an __init__ method that takes data and sets self.data and self.next to None. Then create three nodes called node1, node2, and node3 with data values 10, 20, and 30 respectively. Link node1.next to node2 and node2.next to node3. Finally, create a variable called head that points to node1.
DSA Python
Hint
Think of each node as a box holding a number and a pointer to the next box.
2
Create a counter variable
Create a variable called count and set it to 0. This will keep track of how many nodes we have counted.
DSA Python
Hint
This variable will increase as we visit each node.
3
Traverse the linked list and count nodes
Use a current variable to start at head. Use a while loop that runs as long as current is not None. Inside the loop, increase count by 1 and move current to current.next.
DSA Python
Hint
Think of walking through the chain, one box at a time, counting each box.
4
Print the length of the linked list
Write a print statement to display the value of count.
DSA Python
Hint
The output should be the number of nodes in the linked list.
Practice
(1/5)
1. What does the length of a linked list represent?
easy
A. The memory size of the linked list
B. The value of the first node
C. The number of nodes in the linked list
D. The value of the last node
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 C
Quick Check:
Length counts nodes = number of nodes [OK]
Hint: Length = count of nodes from head to end [OK]
Common Mistakes:
Confusing length with node values
Thinking length is memory size
Counting only first or last node
2. Which of the following Python code snippets correctly initializes a variable to count the length of a linked list?
easy
A. length = None
B. length = 0
C. length = head
D. length = []
Solution
Step 1: Identify counting variable type
Length should be a number starting at zero before counting nodes.
Step 2: Check each option
length = 0 sets length to 0, ready to increment. Others assign wrong types.
Final Answer:
length = 0 -> Option B
Quick Check:
Initialize count as 0 before loop [OK]
Hint: Start counting length from zero [OK]
Common Mistakes:
Initializing length as None or list
Assigning length to head node
Using wrong data types for count
3. What will be the output of the following code?
class Node:
def __init__(self, data):
self.data = data
self.next = None
def get_length(head):
count = 0
current = head
while current:
count += 1
current = current.next
return count
# Creating linked list: 10 -> 20 -> 30 -> None
head = Node(10)
head.next = Node(20)
head.next.next = Node(30)
print(get_length(head))
medium
A. 3
B. 2
C. 0
D. None
Solution
Step 1: Trace linked list nodes
List has nodes with values 10, 20, and 30, total 3 nodes.
Step 2: Follow get_length function
Function counts nodes by moving current pointer until None, increments count each step.
Final Answer:
3 -> Option A
Quick Check:
Count nodes 10,20,30 = 3 [OK]
Hint: Count nodes by traversing until None [OK]
Common Mistakes:
Stopping count early
Not incrementing count inside loop
Returning None instead of count
4. Find the error in this code to get the length of a linked list:
def get_length(head):
count = 0
while head.next:
count += 1
head = head.next
return count
medium
A. It misses counting the last node
B. It causes infinite loop
C. It returns None instead of count
D. It counts nodes twice
Solution
Step 1: Analyze loop condition
Loop runs while head.next exists, so it stops before last node.
Step 2: Check counting logic
Last node is not counted because loop ends early, count misses one node.
Final Answer:
It misses counting the last node -> Option A
Quick Check:
Loop condition excludes last node [OK]
Hint: Use 'while current' not 'while current.next' to count all nodes [OK]
Common Mistakes:
Using head.next in loop condition
Not counting last node
Assuming loop covers all nodes
5. You have a linked list where each node contains a number. Write a function to find the length of the linked list only if all node values are positive. If any node has zero or negative value, return 0 immediately. What is the correct approach?
hard
A. Check only first node value, then count all nodes
B. Count nodes first, then check values in a second pass
C. Return length without checking values
D. Traverse nodes, if value <= 0 return 0, else count nodes till end
Solution
Step 1: Traverse and check values simultaneously
Move through each node, check if value is positive.
Step 2: Return 0 immediately if value is zero or negative, else keep counting
If any node fails condition, stop and return 0; otherwise, return total count.
Final Answer:
Traverse nodes, if value <= 0 return 0, else count nodes till end -> Option D
Quick Check:
Check values while counting, stop early if invalid [OK]
Hint: Check values during traversal, stop early if invalid [OK]
Common Mistakes:
Counting first, then checking values
Ignoring node values
Checking only first node value
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.