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

#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;

class LIST {
public:
  LIST(){};
  ~LIST(){};
  void Insert(int val);
  LIST *Search(int val);
  void Delete(LIST *l);
  void DeleteFirst();
  void DeleteLast();
  void DeleteValue(int val);
  void Show() const;

  long int value;
  LIST *next = NULL;
  LIST *previous = NULL;
};

int main() {
  LIST *L = new LIST();
  L->next = L;
  L->previous = L;

  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    char str[15];
    int N;
    scanf("%s", str);
    if (str[6] != 'F' && str[6] != 'L')
      scanf("%d", &N);
    if (str[0] == 'i') {
      L->Insert(N);
    }

    else if (str[6] == 'F') {
      L->DeleteFirst();
    } else if (str[6] == 'L') {
      L->DeleteLast();
    } else if (str[0] == 'd') {
      L->DeleteValue(N);
    }
  }

  L->Show();

  return 0;
}

void LIST::Insert(int val) {
  LIST *l = new LIST();
  l->value = val;
  l->next = next;
  next->previous = l;
  next = l;
  l->previous = this;
}

LIST *LIST::Search(int val) {
  LIST *l = next;
  while (l != this && l->value != val) {
    l = l->next;
  }
  return l;
}

void LIST::Delete(LIST *l) {
  if (l != this) {
    l->previous->next = l->next;
    l->next->previous = l->previous;
    delete l;
  }
}

void LIST::DeleteFirst() { Delete(next); }
void LIST::DeleteLast() { Delete(previous); }

void LIST::DeleteValue(int val) { Delete(Search(val)); }

void LIST::Show() const {
  LIST *l = next;
  while (l->next != next) {
    cout << l->value;
    l = l->next;
    if (l->next != next) {
      cout << " ";
    }
  }
  cout << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;

class LIST {
public:
  LIST(){};
  ~LIST(){};
  void Insert(int val);
  LIST *Search(int val);
  void Delete(LIST *l);
  void DeleteFirst();
  void DeleteLast();
  void DeleteValue(int val);
  void Show() const;

  int value;
  LIST *next = NULL;
  LIST *previous = NULL;
};

int main() {
  LIST *L = new LIST();
  L->next = L;
  L->previous = L;

  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string str;
    int N;
    cin >> str;
    if (str != "deleteFirst" && str != "deleteLast")
      cin >> N;
    if (str == "insert") {
      L->Insert(N);
    } else if (str == "delete") {
      L->DeleteValue(N);
    } else if (str == "deleteFirst") {
      L->DeleteFirst();
    } else if (str == "deleteLast") {
      L->DeleteLast();
    }
  }

  L->Show();

  return 0;
}

void LIST::Insert(int val) {
  LIST *l = new LIST();
  l->value = val;
  l->next = next;
  next->previous = l;
  next = l;
  l->previous = this;
}

LIST *LIST::Search(int val) {
  LIST *l = next;
  while (l != NULL && l->value != val) {
    l = l->next;
  }
  return l;
}

void LIST::Delete(LIST *l) {
  if (l != this) {
    l->previous->next = l->next;
    l->next->previous = l->previous;
    delete l;
  }
}

void LIST::DeleteFirst() { Delete(next); }
void LIST::DeleteLast() { Delete(previous); }

void LIST::DeleteValue(int val) { Delete(Search(val)); }

void LIST::Show() const {
  LIST *l = next;
  while (l->next != next) {
    cout << l->value;
    l = l->next;
    if (l->next != next) {
      cout << " ";
    }
  }
  cout << endl;
}