Problem:
Doubly Linked List
Your task is to implement a double linked list.
Write a program which performs the following operations:
insert x: insert an element with key x into the front of the list.
delete x: delete the first element which has the key of x from the list. If there is not such element, you need not do anything.
deleteFirst: delete the first element from the list.
deleteLast: delete the last element from the list.
Input
The input is given in the following format:
n
command
1
command
2
...
command
n
In the first line, the number of operations
n
is given. In the following
n
lines, the above mentioned operations are given in the following format:
insert x
delete x
deleteFirst
deleteLast
Output
Print all the element (key) in the list after the given operations. Two consequtive keys should be separated by a single space.
Constraints
The number of operations ≤ 2,000,000
The number of delete operations ≤ 20
0 ≤ value of a key ≤ 10
9
The number of elements in the list does not exceed 10
6
For a delete, deleteFirst or deleteLast operation, there is at least one element in the list.
Sample Input 1
7
insert 5
insert 2
insert 3
insert 1
delete 3
insert 6
delete 5
Sample Output 1
6 1 2
Sample Input 2
9
insert 5
insert 2
insert 3
insert 1
delete 3
insert 6
delete 5
deleteFirst
deleteLast
Sample Output 2
1

Submitted solutions:

code_snippet: #include <bits/stdc++.h>

using namespace std;

int n, x;
string s;
list<int> l;

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

    cin >> n;
    while(n--) {
        cin >> s;
        if(s == "insert") {
            cin >> x;
            l.push_front(x);
        }else if(s == "delete") {
            cin >> x;
            for(auto it = l.begin(); it != l.end(); it++) {
                if(*it == x) {
                    l.erase(it);
                    break;
                }
            }
        }else if(s == "deleteFirst") l.pop_front();
        else l.pop_back();
    }

    for(auto it = l.begin(); it != l.end();) {
        cout << *it;
        if(++it != l.end()) cout << ' ';
    }
    cout << '\n';

    return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 34648
final_rank: 7

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define all(x) x.begin(), x.end()


void solaiman(){
   int n; cin >> n;

   deque<int> dq;
   while(n--){
      string s; cin >> s;
      if(s == "insert"){
         int x; cin >> x;
         dq.push_front(x);
      }
      else if(s == "delete"){
         int x; cin >> x;

         if(!dq.empty()){
            auto it = find(dq.begin(), dq.end(), x);

            if(it != dq.end()){
            dq.erase(it);
            }
         }
      }
      else if(s == "deleteFirst"){
         if(!dq.empty()){
            dq.pop_front();
         }
      }
      else{
         if(!dq.empty()){
            dq.pop_back();
         }
      }
   }

   for(size_t i = 0; i < dq.size(); i++){
       cout << dq[i];
       if(i + 1 < dq.size()) cout << ' ';
   }
   cout << '\n';
}

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

    int T = 1;  
    for(int i = 0; i < T; i++){
        solaiman();
    }
    return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 7132
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n; 
    if (!(cin >> n)) return 0;
    list<int> Q;

    for (int i = 0; i < n; ++i) {
        string cmd; 
        cin >> cmd;
        if (cmd == "insert") {
            int x; cin >> x;
            Q.push_front(x);
        } else if (cmd == "delete") {
            int x; cin >> x;
            auto it = find(Q.begin(), Q.end(), x);
            if (it != Q.end()) Q.erase(it); // 最初の1個だけ削除
        } else if (cmd == "deleteFirst") {
            if (!Q.empty()) Q.pop_front();
        } else if (cmd == "deleteLast") {
            if (!Q.empty()) Q.pop_back();
        }
    }

    // 出力：要素間のみスペース、末尾は改行のみ
    for (auto it = Q.begin(); it != Q.end(); ) {
        cout << *it;
        if (++it != Q.end()) cout << ' ';
    }
    cout << '\n';
    return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 34620
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cin >> n;
    list<int> l;

    for (int i = 0; i < n; i++) {
        string cmd;
        cin >> cmd;

        if (cmd == "insert") 
        {
            int x;
            cin >> x;
            l.push_front(x); // 先頭に挿入
        }

        else if (cmd == "delete") 
        {
            int x;
            cin >> x;
            auto it = find(l.begin(), l.end(), x);//検索
            if (it != l.end()) 
            {
                l.erase(it);//削除
            }
        }

        else if (cmd == "deleteFirst") 
        {
            if (!l.empty()) 
            {
                l.pop_front();
            }
        }

        else if (cmd == "deleteLast") 
        {
            if(!l.empty()) 
            {
                l.pop_back();
            }
        }
    }

    bool first = true;
    for (int x : l) {
        if (!first) cout << " ";
        cout << x;
        first = false;
    }
    cout << endl;

    return 0;
}


