Very easy to understand C++ soln (using stacks)


#1
ListNode* Solution::solve(ListNode* A) {
    stack<int> evenNos;
    ListNode *head = new ListNode(0);
    head->next = A;
    ListNode *curr = A;
    int count = 0;
    
    while(curr != NULL){
        count++;
        if(count % 2 == 0) evenNos.push(curr->val);
        curr = curr->next;
    }
    
    curr = A;
    while(curr != NULL){
        if(curr->val % 2 == 0){
            int res = evenNos.top();
            evenNos.pop();
            curr->val = res;
        }
        curr = curr->next;
    }
    
    return head->next;
}