# My quick sort solution

1. select the pivot, then split into two lists.
2. sort two lists recursivly.
3. concat two lists into one.

But need some optimization which is shown in comment, or it will be TLE-.-

``````class Solution {
public:

}

void AppendNode(ListNode **last, ListNode **head, ListNode *node)
{
if (*last) (*last)->next = node;
else
{
*last = node;
}
}

{

ListNode *lt = NULL, *rt = NULL;
ListNode *tmp = NULL;

while (*cur)
{
if ((*cur)->val < pivot->val)
{
lt = *cur;
}
else
{
rt = *cur;
}

tmp = *cur;
*cur = (*cur)->next;
tmp->next = NULL;
}

//sorting two sides
//optimize this part
while (*ckp && (*ckp)->val == rhead->val) ckp = &(*ckp)->next;
QuickSort(ckp, &rt); //skip pivot

//concats two lists
{
}
//now pivot in middle

//set last node (in case of changes)
if (end) *end = rt;
}
};``````

• The optimization is very important. I failed a lot without that. Thank you very much for sharing.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.