// vectorを使わない理由
//vector<int> v = {2, 3, 4};
//v.insert(v.begin(), 1); // 先頭に 1 を追加 → 既存要素を全て後ろに移動
accuracy: 1
time_ms: 550
memory_kb: 34588
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cin >> n;
    list<int> l;

    for (int i = 0; i < n; i++) {
        string cmd;
        cin >> cmd;
        if (cmd == "insert") {
            int x;
            cin >> x;
            l.push_front(x); // 先頭に挿入
        }
        else if (cmd == "delete") {
            int x;
            cin >> x;
            auto it = find(l.begin(), l.end(), x);
            if (it != l.end()) l.erase(it);
        }
        else if (cmd == "deleteFirst") {
            if (!l.empty()) l.pop_front();
        }
        else if (cmd == "deleteLast") {
            if (!l.empty()) l.pop_back();
        }
    }

    bool first = true;
    for (int x : l) {
        if (!first) cout << " ";
        cout << x;
        first = false;
    }
    cout << "\n";

    return 0;
}
accuracy: 1
time_ms: 550
memory_kb: 34612
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    list<int> l;
    while(t--){
        string op;
        cin >> op;
        if (op == "insert"){
            int x;
            cin>>x;
            l.push_front(x);
        }else if(op == "delete"){
            int x;
            cin >> x;
            auto it = find(l.begin(),l.end(),x);
            if (it != l.end()) l.erase(it);
        }else if (op == "deleteFirst"){
            l.pop_front();
        }else if (op == "deleteLast"){
            l.pop_back();
        }
    }
    for (auto it = l.begin();it!=l.end();it++){
        if(it != l.begin()) cout<<" ";
        cout<<*it;
    }
    cout<<'\n';
}
accuracy: 1
time_ms: 190
memory_kb: 34404
final_rank: 5

code_snippet: #include <iostream>
#include <string>
using namespace std;

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

Node* sentinel;

void init(){
    sentinel = new Node();
    sentinel->next = sentinel;
    sentinel->prev = sentinel;
}

Node* listSearch(unsigned int key){
    Node* current = sentinel->next;
    while (current != sentinel){
        if (current->key == key) return current; // ← ここを == に修正
        current = current->next;
    }
    return sentinel;
}

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

void deleteFirst(){
    Node* t = sentinel->next;
    if (t == sentinel) return;
    deleteNode(t);
}

void deleteLast(){
    Node* t = sentinel->prev;
    if (t == sentinel) return;
    deleteNode(t);
}

void deleteKey(unsigned int key) {
    Node* t = listSearch(key);
    if (t != sentinel) deleteNode(t);
}

void insertFront(unsigned int key) {
    Node* x = new Node();
    x->key = key;

    // sentinel の直後（先頭）に挿入
    x->next = sentinel->next;   // (1) x の右隣は旧先頭
    x->prev = sentinel;         // (2) x の左隣は sentinel
    sentinel->next->prev = x;   // (3) 旧先頭の左隣を x に
    sentinel->next = x;         // (4) sentinel の右隣を x に（= x が新先頭）
}

void printList() {
    Node* cur = sentinel->next;
    bool first = true;
    while (cur != sentinel) {
        if (!first) cout << ' ';
        cout << cur->key;
        first = false;
        cur = cur->next;
    }
    cout << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    init();

    for (int i = 0; i < n; ++i) {
        string com;
        cin >> com;

        if (com == "insert") {
            unsigned int x; cin >> x;
            insertFront(x);
        } 
        else if (com == "delete") {
            unsigned int x; cin >> x;
            deleteKey(x);
        } 
        else if (com == "deleteFirst") {
            deleteFirst();
        } 
        else if (com == "deleteLast") {
            deleteLast();
        }
    }

    printList();
    delete sentinel;
    return 0;
}
accuracy: 1
time_ms: 180
memory_kb: 34644
final_rank: 4

