Answer
See the explanation
Work Step by Step
Here's a Python function to count the nodes of a linked list:
```python
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def count_nodes(head):
count = 0
current = head
while current is not None:
count += 1
current = current.next
return count
```
This function iterates through the linked list starting from the head node and increments a counter for each node encountered until it reaches the end of the list. Then, it returns the count of nodes.