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

#include <iostream>

using namespace std;

class DLL {

private:
  typedef struct NodeData {
    NodeData *next;
    NodeData *prev;
    int key;
    NodeData(int newKey) {
      next = nullptr;
      prev = nullptr;
      key = newKey;
    }
    ~NodeData() {}
  } *Node;

  Node root;

public:
  DLL(void);
  void insert(int);
  void deleteKey(int);
  void deleteFirst(void);
  void deleteLast(void);
  void printList(void);
  ~DLL();
};

DLL::DLL(void) {
  root = new NodeData(0);
  root->next = root;
  root->prev = root;
}

DLL::~DLL() {
  Node node = root->next, nextNode;
  while (node != root) {
    nextNode = node->next;
    delete node;
    node = nextNode;
  }
  delete root;
};

void DLL::insert(int key) {
  Node newNode = new NodeData(key);
  newNode->next = root->next;
  newNode->prev = root;

  root->next->prev = newNode;
  root->next = newNode;
}

void DLL::deleteKey(int key) {
  Node node = root->next;
  while (node != root && node->key != key)
    node = node->next;

  if (node != root) {
    node->prev->next = node->next;
    node->next->prev = node->prev;
    delete node;
  }
}

void DLL::deleteFirst(void) {
  if (root->next != root) {
    Node temp = root->next;
    root->next->next->prev = root;
    root->next = root->next->next;
    delete temp;
  }
}

void DLL::deleteLast(void) {
  if (root->prev != root) {
    Node temp = root->prev;
    root->prev->prev->next = root;
    root->prev = root->prev->prev;
    delete temp;
  }
}

void DLL::printList(void) {
  if (root->next != root) {
    cout << root->next->key;
    for (Node node = root->next->next; node != root; node = node->next)
      cout << " " << node->key;
    cout << endl;
  }
}

int main(void) {
  size_t n;
  DLL dlist;
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;

  for (size_t i = 0; i < n; i++) {
    string buf;
    cin >> buf;
    if (buf == "insert") {
      int key;
      cin >> key;
      dlist.insert(key);
    } else if (buf == "delete") {
      int key;
      cin >> key;
      dlist.deleteKey(key);
    } else if (buf == "deleteFirst")
      dlist.deleteFirst();
    else if (buf == "deleteLast")
      dlist.deleteLast();
    else {
      cout << "Invalid input";
      exit(-1);
    }
  }

  dlist.printList();

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

class DLL {

private:
  typedef struct NodeData {
    NodeData *next;
    NodeData *prev;
    int key;
    NodeData(int newKey) {
      next = nullptr;
      prev = nullptr;
      key = newKey;
    }
    ~NodeData() {}
  } *Node;

  Node root;

public:
  DLL(void);
  void insert(int);
  void deleteKey(int);
  void deleteFirst(void);
  void deleteLast(void);
  void printList(void);
  ~DLL();
};

DLL::DLL(void) {
  root = new NodeData(0);
  root->next = root;
  root->prev = root;
}

DLL::~DLL() {
  Node node = root->next, nextNode;
  while (node != root) {
    nextNode = node->next;
    delete node;
    node = nextNode;
  }
  delete root;
};

void DLL::insert(int key) {
  Node newNode = new NodeData(key);
  newNode->next = root->next;
  newNode->prev = root;

  root->next->prev = newNode;
  root->next = newNode;
}

void DLL::deleteKey(int key) {
  Node node = root->next;
  while (node != root && node->key != key)
    node = node->next;

  if (node != root) {
    node->prev->next = node->next;
    node->next->prev = node->prev;
    delete node;
  }
}

void DLL::deleteFirst(void) {
  if (root->next != root) {
    Node temp = root->next;
    root->next->next->prev = root;
    root->next = root->next->next;
    delete temp;
  }
}

void DLL::deleteLast(void) {
  if (root->prev != root) {
    Node temp = root->prev;
    root->prev->prev->next = root;
    root->prev = root->prev->prev;
    delete temp;
  }
}

void DLL::printList(void) {
  if (root->next != root) {
    cout << root->next->key;
    for (Node node = root->next->next; node != root; node = node->next)
      cout << " " << node->key;
    cout << endl;
  }
}

int main(void) {
  size_t n;
  cin >> n;
  DLL dlist;
  for (size_t i = 0; i < n; i++) {
    string buf;
    cin >> buf;
    if (buf == "insert") {
      int key;
      cin >> key;
      dlist.insert(key);
    } else if (buf == "delete") {
      int key;
      cin >> key;
      dlist.deleteKey(key);
    } else if (buf == "deleteFirst")
      dlist.deleteFirst();
    else if (buf == "deleteLast")
      dlist.deleteLast();
    else {
      cout << "Invalid input";
      exit(-1);
    }
  }

  dlist.printList();

  return 0;
}