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

#include <iostream>
#include <string>
#include <utility>

constexpr int defaultValue = 0;

class DLLNode {
public:
  DLLNode(int key) : key(key), prev(this), next(this) {}

  DLLNode() : key(defaultValue), prev(this), next(this) {}

  DLLNode *Prev() { return prev; }

  DLLNode *Next() { return next; }

  int Key() { return key; }

  void SetPrev(DLLNode *p) { prev = p; }

  void SetNext(DLLNode *n) { next = n; }

private:
  int key;
  DLLNode *prev;
  DLLNode *next;
};

class DoublyLinkedList {
public:
  DoublyLinkedList() { head = new DLLNode(); }

  // ~DoublyLinkedList(){
  //     for( DLLNode* ptr = head->Next(); ptr != head; ptr = head->Next() ){
  //         DeleteFirst();
  //     }
  // }

  void DoCommand(char cmd, int &x) {
    if (cmd == 'i') {
      Insert(x);
    } else {
      Delete(x);
    }
  }
  void DoCommand(char cmd) {
    if (cmd == 'L') {
      DeleteLast();
    } else {
      DeleteFirst();
    }
  }

  void Print() {
    for (DLLNode *itr = head->Next(); itr != head; itr = itr->Next()) {
      std::cout << itr->Key() << ((itr != head->Prev()) ? " " : "");
    }
    std::cout << "\\n";
  }

private:
  // DLLNode* SearchByKey( int key ){
  //     for( DLLNode* itr = head->Next(); itr != head; itr = itr->Next() ){
  //         if( itr->Key() == key ){
  //             return itr;
  //         }
  //     }
  //     return nullptr;
  // }

  // bool IsEmpty(){
  //     return head == head->Next();
  // }

  void Insert(int x) {
    DLLNode *newNode = new DLLNode(x);
    newNode->SetNext(head->Next());
    head->Next()->SetPrev(newNode);
    head->SetNext(newNode);
    newNode->SetPrev(head);
  }

  void Delete(int key) {
    if (!(head == head->Next())) {
      DLLNode *deleteNode = nullptr;
      for (deleteNode = head->Next(); deleteNode != head;
           deleteNode = deleteNode->Next()) {
        if (deleteNode->Key() == key) {
          break;
        } else if (deleteNode == head->Prev()) {
          return;
        }
      }
      deleteNode->Prev()->SetNext(deleteNode->Next());
      deleteNode->Next()->SetPrev(deleteNode->Prev());
      delete deleteNode;
    }
  }

  void DeleteFirst() {
    if (!(head == head->Next())) {
      head->SetNext(head->Next()->Next());
      delete head->Next()->Prev();
      head->Next()->SetPrev(head);
    }
  }

  void DeleteLast() {
    if (!(head == head->Next())) {
      head->SetPrev(head->Prev()->Prev());
      delete head->Prev()->Next();
      head->Prev()->SetNext(head);
    }
  }
  // Variables
  DLLNode *head;
};

class StringSplitter {
public:
  StringSplitter(std::string &str) {
    if (str[0] == 'i') {
      cmd = std::make_pair('i', std::stoi(&str[7]));
    } else {
      for (int i = 0; i < str.size(); ++i) {
        if (str[i] == ' ') {
          cmd = std::make_pair('d', std::stoi(&str[i + 1]));
          return;
        }
      }
      cmd = std::make_pair(str[6], 0);
      isStrOnly = true;
    }
  }

  std::pair<char, int> &GetPair() { return cmd; }

  char &GetChar() { return cmd.first; }

  bool &IsStringOnly() { return isStrOnly; }

private:
  std::pair<char, int> cmd;
  bool isStrOnly = false;
};

int main() {
  std::cin.tie(0);
  int n;
  std::cin >> n;

  DoublyLinkedList *list = new DoublyLinkedList();
  std::string command;
  StringSplitter *splt = nullptr;
  std::getline(std::cin, command);

  for (int i = 0; i < n; ++i) {
    std::getline(std::cin, command);
    if (!splt) {
      delete splt;
    }
    splt = new StringSplitter(command);
    if (splt->IsStringOnly()) {
      list->DoCommand(splt->GetChar());
    } else {
      list->DoCommand(splt->GetPair().first, splt->GetPair().second);
    }
    // std::cout << "sizeof list: " << sizeof(*list) << "\\n";
    // std::cout << "sizeof splt: " << sizeof(*splt) << "\\n";
    // std::cout << "sizeof comd: " << sizeof(command) << "\\n\\n";
    // list->Print();
  }

  list->Print();
}


Error: MLE

Fixed submission:

#include <iostream>
#include <string>
#include <utility>

constexpr int defaultValue = 0;

class DLLNode {
public:
  DLLNode(int key) : key(key), prev(this), next(this) {}

  DLLNode() : key(defaultValue), prev(this), next(this) {}

  DLLNode *Prev() { return prev; }

  DLLNode *Next() { return next; }

  int Key() { return key; }

  void SetPrev(DLLNode *p) { prev = p; }

  void SetNext(DLLNode *n) { next = n; }

private:
  int key;
  DLLNode *prev;
  DLLNode *next;
};

class DoublyLinkedList {
public:
  DoublyLinkedList() { head = new DLLNode(); }

  // ~DoublyLinkedList(){
  //     for( DLLNode* ptr = head->Next(); ptr != head; ptr = head->Next() ){
  //         DeleteFirst();
  //     }
  // }

  void DoCommand(std::string &cmd, int &x) {
    if (cmd == "insert") {
      Insert(x);
    } else {
      Delete(x);
    }
  }
  void DoCommand(std::string &cmd) {
    if (cmd == "deleteLast") {
      DeleteLast();
    } else {
      DeleteFirst();
    }
  }

  void Print() {
    for (DLLNode *itr = head->Next(); itr != head; itr = itr->Next()) {
      std::cout << itr->Key() << ((itr != head->Prev()) ? " " : "");
    }
    std::cout << "\\n";
  }

private:
  // DLLNode* SearchByKey( int key ){
  //     for( DLLNode* itr = head->Next(); itr != head; itr = itr->Next() ){
  //         if( itr->Key() == key ){
  //             return itr;
  //         }
  //     }
  //     return nullptr;
  // }

  // bool IsEmpty(){
  //     return head == head->Next();
  // }

  void Insert(int x) {
    DLLNode *newNode = new DLLNode(x);
    newNode->SetNext(head->Next());
    head->Next()->SetPrev(newNode);
    head->SetNext(newNode);
    newNode->SetPrev(head);
  }

  void Delete(int key) {
    if (!(head == head->Next())) {
      DLLNode *deleteNode = nullptr;
      for (deleteNode = head->Next(); deleteNode != head;
           deleteNode = deleteNode->Next()) {
        if (deleteNode->Key() == key) {
          break;
        } else if (deleteNode == head->Prev()) {
          return;
        }
      }
      deleteNode->Prev()->SetNext(deleteNode->Next());
      deleteNode->Next()->SetPrev(deleteNode->Prev());
      delete deleteNode;
    }
  }

  void DeleteFirst() {
    if (!(head == head->Next())) {
      head->SetNext(head->Next()->Next());
      delete head->Next()->Prev();
      head->Next()->SetPrev(head);
    }
  }

  void DeleteLast() {
    if (!(head == head->Next())) {
      head->SetPrev(head->Prev()->Prev());
      delete head->Prev()->Next();
      head->Prev()->SetNext(head);
    }
  }
  // Variables
  DLLNode *head;
};

class StringSplitter {
public:
  StringSplitter(std::string &str) {
    if (str[0] == 'i') {
      cmd = std::make_pair("insert", std::stoi(&str[7]));
    } else {
      std::string tmp("");
      for (int i = 0; i < str.size(); ++i) {
        if (str[i] == ' ') {
          cmd = std::make_pair(tmp, std::stoi(&str[i + 1]));
          return;
        }
        tmp += str[i];
      }
      cmd = std::make_pair(str, 0);
      isStrOnly = true;
    }
  }

  std::pair<std::string, int> &GetPair() { return cmd; }

  std::string &GetStr() { return cmd.first; }

  bool &IsStringOnly() { return isStrOnly; }

private:
  std::pair<std::string, int> cmd;
  bool isStrOnly = false;
};

int main() {
  std::cin.tie(0);
  int n;
  std::cin >> n;

  DoublyLinkedList *list = new DoublyLinkedList();
  std::string command;
  StringSplitter *splt = nullptr;
  std::getline(std::cin, command);

  for (int i = 0; i < n; ++i) {
    std::getline(std::cin, command);
    if (!splt) {
      delete splt;
    }
    splt = new StringSplitter(command);
    if (splt->IsStringOnly()) {
      list->DoCommand(splt->GetStr());
    } else {
      list->DoCommand(splt->GetPair().first, splt->GetPair().second);
    }
    // std::cout << "sizeof list: " << sizeof(*list) << "\\n";
    // std::cout << "sizeof splt: " << sizeof(*splt) << "\\n";
    // std::cout << "sizeof comd: " << sizeof(command) << "\\n\\n";
    // list->Print();
  }

  list->Print();
}