Problem p02265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <iostream>
#include <string>
// #include <list>
// #include <vector>

template <typename T> class TList {
  struct TNode {
    TNode() {
      Value = NULL;
      Next = Prev = NULL;
    }
    TNode(const T &AValue) {
      Value = new T(AValue);
      Next = Prev = NULL;
    }
    T *Value;
    TNode *Next;
    TNode *Prev;
  };

public:
  class TIterator : public std::iterator<std::forward_iterator_tag, T> {
    friend TList;
    TNode *FNode;
    TIterator(TNode *Node) { FNode = Node; }

  public:
    TIterator operator++() {
      FNode = FNode->Next;
      return TIterator(FNode);
    }
    TIterator operator++(int) {
      FNode = FNode->Next;
      return TIterator(FNode->Prev);
    }
    T &operator*() { return *(FNode->Value); }
    const T &operator*() const { return *(FNode->Value); }
    bool operator==(const TIterator &Iterator) const {
      return FNode == Iterator.FNode;
    }
    bool operator!=(const TIterator &Iterator) const {
      return FNode != Iterator.FNode;
    }
  };

  class TConstIterator : public std::iterator<std::forward_iterator_tag, T> {
    friend TList;
    const TNode *FNode;
    TConstIterator(const TNode *Node) { FNode = Node; }

  public:
    TConstIterator operator++() {
      FNode = FNode->Next;
      return TConstIterator(FNode);
    }
    TConstIterator operator++(int) {
      FNode = FNode->Next;
      return TConstIterator(FNode->Prev);
    }
    const T &operator*() const { return *(FNode->Value); }
    bool operator==(const TConstIterator &Iterator) const {
      return FNode == Iterator.FNode;
    }
    bool operator!=(const TConstIterator &Iterator) const {
      return FNode != Iterator.FNode;
    }
  };
  void Delete(TNode *Node) {
    delete Node->Value;
    delete Node;
  }

public:
  TList() { FBegin = &FEnd; }
  void push_front(int Value) {
    TNode *Node = new TNode(Value);
    Node->Prev = NULL;
    Node->Next = FBegin;
    FBegin->Prev = Node;
    FBegin = Node;
  }
  void erase(TIterator &It) {
    assert(It.FNode != &FEnd);
    TNode *Node = It.FNode;
    if (Node->Prev) {
      Node->Prev->Next = Node->Next;
    } else {
      FBegin = Node->Next;
    }
    if (Node->Next)
      Node->Next->Prev = Node->Prev;
    Delete(Node);
  }
  void pop_front() {
    assert(FBegin != &FEnd);
    TNode *Next = FBegin->Next;
    assert(Next); // ?°?????????¨???FEnd?????????
    Next->Prev = NULL;
    Delete(FBegin);
    FBegin = Next;
  }
  void pop_back() {
    TNode *Last = FEnd.Prev;
    assert(Last);
    FEnd.Prev = Last->Prev;
    if (FEnd.Prev) {
      FEnd.Prev->Next = &FEnd;
    } else {
      FBegin = &FEnd;
    }
    Delete(Last);
  }
  TIterator begin() { return TIterator(FBegin); }
  TConstIterator begin() const { return TConstIterator(FBegin); }
  TIterator end() { return TIterator(&FEnd); }
  TConstIterator end() const { return TConstIterator(&FEnd); }

private:
  TNode *FBegin;
  TNode FEnd;
};

class TDllist {
  // std::list<int> FList;
  TList<int> FList;
  // std::vector<int> FList;
public:
  void Insert(int X) {
    FList.push_front(X);
    // FList.insert(FList.begin(), X);
  }
  void Delete(int X) {
    TList<int>::TIterator It = std::find(FList.begin(), FList.end(), X);
    if (It != FList.end()) {
      FList.erase(It);
    }
    // for(auto It = FList.begin();
    //     It != FList.end();
    //     ++It)
    // {
    //     if(*It == X){
    //         FList.erase(It);
    //         return;
    //     }
    // }
  }
  void DeleteFirst() {
    FList.pop_front();
    // FList.erase(FList.begin());
  }
  void DeleteLast() { FList.pop_back(); }
  void Show() const {
    TList<int>::TConstIterator It = FList.begin();
    std::cout << *It;
    for (++It; It != FList.end(); ++It) {
      std::cout << " " << *It;
    }
    std::cout << std::endl;
  }
};

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  TDllist List;
  int n, X;
  std::cin >> n;
  std::string Command;
  for (int i = 0; i < n; ++i) {
    // std::string Command;
    std::cin >> Command;
    if (Command == "insert") {
      // int X;
      std::cin >> X;
      List.Insert(X);
    } else if (Command == "delete") {
      // int X;
      std::cin >> X;
      List.Delete(X);
    } else if (Command == "deleteFirst") {
      List.DeleteFirst();
    } else if (Command == "deleteLast") {
      List.DeleteLast();
    } else {
      assert(false);
    }
  }
  List.Show();
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <iostream>
#include <string>
// #include <list>
// #include <vector>

