Merge sorting linked lists This works as expected and I want to get this code reviewed so that I can improve my coding ability.

I am concerned about readability and the quality of the code

#include&lt;stdio.h&gt;
#include&lt;stdlib.h&gt;


struct node
{   
    int element;
    struct node *next;
};



struct node* mergeList(struct node *list1, struct node *list2)
{
    struct node *root = NULL, *cur = NULL;

    if(list1 == NULL)
    {
        return list2;
    }

    if(list2 == NULL)
    {
        return list1;
    }   

    while( (list1 != NULL) &amp;&amp; (list2 != NULL) )
    {
        struct node *tmp = malloc(sizeof(struct node));
        if( list1-&gt;element  &lt; list2-&gt;element )
        {
            tmp-&gt;element = list1-&gt;element;
            tmp-&gt;next = NULL;
            list1 = list1-&gt;next;
        }
        else
        {
            tmp-&gt;element = list2-&gt;element;
            tmp-&gt;next = NULL;
            list2 = list2-&gt;next;

        }
        if(root == NULL)
        {
            root = tmp;
            cur = root;
        }
        else
        {
            cur-&gt;next = tmp;
            cur = tmp;
        }


    }


    while(list1 != NULL)
    {
        struct node *tmp = malloc(sizeof(struct node));
        tmp-&gt;element = list1-&gt;element;
        tmp-&gt;next = NULL;
        cur-&gt;next = tmp;
        cur = tmp;
        list1 = list1-&gt;next;
    }
    while(list2 != NULL)
    {
        struct node *tmp = malloc(sizeof(struct node));
        tmp-&gt;element = list2-&gt;element;
        tmp-&gt;next = NULL;
        cur-&gt;next = tmp;
        cur = tmp;
        list2 = list2-&gt;next;
    }


    return root;

}



void findMid(struct node *root, struct node **list1, struct node **list2)
{

    struct node *slow;
    struct node *fast;
    printf("\nIn Findmis\n");
    if( (root == NULL) || (root-&gt;next == NULL) )
    {
        *list1 = root;
        *list2 = NULL;
        return;
    }
    else
    {

        slow =  root;
        fast = root-&gt;next;
        while (fast != NULL) 
        {
            fast = fast-&gt;next;
            if(fast!=NULL)
            {
                slow = slow-&gt;next;
                fast = fast-&gt;next;
            }


        }


        *list1 = root;
        *list2 = slow-&gt;next;
        slow-&gt;next = NULL;

    }

}

void mergeSort(struct node **root)
{
    struct node *list1, *list2;
    struct node *head = *root;
    if( ( head  == NULL) || (head-&gt;next == NULL))
    {
        return;
    }


    findMid(head, &amp;list1, &amp;list2);

    if(list1 != NULL)
    printf("\nList1 = %d", list1-&gt;element);
    if(list2 != NULL)
    printf("\nList2 = %d", list2-&gt;element);
    mergeSort( &amp;list1 );
    mergeSort( &amp;list2 );

    *root = mergeList( list1, list2 ); 


}


void insertNodes(struct node **root, int key)
{
    struct node *tmp = malloc(sizeof(struct node));
    tmp-&gt;element = key;
    tmp-&gt;next = *root;
    *root = tmp;

}


void printList(struct node *root)
{
    while( root!= NULL)
    {
        printf("\n%d\n", root-&gt;element);
        root = root-&gt;next;
    }

}

int main(int argc, char *argv[])
{

    struct node *root1 = NULL;

    struct node *root2 = NULL;
    insertNodes( &amp;root1, 1);
    insertNodes( &amp;root1, 2);
    insertNodes( &amp;root1, 3);
    insertNodes( &amp;root1, 4);
    insertNodes( &amp;root1, 5);
    printList(root1);
    mergeSort( &amp;root1);
    printList(root1);
    return 0;

}
Preliminaries

Your compiler should have warned you that in main(), you never use list2.  You do compile with warnings turned on, I hope?

Usually, the first node of a linked list is called the head.  "Root" usually refers to the top of a tree.

mergeList() and findMid() are helper functions, which are designed to be used exclusively to support mergeSort().  It's a good habit to declare helper functions with the static modifier, so that they will be invisible to code in other .c files.

findMid()

The function could be better named, I think.  "Find" sounds like a passive action, so you wouldn't expect that it might modify its inputs (which it does at the end: slow-&gt;next = NULL).  I suggest calling it bisectList() instead.

Calling the function will always result in setting *list1 to root, so why bother making list1 an out-parameter?  Going a step further, if there's only one output (list2), you can just return it normally.

So, the function could become static struct node *bisectList(struct node *head).  Here's a more compact implementation.

/**
 * Returns a pointer to a node near the middle of the list,
 * after having truncated the original list before that point.
 */
static struct node *bisectList(struct node *head)
{
    /* The fast pointer moves twice as fast as the slow pointer. */
    /* The prev pointer points to the node preceding the slow pointer. */
    struct node *fast = head, *slow = head, *prev = NULL;

    while (fast != NULL &amp;&amp; fast-&gt;next != NULL)
    {
        fast = fast-&gt;next-&gt;next;
        prev = slow;
        slow = slow-&gt;next;
    }

    if (prev != NULL)
    {
        prev-&gt;next = NULL;
    }
    return slow;
}


mergeList()

The function signature looks fine.  There is altogether too much code, though.

One major problem, which @rolfl alluded to, is that you are duplicating every node that you encounter.  Rather, you should be able to merge the lists just by manipulating the next pointers of the existing nodes.

To start with, you can eliminate the two special cases before the loop.  (That meant, by the way, that your function not only leaked memory, it also had an inconsistent memory management policy: sometimes it would duplicate the list, and sometimes not.)

Once we have decided not to duplicate the nodes, the epilogue becomes trivial, and the last two loops can go away.

The code duplication within the main loop can be reduced by using a pointer to a pointer (which I call **min), referring to whichever list a head with the smaller value.  The if (root == NULL) special case can be removed by initializing a pointer to a dummy head node.

static struct node* mergeList(struct node *list1, struct node *list2)
{
    struct node dummy_head = { 0, NULL }, *tail = &amp;dummy_head;

    while ( (list1 != NULL) &amp;&amp; (list2 != NULL) )
    {   
        struct node **min = (list1-&gt;element &lt; list2-&gt;element) ? &amp;list1 : &amp;list2;
        struct node *next = (*min)-&gt;next;
        tail = tail-&gt;next = *min;
        *min = next;
    }
    tail-&gt;next = list1 ? list1 : list2;
    return dummy_head.next;
}


mergeSort()

It would be more natural to accept a struct node * and return a struct node *, I think.

You left in some printf() calls for debugging.  Why not take advantage of the existing printList() function for that?

Most of the improvements are just natural consequences of the changes suggested above.

struct node *mergeSort(struct node *head) 
{   
    struct node *list1 = head; 
    if ( (list1 == NULL) || (list1-&gt;next == NULL) )
    {   
        return list1;
    }   

    struct node *list2 = bisectList(list1);

    printf("\nList1 = "); printList(list1);
    printf("\nList2 = "); printList(list2);

    return mergeList( mergeSort(list1), mergeSort(list2) );
}


Conclusion

Your understanding of the mergesort algorithm is generally sound.  However, there were some massive memory leaks, and the implementation was verbose.  Try to look for opportunities to design more natural function interfaces and reduce special cases.