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

#include <cstdio>
#include <cstdlib>
#include <string>

using namespace std;

struct Node {
  int key;
  Node *prev;
  Node *next;

  Node(int key) : key(key) {}
};

class DoublyLinkedList {
public:
  Node *nil;

  void insert(Node *x) {
    x->next = nil->next;
    nil->next->prev = x;
    nil->next = x;
    x->prev = nil;
  }

  Node *listSearch(int key) {
    Node *cur = nil->next;
    while (cur != nil && cur->key != key) {
      cur = cur->next;
    }
    return cur;
  }

  void deleteNode(Node *t) {
    if (t == nil) {
      return;
    }
    t->prev->next = t->next;
    t->next->prev = t->prev;
    free(t);
  }

  void deleteFirst() { deleteNode(nil->next); }

  void deleteLast() { deleteNode(nil->prev); }

  void deleteKey(int key) { deleteNode(listSearch(key)); }

  DoublyLinkedList() {
    nil = (Node *)malloc(sizeof(Node));
    nil->next = nil;
    nil->prev = nil;
  }
};

int main() {
  int n;
  scanf("%d", &n);

  DoublyLinkedList list;
  for (int i = 0; i < n; i++) {
    char c[20];
    scanf("%s", c);
    string command = c;

    if (command == "insert") {
      int key;
      scanf("%d", &key);
      Node *x = new Node(key);
      list.insert(x);
    } else if (command == "delete") {
      int key;
      scanf("%d", &key);
      list.deleteKey(key);
    } else if (command == "deleteFirst") {
      list.deleteFirst();
    } else if (command == "deleteLast") {
      list.deleteLast();
    }
  }

  Node *cur = list.nil->next;
  while (cur != list.nil) {
    if (cur == list.nil->next) {
      printf("%d", cur->key);
    } else {
      printf(" %d", cur->key);
    }

    cur = cur->next;
  }
  printf("\\n");

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

struct Node {
  int key;
  Node *prev;
  Node *next;
};

Node *nil;

void init() {
  nil = (Node *)malloc(sizeof(Node));
  nil->next = nil;
  nil->prev = nil;
}

void insert(int key) {
  Node *x = (Node *)malloc(sizeof(Node));
  x->key = key;
  x->next = nil->next;
  nil->next->prev = x;
  nil->next = x;
  x->prev = nil;
}

Node *listSearch(int key) {
  Node *cur = nil->next;
  while (cur != nil && cur->key != key) {
    cur = cur->next;
  }
  return cur;
}

void deleteNode(Node *t) {
  if (t == nil) {
    return;
  }
  t->prev->next = t->next;
  t->next->prev = t->prev;
  free(t);
}

void deleteFirst() { deleteNode(nil->next); }

void deleteLast() { deleteNode(nil->prev); }

void deleteKey(int key) { deleteNode(listSearch(key)); }

void command(const string &cmd) {
  int key;
  if (cmd == "insert") {
    cin >> key;
    insert(key);
  } else if (cmd == "delete") {
    cin >> key;
    deleteKey(key);
  } else if (cmd == "deleteFirst") {
    deleteFirst();
  } else if (cmd == "deleteLast") {
    deleteLast();
  }
}

int main() {
  init();

  int n;

  cin >> n;

  for (int i = 0; i != n; ++i) {
    string cmd;
    cin >> cmd;
    command(cmd);
  }

  auto node = nil->next;
  while (node != nil) {
    if (node == nil->next) {
      cout << node->key;
    } else {
      cout << " " + to_string(node->key);
    }
    node = node->next;
  }
  cout << endl;

  return 0;
}