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

#include <iostream>
#include <memory>

class Dlist {
public:
  Dlist(std::shared_ptr<Dlist> prev, std::shared_ptr<Dlist> next,
        unsigned int key)
      : prev(prev), next(next), key(key) {
    // std::cout << "  construct:" << this << ", " << key << std::endl;
  }
  ~Dlist() {
    // std::cout << "    destruct:" << this << ", " << key << std::endl;
  }
  std::shared_ptr<Dlist> prev;
  std::shared_ptr<Dlist> next;
  unsigned int key;
};

std::shared_ptr<Dlist> origin;
std::shared_ptr<Dlist> last;

std::shared_ptr<Dlist> createDlist(std::shared_ptr<Dlist> prev,
                                   std::shared_ptr<Dlist> next,
                                   unsigned int key) {
  std::shared_ptr<Dlist> ptr(new Dlist(prev, next, key));
  return ptr;
}

void purgeNode(std::shared_ptr<Dlist> ptr) {
  ptr->next = nullptr;
  ptr->prev = nullptr;
}

void insertKey(unsigned int key) {
  std::shared_ptr<Dlist> ins = createDlist(origin, origin->next, key);
  if (origin->next != nullptr) {
    origin->next->prev = ins;
  } else {
    last = ins;
  }
  origin->next = ins;
}
void deleteKey(unsigned int key, std::shared_ptr<Dlist> cur) {
  if (key == cur->key) {
    if (cur->next != nullptr)
      cur->next->prev = cur->prev;
    cur->prev->next = cur->next;
    if (last == cur)
      last = cur->prev;
    purgeNode(cur);
  } else if (cur->next != nullptr) {
    deleteKey(key, cur->next);
  }
}
void deleteFirst(void) {
  std::shared_ptr<Dlist> temp = origin->next;
  if (origin->next->next != nullptr)
    origin->next->next->prev = origin;
  origin->next = origin->next->next;
  purgeNode(temp);
  temp = nullptr;
}
void deleteLast(void) {
  last = last->prev;
  purgeNode(last->next);
  last->next = nullptr;
}

void showNodes(std::shared_ptr<Dlist> cur) {
  std::cout << "key:" << cur->key << ", this:" << cur << ", prev:" << cur->prev
            << ", next:" << cur->next << std::endl;
  if (cur->next != nullptr)
    showNodes(cur->next);
  else
    return;
}

void answer(std::shared_ptr<Dlist> cur) {
  std::cout << cur->key;
  if (cur->next != nullptr) {
    std::cout << " ";
    answer(cur->next);
  }
}

int main() {
  unsigned int num;
  std::cin >> num;

  origin = createDlist(nullptr, nullptr, 0);
  last = origin;

  std::string command;
  unsigned int key;
  for (unsigned int i = 0; i < num; i++) {
    std::cin >> command;
    if ((command != "deleteFirst") && (command != "deleteLast"))
      std::cin >> key;
    if (command == "insert") {
      insertKey(key);
    } else if (command == "delete") {
      deleteKey(key, origin->next);
    } else if (command == "deleteFirst") {
      deleteFirst();
    } else if (command == "deleteLast") {
      deleteLast();
    }
  }
  // showNodes(origin);
  answer(origin->next);
  std::cout << std::endl;
  purgeNode(origin);
  purgeNode(last);
}


Error: TLE

Fixed submission:

#include <iostream>

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

void showNodes(Node *cur) {
  std::cout << cur;
  if (cur->next) {
    std::cout << "->";
    showNodes(cur->next);
  } else {
    std::cout << "\\n";
    return;
  }
}

void showKeys(Node *ptr) {
  if (!ptr)
    return;
  Node *cur = ptr;

  while (1) {
    std::cout << cur->key;
    if (!cur->next) {
      std::cout << "\\n";
      return;
    } else {
      std::cout << " ";
      cur = cur->next;
    }
  }
}

// insert nptr(key) next to the cur
void insertNode(int key, Node *cur) {
  Node *nptr = new Node;
  // create node
  nptr->key = key;
  nptr->next = cur->next;
  nptr->prev = cur;
  // insert
  if (cur->next)
    cur->next->prev = nptr;
  cur->next = nptr;
}

void deleteNode(int key, Node *begin) {
  Node *cur = begin;

  while (1) {
    if (cur->key == key) {
      cur->prev->next = cur->next;
      if (cur->next)
        cur->next->prev = cur->prev;
      return;
    }
    if (cur->next)
      cur = cur->next;
    else
      break;
  }
  return;
}

void deleteFirst(Node *origin) {
  if (!origin->next)
    return;

  if (origin->next->next) {
    origin->next->next->prev = origin;
    origin->next = origin->next->next;
  } else {
    origin->next = NULL;
  }
}

void deleteLast(Node *origin) {
  if (!origin->next)
    return;
  Node *cur = origin->next;
  while (1) {
    if (!cur->next) {
      cur->prev->next = NULL;
      return;
    } else {
      cur = cur->next;
    }
  }
  return;
}

int main() {
  // initialize
  Node *null = new Node;
  null->key = 0;
  null->next = NULL;
  null->prev = NULL;

  int n, key;
  std::string opr;

  // input
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);

  std::cin >> n;
  for (int i = 0; i < n; i++) {
    std::cin >> opr;
    if (opr == "insert") {
      std::cin >> key;
      insertNode(key, null);
    } else if (opr == "delete") {
      std::cin >> key;
      deleteNode(key, null);
    } else if (opr == "deleteFirst") {
      deleteFirst(null);
    } else {
      deleteLast(null);
    }
  }
  showKeys(null->next);
}