C++ solution with O(n) complexity


#1

ListNode* Solution::solve(ListNode* A) {
if(A==NULL)
return A;
int count[2]={0};
ListNode *t=A;
while(t!=NULL){
count[t->val]++;
t=t->next;
}
int i=0;
t=A;
while(t!=NULL){
if(count[i]==0){
i++;

    }
    else{
        t->val=i;
        count[i]--;
        t=t->next;
    }
}
t=A;
return t;

}