Problem p02265 (C++)

Buggy submission (status: Time Limit Exceeded):
//============================================================================
// Name        : LinkedList.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <cstdlib>
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <string.h>

using namespace std;

#define BUF_SIZE 1024

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

class ConnectedList {
public:
  ConnectedList() {
    motherNode = new Node();
    motherNode->data = -1;
    motherNode->prev = motherNode;
    motherNode->next = motherNode;
  };

  ~ConnectedList() {
    Node *tmpNode = motherNode->next;
    Node *nextNode;
    while (tmpNode != motherNode) {
      nextNode = tmpNode->next;
      delete tmpNode;
      tmpNode = nextNode;
    }
    delete motherNode;
  }

  void insertNode(int x) {
    Node *tmpNode = motherNode->next;
    motherNode->next = new Node();
    motherNode->next->data = x;
    motherNode->next->next = tmpNode;
    motherNode->next->prev = motherNode;
    tmpNode->prev = motherNode->next;
  };

  void deleteNode(int x) {
    Node *tmpNode = motherNode->next;
    while (tmpNode != motherNode && tmpNode->data != x) {
      tmpNode = tmpNode->next;
    }
    if (tmpNode != motherNode) {
      tmpNode->prev->next = tmpNode->next;
      tmpNode->next->prev = tmpNode->prev;
      delete tmpNode;
    }
  }

  void deleteFirst() {
    Node *tmpNode = motherNode->next;
    if (tmpNode != motherNode) {
      motherNode->next = tmpNode->next;
      tmpNode->next->prev = motherNode;
      delete tmpNode;
    }
  }

  void deleteLast() {
    Node *tmpNode = motherNode->prev;
    if (tmpNode != motherNode) {
      tmpNode->prev->next = motherNode;
      motherNode->prev = tmpNode->prev;
    }
    delete tmpNode;
  }

  void showList() {
    Node *tmpNode = motherNode->next;
    if (tmpNode != motherNode) {
      cout << tmpNode->data;
      tmpNode = tmpNode->next;
    }
    while (tmpNode != motherNode) {
      cout << " " << tmpNode->data;
      tmpNode = tmpNode->next;
    }
    cout << "\n";
  }

private:
  Node *motherNode;
};

int main() {
  ConnectedList list;

  string line;
  getline(cin, line);
  int n_commands = stoi(line);

  for (int i = 0; i < n_commands; i++) {

    string command;
    int data;
    cin >> command;
    if (command == "insert") {
      cin >> data;
      list.insertNode(data);
    } else if (command == "delete") {
      cin >> data;
      list.deleteNode(data);
    } else if (command == "deleteFirst") {
      list.deleteFirst();
    } else if (command == "deleteLast") {
      list.deleteLast();
    }
  }

  list.showList();

  return 0;
}


Fixed submission:
//============================================================================
// Name        : LinkedList.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <cstdlib>
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <string.h>

using namespace std;

#define BUF_SIZE 1024

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

class ConnectedList {
public:
  ConnectedList() {
    motherNode = new Node();
    motherNode->data = -1;
    motherNode->prev = motherNode;
    motherNode->next = motherNode;
  };

  ~ConnectedList() {
    Node *tmpNode = motherNode->next;
    Node *nextNode;
    while (tmpNode != motherNode) {
      nextNode = tmpNode->next;
      delete tmpNode;
      tmpNode = nextNode;
    }
    delete motherNode;
  }

  void insertNode(int x) {
    Node *tmpNode = motherNode->next;
    motherNode->next = new Node();
    motherNode->next->data = x;
    motherNode->next->next = tmpNode;
    motherNode->next->prev = motherNode;
    tmpNode->prev = motherNode->next;
  };

  void deleteNode(int x) {
    Node *tmpNode = motherNode->next;
    while (tmpNode != motherNode && tmpNode->data != x) {
      tmpNode = tmpNode->next;
    }
    if (tmpNode != motherNode) {
      tmpNode->prev->next = tmpNode->next;
      tmpNode->next->prev = tmpNode->prev;
      delete tmpNode;
    }
  }

  void deleteFirst() {
    Node *tmpNode = motherNode->next;
    if (tmpNode != motherNode) {
      motherNode->next = tmpNode->next;
      tmpNode->next->prev = motherNode;
      delete tmpNode;
    }
  }

  void deleteLast() {
    Node *tmpNode = motherNode->prev;
    if (tmpNode != motherNode) {
      tmpNode->prev->next = motherNode;
      motherNode->prev = tmpNode->prev;
    }
    delete tmpNode;
  }

  void showList() {
    Node *tmpNode = motherNode->next;
    if (tmpNode != motherNode) {
      cout << tmpNode->data;
      tmpNode = tmpNode->next;
    }
    while (tmpNode != motherNode) {
      cout << " " << tmpNode->data;
      tmpNode = tmpNode->next;
    }
    cout << "\n";
  }

private:
  Node *motherNode;
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ConnectedList list;

  string line;
  getline(cin, line);
  int n_commands = stoi(line);

  for (int i = 0; i < n_commands; i++) {

    string command;
    int data;
    cin >> command;
    if (command == "insert") {
      cin >> data;
      list.insertNode(data);
    } else if (command == "delete") {
      cin >> data;
      list.deleteNode(data);
    } else if (command == "deleteFirst") {
      list.deleteFirst();
    } else if (command == "deleteLast") {
      list.deleteLast();
    }
  }

  list.showList();

  return 0;
}