You're not splitting a 2-element list into two 1-element lists in
mergeSort()
. Run your code with just two values and print out the lists at
head
and
nhead
after splitting the lists and before recursively sorting them. Your
printList()
function will be handy for that.
The effect of that is infinite recursion. Your "segmentation error" is the result of a stack overflow. All sorts of other errors can be reported as a result of crashing the stack.
It looks like changing the initialization of the fast/slow pointers in
findMid()
will fix that:
Node *slow_ptr = head;
Node *fast_ptr = slow_ptr->next;
You might also want to add a test for a null head pointer before doing that, but it doesn't look like you'll ever call that function when the list is empty.
The idea is that you use the "mid pointer" not as the head of the right-side list, but rather as the tail of the left-size list. That fix effectively starts the fast pointer half a cycle sooner so that the slow pointer lags behind it's old position by one list entry. You'll always get two nonempty lists when there are at least two elements in the starting list.