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

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

// #define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
const double PI = acos(-1);
const double EPS = 1e-9;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
struct List {
  struct Node {
    int key;
    Node *next, *prev;
    Node() {}
  };
  Node *p;
  List() {
    p = (Node *)malloc(sizeof(Node));
    p->next = p->prev = p;
  }
  void insert(int x) {
    Node *n = (Node *)malloc(sizeof(Node));
    n->key = x;
    n->next = p->next;
    n->next->prev = n;
    p->next = n;
    n->prev = p;
  }
  Node *findkey(int x) {
    for (Node *f = p->next; f != p; f = f->next)
      if (f->key == x)
        return f;
    return nullptr;
  }
  void Delete_ptr(Node *f) {
    if (f == nullptr)
      return;
    f->prev->next = f->next;
    f->next->prev = f->prev;
    free(f);
  }
  void Delete_x(int x) { Delete_ptr(findkey(x)); }
  void Deletefirst() { Delete_ptr(p->next); }
  void Deletelast() { Delete_ptr(p->prev); }
  void print() {
    for (Node *z = p->next; z != p; z = z->next) {
      if (z != p->next)
        putchar(' ');
      printf("%d", z->key);
    }
    puts("");
  }
};
signed main() {

  int N;
  scanf("%d", &N);
  char c[256];
  List ls;
  rep(i, 0, N) {
    scanf("%s", c);
    if (c[0] == 'i') {
      int x;
      cin >> x;
      ls.insert(x);
    } else if (strcmp(c, "delete") == 0) {
      int x;
      cin >> x;
      ls.Delete_x(x);
    } else if (c[6] == 'F') {
      ls.Deletefirst();
    } else
      ls.Deletelast();
  }
  ls.print();
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

// #define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
const double PI = acos(-1);
const double EPS = 1e-9;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
struct List {
  struct Node {
    int key;
    Node *next, *prev;
    Node() {}
  };
  Node *p;
  List() {
    p = (Node *)malloc(sizeof(Node));
    p->next = p->prev = p;
  }
  void insert(int x) {
    Node *n = (Node *)malloc(sizeof(Node));
    n->key = x;
    n->next = p->next;
    n->next->prev = n;
    p->next = n;
    n->prev = p;
  }
  Node *findkey(int x) {
    for (Node *f = p->next; f != p; f = f->next)
      if (f->key == x)
        return f;
    return nullptr;
  }
  void Delete_ptr(Node *f) {
    if (f == nullptr)
      return;
    f->prev->next = f->next;
    f->next->prev = f->prev;
    free(f);
  }
  void Delete_x(int x) { Delete_ptr(findkey(x)); }
  void Deletefirst() { Delete_ptr(p->next); }
  void Deletelast() { Delete_ptr(p->prev); }
  void print() {
    for (Node *z = p->next; z != p; z = z->next) {
      if (z != p->next)
        putchar(' ');
      printf("%d", z->key);
    }
    puts("");
  }
};
signed main() {

  int N;
  cin >> N;
  List ls;
  rep(i, 0, N) {
    string s;
    cin >> s;
    if (s[0] == 'i') {
      int x;
      cin >> x;
      ls.insert(x);
    } else if (s == "delete") {
      int x;
      cin >> x;
      ls.Delete_x(x);
    } else if (s[6] == 'F') {
      ls.Deletefirst();
    } else
      ls.Deletelast();
  }
  ls.print();
  return 0;
}