data-structures · beginner · ~15 min
Walk a linked list using a pointer that advances along `->next`.
Count the nodes in a singly linked list by walking it from head to tail.
The grader supplies this node type:
typedef struct node { int data; struct node *next; } node_t;
Implement int list_length(const node_t *head) returning the number of nodes in the list. No main — the grader calls it.
head: a pointer to the first node, or NULL for an empty list. The list is properly NULL-terminated.
The node count as an int (0 for an empty list).
list_length(NULL) -> 0
list_length(1) -> 1 (a single node)
list_length(1->2->3) -> 3
head == NULL returns 0.head: pointer to the first node, or NULL for an empty list.
The number of nodes as an int (0 for NULL).
int list_length(const node_t *head) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.