Most elegant C++ code with stack


#1
ListNode* Solution::solve(ListNode* A) {
    ListNode* iter; int i;
    stack<int> even;
    for(i=0,iter=A;iter!=NULL;iter=iter->next,i++){
        if(i%2!=0){
            even.push(iter->val);
        }
    }
    for(i=0,iter=A;iter!=NULL;iter=iter->next,i++){
        if(i%2!=0){
            iter->val=even.top();
            even.pop();
        }
    }
    return A;
}