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 {
    T Value;
    TNode *Next;
    TNode *Prev;
  };
  TNode *FFirst;
  TNode *FLast;

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 *this;
    }
    TIterator &operator++(int) {
      FNode = FNode->Next;
      return *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;
    }
  };
  TList() {
    FFirst = NULL;
    FLast = NULL;
  }
  void push_front(int Value) {
    TNode *Node = new TNode();
    Node->Value = Value;
    Node->Prev = NULL;
    Node->Next = FFirst;
    if (FFirst) {
      FFirst->Prev = Node;
      FFirst = Node;
    } else {
      FFirst = Node;
      FLast = Node;
    }
  }
  void erase(TIterator &It) {
    TNode *Node = It.FNode;
    if (Node->Prev)
      Node->Prev->Next = Node->Next;
    if (Node->Next)
      Node->Next->Prev = Node->Prev;
    if (Node == FFirst) {
      FFirst = Node->Next;
    }
    if (Node == FLast) {
      FLast = Node->Prev;
    }
    delete Node;
  }
  void pop_front() {
    TNode *Next = FFirst->Next;
    if (Next)
      Next->Prev = NULL;
    delete FFirst;
    if (FFirst == FLast)
      FLast = NULL;
    FFirst = Next;
  }
  void pop_back() {
    TNode *Prev = FLast->Prev;
    if (Prev)
      Prev->Next = NULL;
    delete FLast;
    if (FLast == FFirst)
      FFirst = NULL;
    FLast = Prev;
  }
  TIterator begin() { return TIterator(FFirst); }
  TIterator begin() const { return TIterator(FFirst); }
  TIterator end() { return TIterator(NULL); }
  TIterator end() const { return TIterator(NULL); }
};

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) {
    auto 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 {
    auto 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 {
    T Value;
    TNode *Next;
    TNode *Prev;
  };
  TNode *FFirst;
  TNode *FLast;

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 *this;
    }
    TIterator &operator++(int) {
      FNode = FNode->Next;
      return *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;
    }
  };
  TList() {
    FFirst = NULL;
    FLast = NULL;
  }
  void push_front(int Value) {
    TNode *Node = new TNode();
    Node->Value = Value;
    Node->Prev = NULL;
    Node->Next = FFirst;
    if (FFirst) {
      FFirst->Prev = Node;
      FFirst = Node;
    } else {
      FFirst = Node;
      FLast = Node;
    }
  }
  void erase(TIterator &It) {
    TNode *Node = It.FNode;
    if (Node->Prev)
      Node->Prev->Next = Node->Next;
    if (Node->Next)
      Node->Next->Prev = Node->Prev;
    if (Node == FFirst) {
      FFirst = Node->Next;
    }
    if (Node == FLast) {
      FLast = Node->Prev;
    }
    delete Node;
  }
  void pop_front() {
    TNode *Next = FFirst->Next;
    if (Next)
      Next->Prev = NULL;
    delete FFirst;
    FFirst = Next;
  }
  void pop_back() {
    TNode *Prev = FLast->Prev;
    if (Prev)
      Prev->Next = NULL;
    delete FLast;
    FLast = Prev;
  }
  TIterator begin() { return TIterator(FFirst); }
  TIterator begin() const { return TIterator(FFirst); }
  TIterator end() { return TIterator(NULL); }
  TIterator end() const { return TIterator(NULL); }
};

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) {
    auto 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 {
    auto 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;
}