template <typename T> class TList {
  struct TNode {
    TNode() {
      Value = NULL;
      Next = Prev = NULL;
    }
    TNode(const T &AValue) {
      Value = new T(AValue);
      Next = Prev = NULL;
    }
    T *Value;
    TNode *Next;
    TNode *Prev;
  };

public:
  class TIterator : public std::iterator<std::forward_iterator_tag, T> {
    friend TList;
    TNode *FNode;
    TIterator(TNode *Node) { FNode = Node; }

  public:
    TIterator operator++() {
      FNode = FNode->Next;
      return TIterator(FNode);
    }
    TIterator operator++(int) {
      FNode = FNode->Next;
      return TIterator(FNode->Prev);
    }
    T &operator*() { return *(FNode->Value); }
    const T &operator*() const { return *(FNode->Value); }
    bool operator==(const TIterator &Iterator) const {
      return FNode == Iterator.FNode;
    }
    bool operator!=(const TIterator &Iterator) const {
      return FNode != Iterator.FNode;
    }
  };

  class TConstIterator : public std::iterator<std::forward_iterator_tag, T> {
    friend TList;
    const TNode *FNode;
    TConstIterator(const TNode *Node) { FNode = Node; }

  public:
    TConstIterator operator++() {
      FNode = FNode->Next;
      return TConstIterator(FNode);
    }
    TConstIterator operator++(int) {
      FNode = FNode->Next;
      return TConstIterator(FNode->Prev);
    }
    const T &operator*() const { return *(FNode->Value); }
    bool operator==(const TConstIterator &Iterator) const {
      return FNode == Iterator.FNode;
    }
    bool operator!=(const TConstIterator &Iterator) const {
      return FNode != Iterator.FNode;
    }
  };
  void Delete(TNode *Node) {
    delete Node->Value;
    delete Node;
  }

public:
  TList() { FBegin = &FEnd; }
  void push_front(int Value) {
    TNode *Node = new TNode(Value);
    Node->Prev = NULL;
    Node->Next = FBegin;
    FBegin->Prev = Node;
    FBegin = Node;
  }
  void erase(TIterator &It) {
    assert(It.FNode != &FEnd);
    TNode *Node = It.FNode;
    if (Node->Prev) {
      Node->Prev->Next = Node->Next;
    } else {
      FBegin = Node->Next;
    }
    if (Node->Next)
      Node->Next->Prev = Node->Prev;
    Delete(Node);
  }
  void pop_front() {
    assert(FBegin != &FEnd);
    TNode *Next = FBegin->Next;
    assert(Next); // ?°?????????¨???FEnd?????????
    Next->Prev = NULL;
    Delete(FBegin);
    FBegin = Next;
  }
  void pop_back() {
    TNode *Last = FEnd.Prev;
    assert(Last);
    FEnd.Prev = Last->Prev;
    if (FEnd.Prev)
      FEnd.Prev->Next = &FEnd;
    delete Last;
  }
  TIterator begin() { return TIterator(FBegin); }
  TConstIterator begin() const { return TConstIterator(FBegin); }
  TIterator end() { return TIterator(&FEnd); }
  TConstIterator end() const { return TConstIterator(&FEnd); }

private:
  TNode *FBegin;
  TNode FEnd;
};

class TDllist {
  // std::list<int> FList;
  TList<int> FList;
  // std::vector<int> FList;
public:
  void Insert(int X) {
    FList.push_front(X);
    // FList.insert(FList.begin(), X);
  }
  void Delete(int X) {
    TList<int>::TIterator It = std::find(FList.begin(), FList.end(), X);
    if (It != FList.end()) {
      FList.erase(It);
    }
    // for(auto It = FList.begin();
    //     It != FList.end();
    //     ++It)
    // {
    //     if(*It == X){
    //         FList.erase(It);
    //         return;
    //     }
    // }
  }
  void DeleteFirst() {
    FList.pop_front();
    // FList.erase(FList.begin());
  }
  void DeleteLast() { FList.pop_back(); }
  void Show() const {
    TList<int>::TConstIterator It = FList.begin();
    std::cout << *It;
    for (++It; It != FList.end(); ++It) {
      std::cout << " " << *It;
    }
    std::cout << std::endl;
  }
};

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  TDllist List;
  int n, X;
  std::cin >> n;
  std::string Command;
  for (int i = 0; i < n; ++i) {
    // std::string Command;
    std::cin >> Command;
    if (Command == "insert") {
      // int X;
      std::cin >> X;
      List.Insert(X);
    } else if (Command == "delete") {
      // int X;
      std::cin >> X;
      List.Delete(X);
    } else if (Command == "deleteFirst") {
      List.DeleteFirst();
    } else if (Command == "deleteLast") {
      List.DeleteLast();
    } else {
      assert(false);
    }
  }
  List.Show();
  return 0;
}