data-structures · beginner · ~15 min

Sum of a linked list

Accumulate while traversing a list.

Challenge

With the same node_t, implement long list_sum(node_t *head) returning the sum of all values (0 for NULL).

Starter code

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

long list_sum(node_t *head) {
    /* TODO */
    return 0;
}

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