zoukankan      html  css  js  c++  java
  • 18.2 删除链表中重复的结点

    /*
    struct ListNode {
        int val;
        struct ListNode *next;
        ListNode(int x) :
            val(x), next(NULL) {
        }
    };
    */
    class Solution {
    public:
        ListNode* deleteDuplication(ListNode* pHead) {
            if(pHead == nullptr || pHead->next == nullptr) return pHead;
            auto fake = new ListNode(0);
            auto left = pHead;
            auto right = pHead->next;
            auto pre = fake;
            bool duplicated = false;
            while(right){
                if(left->val == right->val){
                    duplicated = true;
                }else{
                    if(!duplicated){
                        pre->next = left;
                        pre = pre->next;
                    }else{
                        duplicated = false;
                    }
                }
                left = right;
                right = right->next;
            }
            if(!duplicated){
                pre->next = left;
                pre = pre->next;
            }
            pre->next = nullptr;
            return fake->next;
        }
    };
    
  • 相关阅读:
    LSTM
    Realsense D435i
    ubuntu18 realsenseD435i
    net
    test
    LSTM Accuracy
    boost x64 lib
    E0443类模板 "std::unordered_set" 的参数太多
    PropertySheet
    freetype 编译
  • 原文地址:https://www.cnblogs.com/N3ptuner/p/14586872.html
Copyright © 2011-2022 走看看