class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param head ListNode类 
     * @return ListNode类
     */
    ListNode* deleteNodes(ListNode* head) {
        // 用3个指针来完成删除和遍历:pre   cur     nex
        //                          |      |       |
        //              1     3     2      4       5
        ListNode* pre = head;
        ListNode* cur = head->next;
        ListNode* nex = cur->next;
        while(nex){
            // 按照题意,在遍历的过程中删除比前后大的节点
            if(cur->val > pre->val && cur->val > nex->val){
                pre->next = nex;  //pre直接指向nex
                cur = nullptr;    //free掉cur的当前值
                cur = nex;        // 重新给cur赋值,使得cur取代nex的位置
                nex = nex->next;  // nex往后走
            }else {// 移动每个指针来完成遍历
                pre = cur;
                cur = nex;
                nex = nex->next;
            }
        }
        return head;
    }
};