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

#include <iostream>

namespace detail {
struct node {
  int n;
  node *prev;
  node *next;

  node(int x) : n(x), prev(nullptr), next(nullptr) {}
};

struct list {
  node *head;
  node *tail;

  list() : head(nullptr), tail(nullptr) {}

  void insert(int n) {
    node *x = new node(n);
    x->next = head;
    if (head != nullptr) {
      head->prev = x;
    } else {
      tail = x;
    }
    head = x;
  }

  node *find(int x) {
    node *it = head;
    while (it != nullptr) {
      if (it->n == x) {
        return it;
      }
      it = it->next;
    }
    return it;
  }

  void remove(node *it) {
    if (it == nullptr) {
      return;
    }
    if (it == head) {
      head = it->next;
    }
    if (it == tail) {
      tail = it->prev;
    }
    if (it->prev != nullptr) {
      it->prev->next = it->next;
    }
    if (it->next != nullptr) {
      it->next->prev = it->prev;
    }
    delete it;
  }

  void remove(int x) { remove(find(x)); }

  void removeFirst() { remove(head); }

  void removeLast() { remove(tail); }
};
} // namespace detail

using namespace std;

void print_list(detail::list &list) {
  detail::node *node = list.head;
  while (node != list.tail) {
    cout << node->n << " ";
    node = node->next;
  }
  if (list.tail != nullptr) {
    cout << list.tail->n << endl;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  detail::list list;

  int n;
  cin >> n;

  for (int i = 0; i < n; ++i) {
    int x;
    string c;
    cin >> c;
    if (c == "insert") {
      cin >> x;
      list.insert(x);
    } else if (c == "delete") {
      cin >> x;
      list.remove(x);
    } else if (c == "deleteFirst")
      list.removeFirst();
    else if (c == "deleteLast")
      list.removeLast();
  }
  print_list(list);

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

namespace detail {
struct node {
  int n;
  node *prev;
  node *next;

  node(int x) : n(x), prev(nullptr), next(nullptr) {}
};

struct list {
  node *head;
  node *tail;

  list() : head(nullptr), tail(nullptr) {}

  void insert(int n) {
    node *x = new node(n);
    x->next = head;
    if (head != nullptr) {
      head->prev = x;
    } else {
      tail = x;
    }
    head = x;
  }

  node *find(int x) {
    node *it = head;
    while (it != nullptr) {
      if (it->n == x) {
        return it;
      }
      it = it->next;
    }
    return it;
  }

  void remove(node *it) {
    if (it == nullptr) {
      return;
    }
    if (it == head) {
      head = it->next;
    }
    if (it == tail) {
      tail = it->prev;
    }
    if (it->prev != nullptr) {
      it->prev->next = it->next;
    }
    if (it->next != nullptr) {
      it->next->prev = it->prev;
    }
    delete it;
  }

  void remove(int x) { remove(find(x)); }

  void removeFirst() { remove(head); }

  void removeLast() { remove(tail); }
};
} // namespace detail

using namespace std;

void print_list(detail::list &list) {
  detail::node *node = list.head;
  while (node != list.tail) {
    cout << node->n << " ";
    node = node->next;
  }
  if (list.tail != nullptr) {
    cout << list.tail->n << endl;
  }
}

int main() {
  detail::list list;

  int n;
  cin >> n;

  for (int i = 0; i < n; ++i) {
    int x;
    string c;
    cin >> c;
    if (c == "insert") {
      cin >> x;
      list.insert(x);
    } else if (c == "delete") {
      cin >> x;
      list.remove(x);
    } else if (c == "deleteFirst")
      list.removeFirst();
    else if (c == "deleteLast")
      list.removeLast();

    // print_list(list);
    // cerr << "head:" << list.head->n << endl;
    // cerr << "tail:" << list.tail->n << endl;
  }
  print_list(list);

  return 0;
}