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

#include <cassert>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// #include <queue>

#define INF 922337203685477580
typedef long long ll;

using namespace std;

struct List_t {
  ll key;
  struct List_t *prev;
  struct List_t *next;
};
typedef struct List_t List;
List *head;

void insert(ll x) {
  List *l = (List *)malloc(sizeof(List));

  l->prev = head;
  l->next = head->next;
  l->next->prev = l;
  head->next = l;
  l->key = x;
}
List *find(ll x) {
  for (List *i = head->next; i != head; i = i->next) {
    if (i->key == x) {
      return i;
    }
  }
  // assert(0);
  return NULL;
}
void del(List *l) {
  if (l == NULL)
    return;
  // List *l = find(x);
  l->prev->next = l->next;
  l->next->prev = l->prev;

  free(l);
}
void deleteLast() { del(head->prev); }
void deleteFirst() { del(head->next); }

int main() {
  head = (List *)malloc(sizeof(List));
  head->prev = head;
  head->next = head;

  int n;
  cin >> n;

  for (int i = 0; i < n; i++) {
    // string cmd;
    char cmd[100];
    // cin >> cmd;
    scanf("%s", cmd);
    // if (cmd == "insert") {
    if (strcmp("insert", cmd) == 0) {
      ll key;
      // cin >> key;
      scanf("%lld", &key);

      insert(key);
      // } else if (cmd == "delete") {
    } else if (strcmp("delete", cmd) == 0) {
      ll key;
      // cin >> key;
      scanf("%lld", &key);

      List *t = find(key);
      del(t);
    } else if (strcmp("deleteFirst", cmd) == 0) {
      deleteFirst();
    } else if (strcmp("deleteLast", cmd) == 0) {
      deleteLast();
    } else {
      assert(0);
    }
  }

  for (List *i = head->next; i != head; i = i->next) {
    if (i != head->next) {
      cout << " ";
    }
    cout << i->key;
  }
  cout << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <cassert>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
// #include <queue>

#define INF 922337203685477580
typedef long long ll;

using namespace std;

struct List_t {
  ll key;
  struct List_t *prev;
  struct List_t *next;
};
typedef struct List_t List;
List *head;

void insert(ll x) {
  List *l = (List *)malloc(sizeof(List));

  l->prev = head;
  l->next = head->next;
  l->next->prev = l;
  head->next = l;
  l->key = x;
}
List *find(ll x) {
  for (List *i = head->next; i != head; i = i->next) {
    if (i->key == x) {
      return i;
    }
  }
  // assert(0);
  return NULL;
}
void del(List *l) {
  if (l == NULL)
    return;
  // List *l = find(x);
  l->prev->next = l->next;
  l->next->prev = l->prev;

  free(l);
}
void deleteLast() { del(head->prev); }
void deleteFirst() { del(head->next); }

int main() {
  head = (List *)malloc(sizeof(List));
  head->prev = head;
  head->next = head;

  int n;
  cin >> n;

  for (int i = 0; i < n; i++) {
    string cmd;
    cin >> cmd;
    // scanf("%s", &cmd);
    if (cmd == "insert") {
      ll key;
      // cin >> key;
      scanf("%lld", &key);

      insert(key);
    } else if (cmd == "delete") {
      ll key;
      // cin >> key;
      scanf("%lld", &key);

      List *t = find(key);
      del(t);
    } else if (cmd == "deleteFirst") {
      deleteFirst();
    } else if (cmd == "deleteLast") {
      deleteLast();
    } else {
      assert(0);
    }
  }

  for (List *i = head->next; i != head; i = i->next) {
    if (i != head->next) {
      cout << " ";
    }
    cout << i->key;
  }
  cout << endl;

  return 0;
}