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;
    }
  • 相关阅读:
    使用GitHub+hexo搭建个人独立博客
    HDU 3038
    POJ 1417
    HDU 1213
    ZOJ 3781
    ZOJ 3780
    ZOJ 3777
    HDU 3045
    HDU 3480
    POJ 1180
  • 原文地址:https://www.cnblogs.com/wuOverflow/p/4728604.html
Copyright © 2011-2022 走看看