It says 'didn't complete in the allocated time limit'. I have used only one while loop


ListNode* Solution::solve(ListNode* A) {
ListNode *B;
ListNode *C;
ListNode *D;
ListNode *E;
ListNode *next;
C = A;

while(C != NULL){
    if(C->val == 1) {
        next = C->next;
        C->next = B;
        B = C;
        C = next;
        next = C->next;
        E = D;
        D = C;
        D -> next = B;
        E -> next = D;
        D = E;


Your condition is C!=null, and none of your increments MOVE c


First is You are not moving your c fwd and is it necessary to change the whole node i.e you may simply swap the value:)


In else, you are not moving C. So try moving C in else case.