Moving an element in a linked list
There's more elegant ways, although not necessarily more efficient. The common idiom for fiddling with this kind of linked list in C is to add one more layer of indirection and fiddle with pointers-to-node-pointers, like this:

void insert(node_t **head, int val)
{
    node_t *new_node = malloc(sizeof(node_t));
    new_node-&gt;val = val;
    new_node-&gt;next = NULL;

    node_t **ptmp = head;
    while (*ptmp != NULL) {
        ptmp = &amp;(*ptmp)-&gt;next;
    }
    *ptmp = new_node;
}


Given that example, I think you can figure out how to rewrite movetoend in just a few additional lines: much shorter than the page of code you have now.

However, whether this will be equally fast depends on your compiler and optimization levels. When in doubt, check the assembly code! If you see a lot of additional memory loads in the inner loop, that's a bad sign.

In C++, but I think not in C yet, you could eliminate even the appearance of extra memory loads by doing

void insert(node_t **head, int val)
{
    node_t *new_node = malloc(sizeof(node_t));
    new_node-&gt;val = val;
    new_node-&gt;next = NULL;

    node_t **ptmp = head;
    while (node_t tmp = *ptmp) {
        ptmp = &amp;tmp-&gt;next;
    }
    *ptmp = new_node;
}