code_snippet: #include <iostream>
#include <list>
#include <string>
using namespace std;

class Solution {
public:
    int n;
    list<int> lst;
    Solution() {
        cin >> n;
    }
    ~Solution() { }

    void solution() {
        for (int i = 0; i < n; ++i) {
            string str;
            int val;
            cin >> str;
            if (str == "insert") {
                cin >> val;
                lst.push_front(val);
            } else if (str == "deleteFirst") {
                lst.pop_front();
            } else if (str == "deleteLast") {
                lst.pop_back();
            } else if (str == "delete") {
                cin >> val;
                delete_node(val);
            }
        }
        print();
    }
    void delete_node(int val) {
        for (list<int>::iterator it = lst.begin(); it != lst.end(); ++it) {
            if (*it == val) {
                lst.erase(it);
                break;
            }
        }
    }
    void print() {
        for (auto it = lst.begin(); it != lst.end(); ++it) {
            list<int>::iterator next = it;
            if ((++next) != lst.end()) {
                cout << *it << ' ';
            } else {
                cout << *it << '\n';
            }
        }
    }
} ;
int main() {
    Solution solute;
    solute.solution();
    return 0;
}
accuracy: 1
time_ms: 550
memory_kb: 34688
final_rank: 17

code_snippet: #include <iostream>
#include <list>
#include <string>
using namespace std;

class Solution {
public:
    int n;
    list<int> lst;
    Solution() {
        cin >> n;
    }
    ~Solution() { }

    void solution() {
        for (int i = 0; i < n; ++i) {
            string str;
            int val;
            cin >> str;
            if (str == "insert") {
                cin >> val;
                lst.push_front(val);
            } else if (str == "deleteFirst") {
                lst.pop_front();
            } else if (str == "deleteLast") {
                lst.pop_back();
            } else if (str == "delete") {
                cin >> val;
                delete_node(val);
            }
        }
        print();
    }
    void delete_node(int val) {
        list<int>::iterator it;
        for (it = lst.begin(); it != lst.end(); ++it) {
            if (*it == val) {
                lst.erase(it);
                break;
            }
        }
    }
    void print() {
        for (auto it = lst.begin(); it != lst.end(); ++it) {
            list<int>::iterator next = it;
            if ((++next) != lst.end()) {
                cout << *it << ' ';
            } else {
                cout << *it << '\n';
            }
        }
    }
} ;
int main() {
    Solution solute;
    solute.solution();
    return 0;
}
accuracy: 1
time_ms: 550
memory_kb: 34572
final_rank: 14

code_snippet: #include <iostream>
using namespace std;

class Solution {
public:
    struct Node {
        int value;
        Node *prev, *next;
        Node() {}
        Node(int V) : value(V), prev(nullptr), next(nullptr) {}
        Node(int V, Node *P, Node *N) : value(V), prev(P), next(N) {}
    } ;
    int n;
    Node *head, *tail;
    Solution() {
        cin >> n;
        head = nullptr, tail = nullptr;
    }
    ~Solution() { }

    void solution() {
        for (int i = 0; i < n; ++i) {
            string str;
            int val;
            cin >> str;
            if (str == "insert") {
                cin >> val;
                insert(val);
            } else if (str == "deleteFirst") {
                delete_first();
            } else if (str == "deleteLast") {
                delete_last();
            } else if (str == "delete") {
                cin >> val;
                delete_node(val);
            }
        }
        print();
    }
    void insert(int val) {
        Node *node = new Node(val, nullptr, head);
        if (head == nullptr) {
            head = node;
            tail = node;
        } else {
            head = node;
            head->next->prev = head;
        }
    }
    void delete_node(int val) {
        Node *node(head);
        while (node != nullptr && node->value != val) {
            node = node->next;
        }
        if (node == nullptr) {
            return;
        }
        if (node == head) {
            delete_first();
        } else if (node == tail) {
            delete_last();
        } else {
            node->prev->next = node->next;
            node->next->prev = node->prev;
            delete node;
        }
    }
    void delete_first() {
        Node *node = head;
        head = node->next;
        if (head != nullptr) {
            head->prev = nullptr;
        } else {
            tail = nullptr;
        }
        delete node;
    }
    void delete_last() {
        Node *node = tail;
        tail = node->prev;
        if (tail != nullptr) {
            tail->next = nullptr;
        } else {
            head = nullptr;
        }
        delete node;
    }
    void print() {
        Node *node = head;
        while (node->next != nullptr) {
            cout << node->value << ' ';
            node = node->next;
        }
        cout << node->value << '\n';
    }
} ;
int main() {
    Solution solute;
    solute.solution();
    return 0;
}
accuracy: 1
time_ms: 550
memory_kb: 34452
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, s, n) for (ll i = (ll)(s); i < (ll)(n); i++)


