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

#include <cstdio>
#include <cstdlib>
#include <cstring>

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

Node *nil;

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

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

void deleteNode(Node *cur) {
  cur->next->prev = cur->prev;
  cur->prev->next = cur->next;
  free(cur);
}

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

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

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

void deleteKey(int key) {
  Node *cur = listSearch(key);
  if (cur == nil)
    return;
  deleteNode(cur);
}

void printList() {
  Node *ptr = nil->next;
  // do{
  //     if (head == ptr)
  //         printf("%d", ptr->x);
  //     else
  //         printf(" %d", ptr->x);
  //     ptr = ptr->next;
  // }while(ptr != head);
  int flag = 1;
  while (ptr != nil) {
    if (flag) {
      printf("%d", ptr->key);
      flag = 0;
    } else {
      printf(" %d", ptr->key);
    }
    ptr = ptr->next;
  }
  putchar('\\n');
}

int main() {
  init();
  int n, key;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    char s[10];
    scanf("%s", s);
    if (strcmp(s, "insert") == 0) {
      scanf("%d", &key);
      insert(key);
    }
    if (strcmp(s, "delete") == 0) {
      scanf("%d", &key);
      deleteKey(key);
    }
    if (strcmp(s, "deleteFirst") == 0) {
      deleteFirst();
    }
    if (strcmp(s, "deleteLast") == 0) {
      deleteLast();
    }
  }
  printList();
  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <cstring>

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

Node *nil;

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

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

void deleteNode(Node *cur) {
  cur->next->prev = cur->prev;
  cur->prev->next = cur->next;
  free(cur);
}

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

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

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

void deleteKey(int key) {
  Node *cur = listSearch(key);
  deleteNode(cur);
}

void printList() {
  Node *ptr = nil->next;
  // do{
  //     if (head == ptr)
  //         printf("%d", ptr->x);
  //     else
  //         printf(" %d", ptr->x);
  //     ptr = ptr->next;
  // }while(ptr != head);
  int flag = 1;
  while (ptr != nil) {
    if (flag) {
      printf("%d", ptr->key);
      flag = 0;
    } else {
      printf(" %d", ptr->key);
    }
    ptr = ptr->next;
  }
  putchar('\\n');
}

int main() {
  init();
  int n, key;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    char s[10];
    scanf("%s", s);
    if (strcmp(s, "insert") == 0) {
      scanf("%d", &key);
      insert(key);
    }
    if (strcmp(s, "delete") == 0) {
      scanf("%d", &key);
      deleteKey(key);
    }
    if (strcmp(s, "deleteFirst") == 0) {
      deleteFirst();
    }
    if (strcmp(s, "deleteLast") == 0) {
      deleteLast();
    }
  }
  printList();
  return 0;
}