O(N) time and O(1) space solution C++ using 2 pointers


#1

Using two pointers:
t1 - always points to the last position where 0 should be placed if found while traversing the list
t2 - traverses the linked list.

 ListNode* Solution::solve(ListNode* A) {
    if(A==NULL)return A;
    ListNode *t1 = A,*t2 = A->next;

    while(t2){
        if(t1->val==1 && t2->val==0){
            swap(t1->val,t2->val);
        }
        if(t1->val==0)t1=t1->next;
        t2=t2->next;
    }
    return A;
}