Simple yet Effective Solution O(n) time complexity O(1)space


#1

I have a simple yet effective approach
//Count the number so zeros and ones
//Then place them on the list while traversing the same.

ListNode* Solution::solve(ListNode* A) {
int zero=0, one=0;
ListNode* temp = A;
while(temp!=NULL){
    if(temp->val==0)    zero++;
    else                one++;
    temp=temp->next;
}

temp=A;
while(zero--){
    temp->val=0;temp=temp->next;
}
while(one--){
    temp->val=1;temp=temp->next;
}
return A;

}