How do I merge-sort a linked list in c++?
<p>Hi everyone sorry my question is really vague that wasn't my intention. Anyway I have been trying to use mergesort to sort a linked list in c++. Unfortunately I am getting an access violation error ill try to show my error using a screenshot. Any way here is my code. Can you please help me figure out whats wrong?.</p>

<p><img src="https://i.stack.imgur.com/GyVhn.jpg" alt="this image  shows the error I am getting"></p>

<p>Here is my code:</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;iomanip&gt;    
#include &lt;fstream&gt;    
#include &lt;stdlib.h&gt;   
#include &lt;string&gt;

using namespace std;    

struct listNode
{   
    int  id, inv;
    listNode  *next;

    listNode(int tempId, int tempInv, listNode *nxt);
};

listNode::listNode(int tempId, int tempInv, listNode *nxt)
    : id(tempId), inv(tempInv), next(nxt)    
{       
    // all values initialized using initializer list
}   

void merge(listType &amp;root, nodePtr &amp;first, nodePtr &amp;mid, nodePtr &amp;last)
{    
    nodePtr index = root.first;   
    nodePtr index2 = mid-&gt;next;

    int num;

    nodePtr first2 = first;    
    nodePtr last2 = last;  
    nodePtr mid2 = mid;

    for (first2; first2-&gt;id &lt;= last-&gt;id; first2 = first2-&gt;next)
    {   
        if (index-&gt;id &gt; mid-&gt;id)
        {
            swap(first-&gt;id, index2-&gt;id);
            mid = mid-&gt;next;
            index2 = mid;

        }
        else if (index2-&gt;id &gt; last-&gt;id)
        {
            swap(first-&gt;id, index-&gt;id);
            root.first = root.first-&gt;next;
            index = root.first-&gt;next;
        }
        else if (first-&gt;id &lt; index-&gt;id)
        {
            swap(first-&gt;id, index-&gt;id);
            root.first = root.first-&gt;next;
            index = root.first-&gt;next;
        }
        else
        {
            swap(first-&gt;id, index2-&gt;id);
            mid = mid-&gt;next;
            index2 = mid;
        }
    }
}

void mergeSort(listType&amp; root, nodePtr &amp;first  , nodePtr &amp;last )
{
    if (root.last-&gt;id - root.first-&gt;id == 0)
    {
    }
    else if (root.first-&gt;next == root.last)
    {
        if (root.last-&gt;id &lt; root.first-&gt;id)
        {
            //int temp = root.first-&gt;id;
            swap(root.first-&gt;id, root.last-&gt;id);
            //swap();
        }
    }
    else
    {
        nodePtr first = root.first;
        int i = 0;
        for (root.first; root.first-&gt;id &lt; root.last-&gt;id; root.first = root.first-&gt;next)
        {
            i++;
        }
        int mid = i / 2;
        nodePtr merger = first;
        nodePtr merger2;
        root.first = first;
        for (int r = 0; r &lt; mid; r++)
        {
            root.first = root.first-&gt;next;
            merger = root.first;
        }

        mergeSort(root, root.first, merger);
        merger2 = root.first-&gt;next;
        mergeSort(root, merger2, root.last);
        merge(root, root.first, merger, root.last);
    }
}
</code></pre>

<p>Sorry this is being shown horribly this is my first time trying to post something like this i have tried doing this stuff before but it never works well. Sorry </p>