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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define int long long

using namespace std;

struct node {
  int key;
  node *prev;
  node *next;
};

class doubly_linked {
private:
public:
  node *head;
  doubly_linked() {
    head = new node();
    head->next = head;
    head->prev = head;
  }
  void insert(int x) {
    node *now = new node();
    now->next = head->next;
    head->next->prev = now;
    head->next = now;
    now->prev = head;
    now->key = x;
  };
  void delete2(int x) {
    node *now = head->next;
    while ((now->key != x) && (now != head))
      now = now->next;
    if (now != head) {
      now->prev->next = now->next;
      now->next->prev = now->prev;
    }
  }
  void deleteFirst() {
    head->next = head->next->next;
    head->next->prev = head;
  }
  void deleteLast() {
    head->prev = head->prev->prev;
    head->prev->next = head;
  }
  void print() {
    node *now = head->next;
    bool fst = false;
    while (now != head) {
      if (fst)
        cout << " ";
      else
        fst = true;
      cout << now->key;
      now = now->next;
    }
    cout << endl;
  }
};

signed main() {
  doubly_linked d;
  int n, v;
  string s;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s;
    if (s == "insert") {
      cin >> v;
      d.insert(v);
    }
    if (s == "delete") {
      cin >> v;
      d.delete2(v);
    }
    if (s == "deleteFirst")
      d.deleteFirst();
    if (s == "deleteLast")
      d.deleteLast();
  }
  d.print();
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define int long long

using namespace std;

struct node {
  int key;
  node *prev;
  node *next;
};

class doubly_linked {
private:
public:
  node *head;
  doubly_linked() {
    head = new node();
    head->next = head;
    head->prev = head;
  }
  void insert(int x) {
    node *now = new node();
    now->next = head->next;
    head->next->prev = now;
    head->next = now;
    now->prev = head;
    now->key = x;
  };
  void delete2(int x) {
    node *now = head;
    while ((now->key != x) || (now = head))
      now = now->next;
    if (now != head) {
      now->prev->next = now->next;
      now->next->prev = now->prev;
    }
  }
  void deleteFirst() {
    head->next = head->next->next;
    head->next->prev = head;
  }
  void deleteLast() {
    head->prev = head->prev->prev;
    head->prev->next = head;
  }
  void print() {
    node *now = head->next;
    bool fst = false;
    while (now != head) {
      if (fst)
        cout << " ";
      else
        fst = true;
      cout << now->key;
      now = now->next;
    }
    cout << endl;
  }
};

signed main() {
  doubly_linked d;
  int n, v;
  string s;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s;
    if (s == "insert") {
      cin >> v;
      d.insert(v);
    }
    if (s == "delete") {
      cin >> v;
      d.delete2(v);
    }
    if (s == "deleteFirst")
      d.deleteFirst();
    if (s == "deleteLast")
      d.deleteLast();
  }
  d.print();
}