// vectorを出力する
void vec1print(vector<ll> vec) {
    rep(i, vec.size()) {
        cout << vec[i] << endl;
    }
}
void vec2print(vector<ll> vec) {
    rep(i, vec.size()) {
        cout << vec[i] << " ";
    }
    cout << endl;
}

// 最大か最小を求める
int minval(vector<ll> vec) {
    return *min_element(begin(vec), end(vec));
}
int maxval(vector<ll> vec) {
    return *max_element(begin(vec), end(vec));
}

// グリット用
vector<ll> dx = {1, 0, -1, 0};
vector<ll> dy = {0, 1, 0, -1};
bool in_grid(ll x, ll y, ll limit_x, ll limit_y) {
    return 0 <= x and x < limit_x and 0 <= y and y < limit_y;
}
struct linked_list {
    ll prev;
    ll next;
    ll key;
};

linked_list l[2000000];
ll head = -1;
ll tail = -1;
ll unused = 0;

void insert(ll x) {
    if (head == -1) {
        l[0].key = x;
        l[0].prev = -1;
        l[0].next = -1;
        head = 0;
        tail = 0;
        unused = 1;
    }
    else {
        l[unused].key = x;
        l[unused].prev = -1;
        l[unused].next = head;
        l[head].prev = unused;
        head = unused;
        unused++;
    }
}

void delete_node(ll x) {
    ll target = head;
    while (target != -1) {
        if (l[target].key == x) {
            ll prev = l[target].prev;
            ll next = l[target].next;

            if (prev != -1) {
                l[prev].next = next;
            }
            else {
                head = next;
            }
            if (next != -1) {
                l[next].prev = prev;
            }
            else {
                tail = prev;
            }
            break;
        }
        target = l[target].next;
    }
}

void deleteFirst() {
    if (head == -1) return;
    ll next = l[head].next;
    if (next != -1) {
        l[next].prev = -1;
    }
    else {
        tail = -1;
    }
    head = next;
}

void deleteLast() {
    if (tail == -1) return;
    ll prev = l[tail].prev;
    if (prev != -1) {
        l[prev].next = -1;
    }
    else {
        head = -1;
    }
    tail = prev;
}

void print_list() {
    if (head == -1) return;
    ll target = head;
    while (target != -1) {
        cout << l[target].key;
        target = l[target].next;
        if (target != -1) cout << " ";
    }
    cout << endl;
}


// main
void solve() {
    ll n;
    cin >> n;
    rep(i,n) {
        string s;
        ll x;
        cin >> s;
        if (s == "insert") {
            cin >> x;
            insert(x);
        }
        else if (s == "delete") {
            cin >> x;
            delete_node(x);
        }
        else if (s == "deleteFirst") {
            deleteFirst();
        }
        else if (s == "deleteLast") {
            deleteLast();
        }
    }
    print_list();

    return;
}

int main() {
    solve();
    return 0;
}
accuracy: 1
time_ms: 380
memory_kb: 30140
final_rank: 8

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
long long ar[200005];
int main()
{
  optimize();
  long long n,m,i;
  cin>>n;
  list<long long>li;
  while(n--)
  {
    string s;
    cin>>s;
    if(s=="insert")
    {
      cin>>m;
      li.push_front(m);
    }
    else if(s=="delete")
    {
      cin>>m;
      auto it=find(li.begin(),li.end(),m);
      if(it != li.end())
      {
        li.erase(it);
      }
    }
    else if(s=="deleteFirst")
    {
      li.pop_front();
    }
    else if (s=="deleteLast")
    {
      li.pop_back();
    }
  }
  for(auto it=li.begin(); it!=li.end();it++)
  {
    if(it!=li.begin())
    {
      cout<<" ";
    }
    cout<<*it;
  }
  cout<<"\n";
  return 0;
}
accuracy: 1
time_ms: 180
memory_kb: 34412
final_rank: 3

