Singly linked list exercise in C Being completely new to the world of C, I started off with the excercise of building a singly linked list of integers:

/*
  Single linked list test
*/
#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;


typedef struct ListElement {
    int         value;
    struct ListElement  *next;
} ListElement;


void printList(ListElement *root) {
    ListElement *current;
    current = root;

    while (current-&gt;next) {
        printf("Value: %i\n", current-&gt;value);
        current = current-&gt;next;
    }

    printf("Value: %i\n", current-&gt;value);
}


int freeLast(ListElement *root) {
    int retval;
    retval = 1;
    ListElement *previous;
    ListElement *current;
    current = root;

    while (current-&gt;next) {
        previous = current;
        current = current-&gt;next;
    }

    if (current == root) {
        retval = 0;
    }

    previous-&gt;next = NULL;
    printf("Freeing %i\n", current-&gt;value);
    free(current);
    return retval;
}


void freeList(ListElement *root) {
    while(freeLast(root));
}


ListElement * createList(int size) {
    ListElement *root;
    ListElement *current;
    root = malloc(sizeof(ListElement));

    if (root != NULL) {
        root-&gt;value = 0;
        current = root;
        int i;

        for (i = 1; i &lt; size; i++) {
            current-&gt;next = malloc(sizeof(ListElement));

            if (current-&gt;next != NULL) {
                current-&gt;next-&gt;value = i;
                current = current-&gt;next;
            } else {
                freeList(root);
                return NULL;
            }
        }

        return root;
    } else {
        return NULL;
    }
}


int main() {
    ListElement *root = createList(100);
    printList(root);
    freeList(root);
}


While the code compiles and runs as intended I'd like to have some feedback on the coding style, program structure and possibly existing quirks with the memory handling in C, which I hope I got right.
This is pretty good. Minor points:

You can return just root in createList:

ListElement * createList(int size) {
    ListElement *root;
    ListElement *current;
    root = malloc(sizeof(ListElement));

    if (root != NULL) {
        root-&gt;value = 0;
        current = root;
        int i;

        for (i = 1; i &lt; size; i++) {
            current-&gt;next = malloc(sizeof(ListElement));

            if (current-&gt;next != NULL) {
                current-&gt;next-&gt;value = i;
                current = current-&gt;next;
            } else {
                freeList(root);
                return NULL;
            }
        }
    }

    return root;
}


And your freeList is O(n^2) complex, because you are removing from the end. Removing from the beginning would make it O(n).