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

#include <cstdio>
#include <cstring>

class Node {
public:
  long key;
  Node *prev;
  Node *next;
  Node(long x) {
    key = x;
    prev = next = NULL;
  };
};

class DoublyLinkedList {
  Node *nil;

public:
  DoublyLinkedList(void) {
    Node *n = new Node(0);
    nil = n;
    nil->prev = nil;
    nil->next = nil;
  };
  bool isempty(void) { return nil->next == nil; }
  void insertNode(Node *n) {
    /* insert the node 'n' at beginning of the list */
    n->prev = nil;
    n->next = nil->next;
    nil->next->prev = n;
    nil->next = n;
  };
  void insert(long x) {
    Node *n = new Node(x);
    insertNode(n);
  };
  void deleteWithKey(long x) {
    Node *n = nil->next;
    while (n != nil && n->key != x) {
      n = n->next;
    }
    if (n != nil) {
      n->prev->next = n->next;
      n->next->prev = n->prev;
      delete n;
    }
  };
  void deleteFirst(void) {
    if (!isempty()) {
      deleteWithKey(nil->next->key);
    }
  }
  void deleteLast(void) {
    if (!isempty()) {
      Node *n = nil->prev;
      nil->prev->prev->next = nil;
      nil->prev = nil->prev->prev;
      delete n;
    }
  }
  void printKeys(void) {
    for (Node *n = nil->next; n != nil; n = n->next) {
      printf("%ld", n->key);
      if (n->next != nil) {
        printf(" ");
      } else {
        printf("\\n");
      }
    }
  };
};

int main(void) {
  int size;
  long key;
  char cmd[20];
  DoublyLinkedList *DLList = new DoublyLinkedList();

  scanf("%d", &size);

  for (int i = 0; i < size; i++) {
    scanf("%s", cmd);

    if (cmd[0] == 'i') {
      scanf("%ld", &key);
      DLList->insert(key);
    } else if (cmd[0] == 'd') {
      if (cmd[6] == 'F') {
        DLList->deleteFirst();
      } else if (cmd[6] == 'L') {
        DLList->deleteLast();
      } else {
        scanf("%ld", &key);
        DLList->deleteWithKey(key);
      }
    }
  }

  DLList->printKeys();

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>

class Node {
public:
  int key;
  Node *prev;
  Node *next;
  Node(int x) {
    key = x;
    prev = next = NULL;
  };
};

class DoublyLinkedList {
  Node *nil;

public:
  DoublyLinkedList(void) {
    Node *n = new Node(0);
    nil = n;
    nil->prev = nil;
    nil->next = nil;
  };
  bool isempty(void) { return nil->next == nil; }
  void insertNode(Node *n) {
    /* insert the node 'n' at beginning of the list */
    n->prev = nil;
    n->next = nil->next;
    nil->next->prev = n;
    nil->next = n;
  };
  void insert(int x) {
    Node *n = new Node(x);
    insertNode(n);
  };
  void deleteWithKey(int x) {
    Node *n = nil->next;
    while (n != nil && n->key != x) {
      n = n->next;
    }
    n->prev->next = n->next;
    n->next->prev = n->prev;
    delete n;
  };
  void deleteFirst(void) {
    if (!isempty()) {
      deleteWithKey(nil->next->key);
    }
  }
  void deleteLast(void) {
    if (!isempty()) {
      Node *n = nil->prev;
      nil->prev->prev->next = nil;
      nil->prev = nil->prev->prev;
      delete n;
    }
  }
  void printKeys(void) {
    for (Node *n = nil->next; n != nil; n = n->next) {
      printf("%d", n->key);
      if (n->next != nil) {
        printf(" ");
      } else {
        printf("\\n");
      }
    }
  };
};

int main(void) {
  int size;
  int key;
  char cmd[11];
  DoublyLinkedList *DLList = new DoublyLinkedList();

  scanf("%d", &size);

  for (int i = 0; i < size; i++) {
    scanf("%s", cmd);

    if (cmd[0] == 'i') {
      scanf("%d", &key);
      DLList->insert(key);
    } else if (cmd[0] == 'd') {
      if (cmd[6] == 'F') {
        DLList->deleteFirst();
      } else if (cmd[6] == 'L') {
        DLList->deleteLast();
      } else {
        scanf("%d", &key);
        DLList->deleteWithKey(key);
      }
    }
  }

  DLList->printKeys();

  return 0;
}