data-structures · beginner · ~15 min

Linked list length

Walk a linked list using a pointer that advances along `->next`.

Challenge

Given

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

implement int list_length(const node_t *head) returning the number of nodes (0 for NULL).

Starter code

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.