Efficiency of a linked list partitioning algorithm I have come across a fun puzzle, sorting a linked list, and I wanted to go for quicksort. However, I am not quite sure whether my partitioning algorithm is efficient enough. I also believe that my code is not clean.

Is it possible to remove some variables or make it more efficient?

#include &lt;iostream&gt;
#include &lt;cstdlib&gt;

using namespace std;


struct Node
{
    int data;
    Node *next;
};

void printList(Node *list)
{
    Node *temp = list;
    while(temp != NULL)
    {
        cout&lt;&lt;temp-&gt;data&lt;&lt;" ";
        temp = temp-&gt;next;
    }
}

int partition(Node *list, int start, int end)
{
    Node *startPtr = list, *endPtr = list, *head = list;

    for(int i = 0; i &lt; start; i++)
        if(startPtr == NULL || startPtr-&gt;next == NULL)
            return -1;
        startPtr = startPtr-&gt;next;

    for(int i = 0; i &lt; end; i++)
    {
        if(endPtr == NULL || endPtr-&gt;next == NULL)
            return -1;
        endPtr = endPtr-&gt;next;
    }


    Node *temp = NULL, *temp2 = startPtr;
    int pivot = endPtr-&gt;data;

    while(temp2-&gt;next != endPtr)
    {
        if(temp2-&gt;data &lt; pivot)
        {
            if(temp != NULL)
                temp = temp-&gt;next;
            else
                temp = startPtr;

            swap(temp-&gt;data, temp2-&gt;data);
        }
        temp2 = temp2-&gt;next;
    }
    temp = temp-&gt;next;
    swap(temp-&gt;data, endPtr-&gt;data);

    int index;
    Node *temp3 = list;

    for(index = 0; temp3-&gt;data != pivot; index++)
        temp3 = temp3-&gt;next;

    return index;
}

int main()
{
    Node *a = new Node;
    Node *b = new Node;
    Node *c = new Node;
    Node *d = new Node;
    Node *e = new Node;
    Node *f = new Node;

    a-&gt;next = b;
    a-&gt;data = 2;
    b-&gt;next = c;
    b-&gt;data = 6;
    c-&gt;next = d;
    c-&gt;data = 3;
    d-&gt;next = e;
    d-&gt;data = 1;
    e-&gt;next = f;
    e-&gt;data = 7;
    f-&gt;data = 5;

    cout&lt;&lt;partition(a, 0, 5)&lt;&lt;endl;

    return 0;
}
Yeah, you're missing brackets, but I think I get what you're trying to do. If I'm wrong, ignore this obviously.

printList can just use a for-loop:

void printList(Node *list)
{

 for(Node *temp = list; temp != NULL; temp = temp-&gt;next;)
 {
    cout&lt;&lt;temp-&gt;data&lt;&lt;" ";
 }
} 


Now for the partition function, you're always returning -1 if startPtr == NULL or endPtr == NULL. But you say that those point to what list points to (at the start of the function). In that case, just check if list is null first. Your function won't do anything if it is anyway:

if(list == NULL)
   return -1;


Now in those for-loops, all you have to do is check if startPtr-&gt;next and endPtr-&gt;next == NULL.

In your while loop, you have this code:

if(temp2-&gt;data &lt; pivot)
{
    if(temp != NULL)
        temp = temp-&gt;next;
    else
        temp = startPtr;

    swap(temp-&gt;data, temp2-&gt;data);
}


But what if, for example, temp == NULL and temp2 == startPtr? Assuming swap() does what it says it does, you're unnecessarily swapping values. It might be expensive, and it might not be. I dunno. The function might also do something weird if you try to swap the same exact values. (As a side note,  you're just passing the value of data. If swap doesn't take values by reference, you won't actually be doing anything to temp-&gt;data and temp2-&gt;data).

Also, in the declaration of your while loop, you're not checking for if temp2 == NULL.

while(temp2-&gt;next != endPtr) //This is will throw a Null Pointer Access Violation exception if temp2 == NULL


This could happen, because you aren't checking if temp2-&gt;next == NULL before you say temp2 = temp2-&gt;next. If it ever is, your loop will crash the program. I don't know if it's possible for temp2 to reach the end of the list, but if it is, you should just add a conditional.

After your while-loop, you have:

temp = temp-&gt;next;
swap(temp-&gt;data, endPtr-&gt;data);


That could potentially cause the same problems I described before.

Anyway, these are just suggestions. I didn't check thoroughly to see if there were variables you could remove, though.