code_snippet: # include <iostream>
# include <list>
using namespace std;

int main() {
    
    list<int> lst;

    int n; cin >> n;
    while (n--) {
        string cmd; cin >> cmd;
        if (cmd == "insert") {  //头部插入节点
            int x; cin >> x;
            lst.insert(lst.begin(), x);
        }
        else if (cmd == "delete") {   //删除第一个值为x的节点
            int x; cin >> x;
            //遍历list,list不支持随机访问必须用迭代器遍历
            for (auto it = lst.begin(); it != lst.end(); it++)
            {
                if (*it == x) {
                    //迭代器删除
                    //不能用值删除，会删除所有val==x的节点
                    lst.erase(it);
                    break;
                }
            }
        }
        else if (cmd == "deleteFirst") {    //删除头部元素
            lst.pop_front();
        }
        else {  //删除尾部元素
            lst.pop_back();
        }
    }

    for (auto it = lst.begin(); it != lst.end(); it++)
    {
        if (it != lst.begin()) cout << " ";
        cout << *it;
    }
    cout << endl;
}
accuracy: 1
time_ms: 550
memory_kb: 34288
final_rank: 12

code_snippet: using namespace std;
#include <iostream>
#include <string>
#include <vector>
struct TNode
{
    TNode *Next;
    TNode *Previous;
    int Element;
};
class TList
{
private:
    TNode *Top = nullptr;
    TNode *Last = nullptr;

public:
    void Insert(int &Element)
    {
        TNode *NewNodeptr = new TNode{Top, nullptr, Element};
        if (Top == nullptr && Last == nullptr)
        { /// 初期状態の処理を記述
            Last = NewNodeptr;
        }
        else
        {
            Top->Previous = NewNodeptr;
        }
        Top = NewNodeptr;
    }
    void DeleteOperation(TNode *Target)
    {
        if (Target == nullptr)
        {
            return;
        }
        if (Target == Top)
        {
            Top = Top->Next;
        }
        else
        {
            Target->Previous->Next = Target->Next;
        }
        if (Target == Last)
        {
            Last = Last->Previous;
        }
        else
        {
            Target->Next->Previous = Target->Previous;
        }
        delete Target;
        return;
    }
    TNode *Find(int Element)
    {
        for (TNode *Current = Top; Current != nullptr; Current = Current->Next)
        {
            if (Current->Element == Element)
            {
                return Current;
            }
        }
        return nullptr;
    }
    void Delete(int &Element)
    {
        DeleteOperation(Find(Element));
    }
    void DeleteFirst()
    {
        DeleteOperation(Top);
    }
    void DeleteLast()
    {
        DeleteOperation(Last);
    }
    void ShowAllElements()
    {
        if (Top == nullptr || Last == nullptr)
        {
            return;
        }
        while (Top->Next != nullptr)
        {
            cout << Top->Element << " ";
            Top = Top->Next;
        }
        cout << Top->Element << "\n";
    }
}
;

int main()
{
    int N;
    cin >> N;
    TList list;
    for (int index = 0; index < N; index++)
    {
        string Command;
        cin >> Command;
        if (Command == "insert")
        {
            int Element;
            cin >> Element;
            list.Insert(Element);
        }
        else if (Command == "delete")
        {
            int Element;
            cin >> Element;
            list.Delete(Element);
        }
        else if (Command == "deleteFirst")
        {
            list.DeleteFirst();
        }
        else
        {
            list.DeleteLast();
        }
    }
    list.ShowAllElements();
}
accuracy: 1
time_ms: 410
memory_kb: 34816
final_rank: 10

code_snippet: #include <bits/stdc++.h>


template <typename T>
class Node{
    public:
        T value_;
        Node<T>* prev_;
        Node<T>* next_;

        Node(T v, Node<T>* prev, Node<T>* next): value_(v), prev_(prev), next_(next){} 
};

