Problem p02265 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <vector>

using namespace std;

struct Node {
  Node *prev;
  Node *next;
  int value;
  Node(Node *prev, Node *next, int value)
      : prev(prev), next(next), value(value) {}
};

class List {
  Node *begin = new Node(nullptr, nullptr, -1);
  Node *end = new Node(nullptr, nullptr, -1);

public:
  List() {
    begin->next = end;
    end->prev = begin;
  }

  void insert(int value) {
    auto next = begin->next;
    auto n = new Node(begin, next, value);
    next->prev = n;
    begin->next = n;
  }
  void deleteNode(int value) {
    auto node = begin;
    for (node = begin->next; node != end; node = node->next) {
      if (node->value == value) {
        node->prev->next = node->next;
        node->next->prev = node->prev;
        return;
      }
    }
  }
  void deleteFirst() {
    auto old = begin->next;
    auto next = old->next;
    begin->next = next;
    next->prev = begin;
    delete old;
  }
  void deleteLast() {
    auto old = end->prev;
    auto prev = old->prev;
    prev->next = end;
    end->prev = prev;
    delete old;
  }

  void print() {
    for (auto node = begin->next; node != end; node = node->next) {
      cout << node->value;
      if (node != end->prev) {
        cout << " ";
      }
    }
    cout << endl;
  }
};

int main() {
  int n;
  cin >> n;
  List list;
  for (int i = 0; i < n; ++i) {
    string command;
    cin >> command;
    if (command == "deleteFirst") {
      list.deleteFirst();
    } else if (command == "deleteLast") {
      list.deleteLast();
    } else if (command == "insert") {
      int value;
      cin >> value;
      list.insert(value);
    } else if (command == "delete") {
      int value;
      cin >> value;
      list.deleteNode(value);
    }
  }
  list.print();
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <vector>

using namespace std;

struct Node {
  Node *prev;
  Node *next;
  int value;
  Node(Node *prev, Node *next, int value)
      : prev(prev), next(next), value(value) {}
};

class List {
  Node *begin = new Node(nullptr, nullptr, -1);
  Node *end = new Node(nullptr, nullptr, -1);

public:
  List() {
    begin->next = end;
    end->prev = begin;
  }

  void insert(int value) {
    auto next = begin->next;
    auto n = new Node(begin, next, value);
    next->prev = n;
    begin->next = n;
  }
  void deleteNode(int value) {
    auto node = begin;
    for (node = begin->next; node != end; node = node->next) {
      if (node->value == value) {
        node->prev->next = node->next;
        node->next->prev = node->prev;
        return;
      }
    }
  }
  void deleteFirst() {
    auto old = begin->next;
    auto next = old->next;
    begin->next = next;
    next->prev = begin;
    delete old;
  }
  void deleteLast() {
    auto old = end->prev;
    auto prev = old->prev;
    prev->next = end;
    end->prev = prev;
    delete old;
  }

  void print() {
    for (auto node = begin->next; node != end; node = node->next) {
      cout << node->value;
      if (node != end->prev) {
        cout << " ";
      }
    }
    cout << endl;
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;
  List list;
  for (int i = 0; i < n; ++i) {
    string command;
    cin >> command;
    if (command == "deleteFirst") {
      list.deleteFirst();
    } else if (command == "deleteLast") {
      list.deleteLast();
    } else if (command == "insert") {
      int value;
      cin >> value;
      list.insert(value);
    } else if (command == "delete") {
      int value;
      cin >> value;
      list.deleteNode(value);
    }
  }
  list.print();
}