Very simple and self-explanatory solution. O(n) time, O(1) space


#1

ListNode* Solution::solve(ListNode* A) {
if(A==NULL || A->next==NULL) return A;
ListNode* currone=A;
while(currone!=NULL && currone->val==0){
currone=currone->next;
}
if(currone==NULL) return A;
ListNode* temp=currone;
while(temp!=NULL && currone->next!= NULL){
if(temp->val==1) temp=temp->next;
else if(temp->val==0){
currone->val=0;
temp->val=1;
currone=currone->next;
temp=temp->next;
}
}
return A;
}