template <typename T>
class List{
    Node<T> lSentinel = Node<T>(T(),nullptr, nullptr);
    Node<T> rSentinel = Node<T>(T(),nullptr, nullptr);

    public:
        List(){
            lSentinel.next_ = &rSentinel;
            rSentinel.prev_ = &lSentinel;
        }
        
        void push_front(T value){
            Node<T>* newNode = new Node<T>(value, &lSentinel, lSentinel.next_);

            lSentinel.next_->prev_ = newNode;
            lSentinel.next_ = newNode;
        }

        void push_back(T value){
            Node<T>* newNode = new Node<T>(value, rSentinel.prev_, &rSentinel);

            rSentinel.prev_->next_ =newNode;
            rSentinel.prev_ = newNode;
        }

        void delete_node(T v){
            Node<T>* node = lSentinel.next_;
            while(node != &rSentinel){
                if(node->value_ == v){
                    node->prev_->next_ = node->next_;
                    node->next_->prev_ = node->prev_;
                    delete node;
                    break;
                }
                node = node->next_;
            }
        }

        void pop_front(){
            if(lSentinel.next_ == &rSentinel) return;

            Node<T>* target = lSentinel.next_;
            lSentinel.next_ = target->next_;
            target->next_->prev_ = &lSentinel;
            delete target;
        }

        void pop_back(){
            if(rSentinel.prev_ == &lSentinel) return;

            Node<T>* target = rSentinel.prev_;
            rSentinel.prev_ = target->prev_;
            target->prev_->next_ = &rSentinel;
            delete target;
        }

        class Itarator{
            Node<T>* node_;
            public:
            Itarator(Node<T>* node):node_(node){}
            T& operator*(){return node_->value_;}
            Itarator& operator++(){node_ =  node_->next_; return *this;}
            bool operator!=(const Itarator& other)const {return node_ != other.node_;}
        };

        Itarator begin(){return Itarator(lSentinel.next_);}
        Itarator end(){return Itarator(&rSentinel);}
};

int main(){
    int n;
    std::cin >> n;
    List<long long> list;
    for (int i = 0; i < n; i++){
        std::string c; std::cin >> c;
        if(c =="insert"){
            int x; std::cin >> x;
            list.push_front(x);
        }else if(c == "delete"){
            int x; std::cin >> x;
            list.delete_node(x);
        }else if(c == "deleteFirst"){
            list.pop_front();
        }else if(c == "deleteLast"){
            list.pop_back();
        }
    }
    std::string ans;
    for(auto value: list){
        ans += std::to_string(value) + " ";
    }
    ans.pop_back();
    std::cout << ans << std::endl;
}
accuracy: 1
time_ms: 550
memory_kb: 34724
final_rank: 18

code_snippet: #include <cstdio>

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

Node* nil = nullptr;

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

void init() {
    nil = new Node();
    nil->next = nil;
    nil->prev = nil;
}

void printList() {
    Node* cur = nil->next;
    bool first = true;
    while (cur != nil) {
        if (!first) printf(" ");
        printf("%d", cur->key);
        first = false;
        cur = cur->next;
    }
    printf("\n");
}

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

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

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

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

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

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

    init();

    for (int i = 0; i < n; i++) {
        char com[20];
        int key;
        scanf("%s", com);

        if (com[0] == 'i') {
            scanf("%d", &key);
            insert(key);
        } else if (com[0] == 'd') {
            if (com[6] == 'F') {
                deleteFirst();
            } else if (com[6] == 'L') {
                deleteLast();
            } else {
                scanf("%d", &key);
                deleteKey(key);
            }
        }
    }

    printList();

    // 最後に番兵も解放
    delete nil;

    return 0;
}
accuracy: 1
time_ms: 180
memory_kb: 34032
final_rank: 2

code_snippet: #include <bits/stdc++.h>
#include <cstddef>
using namespace std;

const int CAPACITY = 2000000;

template <typename T> struct DataLink {
    T data;
    DataLink *pre;
    DataLink *next;

    DataLink() : data{}, pre{nullptr}, next{nullptr} {}
    DataLink(T i, DataLink *pre, DataLink *next)
        : data{i}, pre{pre}, next{next} {}
};

