Problem p02265 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int N;

struct List {
  struct ListI {
    ListI *next;
    ListI *prev;
    int value;

    ListI(ListI *n, ListI *p, int v) : next(n), prev(p), value(v) {}

    void *operator new(size_t, void *p) { return p; }

    static ListI *create(ListI *n, ListI *p, int v) {
      return new (malloc(sizeof(ListI))) ListI(n, p, v);
    }
  };

  ListI *first;
  ListI *last;

  List() : first(0), last(0) {}

  void insert(int x) {
    if (first == 0) {
      first = last = ListI::create(0, 0, x);
    } else {
      ListI *of = first;
      ListI *nf = ListI::create(of, 0, x);
      of->prev = nf;
      first = nf;
    }
  }

  void remove(int x) {
    if (first == 0) {
      return;
    }
    if (first == last && first->value == x) {
      free(first);
      first = last = 0;
      return;
    }

    if (first->value == x) {
      deleteFirst();
      return;
    }

    if (first->next != 0) {
      for (ListI *i = first->next; i != last; i = i->next) {
        if (i->value == x) {
          ListI *p = i->prev;
          ListI *n = i->next;
          p->next = n;
          n->prev = p;
          free(i);
          return;
        }
      }
    }

    if (last->value == x) {
      deleteLast();
    }
  }

  void deleteFirst() {
    if (first == 0) {
      return;
    }
    if (first == last) {
      free(first);
      first = last = 0;
      return;
    }

    ListI *f = first;
    first = first->next;
    first->prev = 0;
    free(f);
  }

  void deleteLast() {
    if (last == 0) {
      return;
    }
    if (first == last) {
      free(last);
      first = last = 0;
      return;
    }

    ListI *l = last;
    last = last->prev;
    last->next = 0;
    free(l);
  }

  void print() {
    if (first == 0) {
      cout << endl;
      return;
    }
    for (ListI *i = first; i != last; i = i->next) {
      cout << i->value << " ";
    }
    cout << last->value << endl;
  }
};

int main() {
  cin >> N;
  List lis;
  for (int i = 0; i < N; i++) {
    string mne;
    cin >> mne;
    if (mne == "insert") {
      int val;
      cin >> val;
      lis.insert(val);
    } else if (mne == "delete") {
      int val;
      cin >> val;
      lis.remove(val);
    } else if (mne == "deleteFirst") {
      lis.deleteFirst();
    } else if (mne == "deleteLast") {
      lis.deleteLast();
    }
  }
  lis.print();
  return 0;
}

Fixed submission:
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int N;

struct List {
  struct ListI {
    ListI *next;
    ListI *prev;
    int value;

    ListI(ListI *n, ListI *p, int v) : next(n), prev(p), value(v) {}

    void *operator new(size_t, void *p) { return p; }

    static ListI *create(ListI *n, ListI *p, int v) {
      return new (malloc(sizeof(ListI))) ListI(n, p, v);
    }
  };

  ListI *first;
  ListI *last;

  List() : first(0), last(0) {}

  void insert(int x) {
    if (first == 0) {
      first = last = ListI::create(0, 0, x);
    } else {
      ListI *of = first;
      ListI *nf = ListI::create(of, 0, x);
      of->prev = nf;
      first = nf;
    }
  }

  void remove(int x) {
    if (first == 0) {
      return;
    }
    if (first == last && first->value == x) {
      free(first);
      first = last = 0;
      return;
    }

    if (first->value == x) {
      deleteFirst();
      return;
    }

    if (first->next != 0) {
      for (ListI *i = first->next; i != last; i = i->next) {
        if (i->value == x) {
          ListI *p = i->prev;
          ListI *n = i->next;
          p->next = n;
          n->prev = p;
          free(i);
          return;
        }
      }
    }

    if (last->value == x) {
      deleteLast();
    }
  }

  void deleteFirst() {
    if (first == 0) {
      return;
    }
    if (first == last) {
      free(first);
      first = last = 0;
      return;
    }

    ListI *f = first;
    first = first->next;
    first->prev = 0;
    free(f);
  }

  void deleteLast() {
    if (last == 0) {
      return;
    }
    if (first == last) {
      free(last);
      first = last = 0;
      return;
    }

    ListI *l = last;
    last = last->prev;
    last->next = 0;
    free(l);
  }

  void print() {
    if (first == 0) {
      cout << endl;
      return;
    }
    for (ListI *i = first; i != last; i = i->next) {
      cout << i->value << " ";
    }
    cout << last->value << endl;
  }
};

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

  cin >> N;
  List lis;
  for (int i = 0; i < N; i++) {
    string mne;
    cin >> mne;
    if (mne == "insert") {
      int val;
      cin >> val;
      lis.insert(val);
    } else if (mne == "delete") {
      int val;
      cin >> val;
      lis.remove(val);
    } else if (mne == "deleteFirst") {
      lis.deleteFirst();
    } else if (mne == "deleteLast") {
      lis.deleteLast();
    }
  }
  lis.print();
  return 0;
}