zoukankan      html  css  js  c++  java
  • 删除有序链表中重复的项

    一开始还看错了我去,后来发现后改为:

    ListNode* deleteDuplicates(ListNode* head) {
        if (head == nullptr || head->next == nullptr){
            return head;
        }
        
        int  currentMin = INT16_MIN;
        int& headVal    = head->val;
        function<ListNode*(ListNode*&)> check;
        check = [&](ListNode*& node)->ListNode*
        {
            if (node == nullptr) {
                return nullptr;
            }
            
            node->next = check(node->next);
            int& currentVal = node->val;
            if (currentVal == currentMin || currentVal == headVal){
                return node->next;
            }
            currentMin = currentVal;
            return node;
        };
        head->next = check(head->next);
        return head;
    }

    写完后不禁叹道:“唉,还是递归合我胃口!”

    结果在看了其他人的答案后,觉得我这不是傻逼吗? 我离更简单的答案居然这么近都没想到:

    ListNode* deleteDuplicates(ListNode* head) {
        if (head == nullptr || head->next == nullptr){
            return head;
        }
        head->next = deleteDuplicates(head->next);
        return head->val == head->next->val? head->next : head;
    }
  • 相关阅读:
    spark foreachPartition
    spark mapPartition
    spark union intersection subtract
    spark join 类算子
    spark action 算子
    卷展栏模板
    锁定/解锁
    3D一些提示信息
    文件对话框多选
    吸取属性
  • 原文地址:https://www.cnblogs.com/wuOverflow/p/4728604.html
Copyright © 2011-2022 走看看