Sorting a linked list by turning it into a binary search tree


A doubly-linked list node has two pointers, previous and next, and a binary tree node also has two pointers, left and right

This means that one way of sorting a linked list is to turn it into a binary tree and then back into a list again.

Turning the list into a binary tree

Turning the list into a tree is just a question of iterating over the list and performing the normal binary tree insertion algorithm. We need to remember to save the current node’s next pointer before we insert it, as it will not be available afterwards.

typedef int(*cmpfn)(const void*, const void*);

listnode * linkedlist_to_tree(linkedlist * list, cmpfn compare)
    listnode * root = list->head;
    if (root) {
        listnode * node = root->next;
        root->previous = NULL;
        root->next = NULL;
        while (node != NULL) {
            listnode * next = node->next;
            listnode * current = root, * previous = NULL;
            int result;
            while (current != NULL) {
                previous = current;
                result = compare(current->data, node->data);
                if (result > 0) {
                    current = current->previous;
                else {
                    current = current->next;
            if (result > 0) {
                previous->previous = node;
            else {
                previous->next = node;
            node->previous = NULL;
            node->next = NULL;
            node = next;
    return root;

Turning the binary tree back into a list

This involves an inorder traversal of the tree, which is most easily implemented using recursion. We need to look out for the first and last elements; they need to be treated specially as they are the head and tail of the list.

The first element in a binary tree is the one that we encounter the first time we can go left no further. We find the last element by keeping track of the number of elements encountered so far, and comparing it to the number in the list.

void linkedlist_from_tree(linkedlist * list, listnode * root,
        listnode ** previous, unsigned int * count)
    if (root) {
        listnode * left = root->previous;
        listnode * right = root->next;
        linkedlist_from_tree(list, left, previous, count);
        if (root->previous == NULL && list->head == NULL) {
            /* We're at the first element */
            list->head = root;
            list->head->previous = NULL;
        else {
            (*previous)->next = root;
            root->previous = *previous;
        if (*count == linkedlist_get_count(list) - 1) {
            /* We're at the last element */
            list->tail = root;
            list->tail->next = NULL;
        *previous = root;
        linkedlist_from_tree(list, right, previous, count);

The sort function

Our linked list sort function now just needs to combine the two operations:

void linkedlist_sort(linkedlist * list, cmpfn compare)
    unsigned int count = 0;
    listnode * previous;
    listnode * tree = linkedlist_to_tree(list, compare);
    list->head = NULL;
    list->tail = NULL;
    linkedlist_from_tree(list, tree, &previous, &count);

Example program

int main(void)
    char * elements[] = {"D", "B", "D", "F", "A", "C", "E"};
    const unsigned int n = sizeof(elements) / sizeof(const char *);
    linkedlist * list;
    unsigned int i;

    list = linkedlist_create();
    for (i = 0; i < n; i++) {
        linkedlist_add_tail(list, elements[i]);
    linkedlist_sort(list, (cmpfn)strcmp);
    linkedlist_for_each(list, (forfn)puts);

    return 0;

Leave a Reply

Your email address will not be published. Required fields are marked *