template <typename T> struct LinkedList {
    array<DataLink<T>, CAPACITY> link_stack;
    int stack_tail = 0;
    DataLink<T> *first = nullptr;
    DataLink<T> *last = nullptr;
    int count = 0;

    void scan_pre(DataLink<T> *&d) { d = d->pre; }
    void scan_next(DataLink<T> *&d) { d = d->next; }

    bool is_empty() { return count == 0; }

    void insert(const T &data) {
        link_stack[stack_tail] = (DataLink<T>(data, nullptr, first));
        DataLink<T> *new_data = &link_stack[stack_tail];
        stack_tail++;

        if (is_empty()) {
            last = new_data;
        } else {
            first->pre = new_data;
        }
        first = new_data;
        count++;
    }

    void del_first() {
        if (count == 1) {
            first = nullptr;
            last = nullptr;
        } else {
            first->next->pre = nullptr;
            first = first->next;
        }
        count--;
    }
    void del_last() {
        if (count == 1) {
            first = nullptr;
            last = nullptr;
        } else {
            last->pre->next = nullptr;
            last = last->pre;
        }
        count--;
    }
    void del(const T &del_data) {
        DataLink<T> *seek = first;
        while (seek->data != del_data) {
            scan_next(seek);
            if (seek == nullptr) {
                return;
            }
        }

        if (seek == first) {
            del_first();
        } else if (seek == last) {
            del_last();
        } else {
            seek->pre->next = seek->next;
            seek->next->pre = seek->pre;
            count--;
        }
    }
};

int main() {
    int n;
    cin >> n;
    LinkedList<int> l;
    for (int i = 0; i < n; i++) {
        char buf[15];
        int key;
        scanf("%s", buf);
        string command = buf;
        if (command == "insert") {
            cin >> key;
            l.insert(key);
        } else if (command == "delete") {
            cin >> key;
            l.del(key);
        } else if (command == "deleteFirst")
            l.del_first();
        else if (command == "deleteLast")
            l.del_last();
    }

    DataLink<int> *seek = l.first;
    for (int i = 0; i < l.count; i++) {
        if (i == l.count - 1)
            cout << seek->data << endl;
        else
            cout << seek->data << " ";

        if (seek->next != nullptr)
            l.scan_next(seek);
    }
}
accuracy: 1
time_ms: 300
memory_kb: 50360
final_rank: 11

code_snippet: #include <bits/stdc++.h>
#include <cstddef>
using namespace std;

const int CAPACITY = 100000;

template <typename T> struct DataLink {
    T data;
    DataLink *pre;
    DataLink *next;

    DataLink() : data{}, pre{nullptr}, next{nullptr} {}
    DataLink(T i, DataLink *pre, DataLink *next)
        : data{i}, pre{pre}, next{next} {}
};

template <typename T> struct LinkedList {
    array<DataLink<T>, CAPACITY> link_stack;
    int stack_tail = 0;
    DataLink<T> *first = nullptr;
    DataLink<T> *last = nullptr;
    int count = 0;

    void scan_pre(DataLink<T> *&d) { d = d->pre; }
    void scan_next(DataLink<T> *&d) { d = d->next; }

    bool is_empty() { return count == 0; }

    void insert(const T &data) {
        link_stack[stack_tail] = (DataLink<T>(data, nullptr, first));
        DataLink<T> *new_data = &link_stack[stack_tail];
        stack_tail++;

        if (is_empty()) {
            last = new_data;
        } else {
            first->pre = new_data;
        }
        first = new_data;
        count++;
    }

    void del_first() {
        if (count == 1) {
            first = nullptr;
            last = nullptr;
        } else {
            first->next->pre = nullptr;
            first = first->next;
        }
        count--;
    }
    void del_last() {
        if (count == 1) {
            first = nullptr;
            last = nullptr;
        } else {
            last->pre->next = nullptr;
            last = last->pre;
        }
        count--;
    }
    void del(const T &del_data) {
        DataLink<T> *seek = first;
        while (seek->data != del_data) {
            scan_next(seek);
            if (seek == nullptr) {
                return;
            }
        }

        if (seek == first) {
            del_first();
        } else if (seek == last) {
            del_last();
        } else {
            seek->pre->next = seek->next;
            seek->next->pre = seek->pre;
            count--;
        }
    }
};

