data-structures · beginner · ~15 min

Length of a linked list

Traverse a singly-linked list.

Challenge

With typedef struct node { int v; struct node *next; } node_t;, implement int list_length(node_t *head) returning the number of nodes (0 for NULL).

Starter code

typedef struct node { int v; struct node *next; } node_t;

int list_length(node_t *head) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.