data-structures · intermediate · ~15 min

Insert at end

Mutating a linked list while preserving its head.

Challenge

Given the same node_t definition, implement node_t *insert(node_t *head, int value) that appends a new node and returns the (possibly new) head. If head is NULL the new node becomes head.

Starter code

#include <stdlib.h>

node_t *insert(node_t *head, int value) {
    /* TODO */
    return head;
}

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