int main() {
    int n;
    cin >> n;
    LinkedList<int> l;
    for (int i = 0; i < n; i++) {
        char buf[15];
        int key;
        scanf("%s", buf);
        string command = buf;
        if (command == "insert") {
            cin >> key;
            l.insert(key);
        } else if (command == "delete") {
            cin >> key;
            l.del(key);
        } else if (command == "deleteFirst")
            l.del_first();
        else if (command == "deleteLast")
            l.del_last();
    }

    DataLink<int> *seek = l.first;
    for (int i = 0; i < l.count; i++) {
        if (i == l.count - 1)
            cout << seek->data << endl;
        else
            cout << seek->data << " ";

        if (seek->next != nullptr)
            l.scan_next(seek);
    }
}
accuracy: 0.9
time_ms: 230
memory_kb: 5824
final_rank: 20

code_snippet: using namespace std;
#include <iostream>
#include <string>
#include <vector>
struct TNode
{
    TNode *Next;
    TNode *Previous;
    int Element;
};
class TList
{
private:
    TNode *Top = nullptr;
    TNode *Last = nullptr;

public:
    void Insert(int &Element)
    {
        TNode *NewNodeptr = new TNode{Top, nullptr, Element};
        if (Top == nullptr && Last == nullptr)
        { /// 初期状態の処理を記述
            Last = NewNodeptr;
        }
        else
        {
            Top->Previous = NewNodeptr;
        }
        Top = NewNodeptr;
    }
    void DeleteOperation(TNode *Target)
    {
        if (Target == nullptr)
        {
            return;
        }
        else
        {
            if (Target == Top)
            {
                Top = Top->Next;
            }
            else
            {
                Target->Previous->Next = Target->Next;
            }
            if (Target == Last)
            {
                Last = Last->Previous;
            }
            else
            {
                Target->Next->Previous = Target->Previous;
            }
            delete Target;
            return;
        }
    }
    TNode *Find(int Element)
    {
        for (TNode *Current = Top; Current != nullptr; Current = Current->Next)
        {
            if (Current->Element == Element)
            {
                return Current;
            }
        }
        return nullptr;
    }
    void Delete(int &Element)
    {
        DeleteOperation(Find(Element));
    }
    void DeleteFirst()
    {
        DeleteOperation(Top);
    }
    void DeleteLast()
    {
        DeleteOperation(Last);
    }
    void ShowAllElements()
    {
        if (Top != nullptr && Last != nullptr)
        {
            while (Top->Next != nullptr)
            {
                cout << Top->Element << " ";
                Top = Top->Next;
            }
            cout << Top->Element << "\n";
        }
    }
};

int main()
{
    int N;
    cin >> N;
    TList list;
    for (int index = 0; index < N; index++)
    {
        string Command;
        cin >> Command;
        if (Command == "insert")
        {
            int Element;
            cin >> Element;
            list.Insert(Element);
        }
        else if (Command == "delete")
        {
            int Element;
            cin >> Element;
            list.Delete(Element);
        }
        else if (Command == "deleteFirst")
        {
            list.DeleteFirst();
        }
        else
        {
            list.DeleteLast();
        }
    }
    list.ShowAllElements();
}
accuracy: 1
time_ms: 410
memory_kb: 34688
final_rank: 9

code_snippet: #include <bits/stdc++.h>


int main(){
    int N;
    std::cin >> N;
    std::list<int> A;
    for (int i = 0; i < N; i++){
        std::string cmd;
        std::cin >> cmd;
        if(cmd == "insert"){
            int v; std::cin >> v;
            A.push_front(v);
        }else if(cmd == "delete"){
            int v; std::cin >> v;
            auto it = std::find(A.begin(), A.end(), v);
            if(it != A.end()) A.erase(it);
        }else if( cmd == "deleteFirst"){
            A.pop_front();
        }else if( cmd == "deleteLast"){
            A.pop_back();
        }
    }

    std::string ans = "";
    for(auto v: A){
        ans +=  std::to_string(v) + " ";
    } 
    ans.pop_back();
    std::cout << ans << std::endl;
}
accuracy: 1
time_ms: 560
memory_kb: 34416
final_rank: 19