# QuickSort be accepted. And Runtime Error may be your program in the loop.

• /**

• struct ListNode {

• ``````int val;
``````
• ``````ListNode *next;
``````
• ``````ListNode(int x) : val(x), next(NULL) {}
``````
• };
/
class Solution {
public:
ListNode

`````` if(head == NULL) {
}

if(p == NULL){
}

ListNode *Btemp = NULL;
ListNode *bt=NULL;

ListNode *Atemp = NULL;
ListNode *at=NULL;

while(p){
if(Btemp){
bt->next = p;
bt = bt->next;
}
else{
Btemp = p;
bt = Btemp;
}
if(Atemp){
at->next = p;
at = at->next;
}
else{
Atemp = p;
at = Atemp;
}
}else{
h->next = p;
h = h->next;
}
p= p->next;
}

if(bt){
bt->next = NULL;
}
if(at){
at->next = NULL;
}

bt = sortList(Btemp);
at = sortList(Atemp);

h->next = at;
if(bt){
ListNode *bl = bt;
while(bl->next){
bl = bl->next;
}
}else{
}

return bt;
``````

}
};

• It's quick sort, but doesn't ensure nlgn complexity since you always use head as pivot.

Another post mentioned the test cases used for this question with similar strategy as yours.

It's a good idea to move nodes smaller or bigger to 2 temporary lists, it won't be too complicated even using random pivot. I didn't thought this but just moved nodes back and forth in the original list with random pivot, which made my codes more complicated.

• Confused, what does "And Runtime Error may be your program in the loop." mean...?

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