Problem p02265 (C++)

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

using namespace std;

class DLinkList {
private:
  struct ListNode {
    ListNode *prior, *next;
    int val;
    ListNode(int v) : prior(NULL), next(NULL), val(v){};
  };

private:
  ListNode *head;
  int len;

public:
  DLinkList() {
    head = new ListNode(-1);
    head->prior = head;
    head->next = head;

    len = 0;
  }

  ListNode *search(int v) {
    ListNode *p = head->next;
    while (p != head && p->val != v)
      p = p->next;
    return p;
  }

  void del_helper(ListNode *p) {
    if (p == head)
      return;
    p->prior->next = p->next;
    p->next->prior = p->prior;
    delete p;
    len--;
  }

  void del_first() { del_helper(head->next); }

  void del_last() { del_helper(head->prior); }

  void del_x(int v) { del_helper(search(v)); }

  void insert(int val) {
    ListNode *node = new ListNode(val);

    node->next = head->next;
    head->next->prior = node;

    head->next = node;
    node->prior = head;

    len++;
  }

  void print() {
    ListNode *p = head->next;
    int count = 0;
    while (count < len - 1) {
      cout << p->val << " ";
      p = p->next;
    }
    if (len - 1 >= 0)
      cout << p->val << endl;
  }
};

int main() {
  ios_base::sync_with_stdio(false);

  DLinkList l;
  string str;
  int val;
  cin >> str;
  while (cin >> str) {
    if (str == "insert") {
      cin >> val;
      l.insert(val);
    } else if (str == "delete") {
      cin >> val;
      l.del_x(val);
    } else if (str == "deleteFirst")
      l.del_first();
    else if (str == "deleteLast")
      l.del_last();
  }
  l.print();
}

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

using namespace std;

class DLinkList {
private:
  struct ListNode {
    ListNode *prior, *next;
    int val;
    ListNode(int v) : prior(NULL), next(NULL), val(v){};
  };

private:
  ListNode *head;
  int len;

public:
  DLinkList() {
    head = new ListNode(-1);
    head->prior = head;
    head->next = head;

    len = 0;
  }

  ListNode *search(int v) {
    ListNode *p = head->next;
    while (p != head && p->val != v)
      p = p->next;
    return p;
  }

  void del_helper(ListNode *p) {
    if (p == head)
      return;
    p->prior->next = p->next;
    p->next->prior = p->prior;
    delete p;
    len--;
  }

  void del_first() { del_helper(head->next); }

  void del_last() { del_helper(head->prior); }

  void del_x(int v) { del_helper(search(v)); }

  void insert(int val) {
    ListNode *node = new ListNode(val);

    node->next = head->next;
    head->next->prior = node;

    head->next = node;
    node->prior = head;

    len++;
  }

  void print() {
    ListNode *p = head->next;
    int count = 0;
    while (count++ < len - 1) {
      cout << p->val << " ";
      p = p->next;
    }
    if (len - 1 >= 0)
      cout << p->val << endl;
  }
};

int main() {
  ios_base::sync_with_stdio(false);

  DLinkList l;
  string str;
  int val;
  cin >> str;
  while (cin >> str) {
    if (str == "insert") {
      cin >> val;
      l.insert(val);
    } else if (str == "delete") {
      cin >> val;
      l.del_x(val);
    } else if (str == "deleteFirst")
      l.del_first();
    else if (str == "deleteLast")
      l.del_last();
  }
  l.print();
}