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

command1 

command2 

...

commandn 

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 ≤ 109

The number of elements in the list does not exceed 106

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

Template in C
-->


Sample input:
7
insert 5
insert 2
insert 3
insert 1
delete 3
insert 6
delete 5


Sample output:
6 1 2

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-


class Node(object):
    def __init__(self, key, prev=None, nex=None):
        self.key = key
        self.prev = prev
        self.next = nex


class DoublyLinkedLiset(object):
    def __init__(self):
        self.nil = Node(None)
        self.nil.next = self.nil
        self.nil.prev = self.nil

    def insert(self, x):
        X = Node(x)
        X.next = self.nil.next
        self.nil.next.prev = X
        self.nil.next = X
        X.prev = self.nil

    def listSearch(self, x):
        cur = self.nil.next
        while (cur != self.nil and cur.key != x):
            cur = cur.next

        return cur

    def deleteNode(self, X):
        #if X == self.nil:
        #    return
        X.prev.next = X.next
        X.next.prev = X.prev
        del X

    def deleteFirst(self):
        self.deleteNode(self.nil.next)

    def deleteLast(self):
        self.deleteNode(self.nil.prev)

    def deleteKey(self, x):
        X = self.listSearch(x)
        if X == self.nil:
            return
        self.deleteNode(X)

    def printList(self):
        cur = self.nil.next
        ret = []

        while cur.key is not None:
            ret.append(cur.key)
            cur = cur.next

        print(' '.join(ret))


if __name__ == '__main__':

    n = int(input())
    dll = DoublyLinkedLiset()

    for _ in range(n):
        command = input().split(" ")
        if command[0] == "insert":
            dll.insert(command[1])
        elif command[0] == "delete":
            dll.deleteKey(command[1])
        elif command[0] == "deleteFirst":
            dll.deleteFirst()
        elif command[0] == "deleteLast":
            dll.deleteLast()

    dll.printList()

Submission status: Time Limit Exceeded

Solution code:
s=[]
for _ in range(int(input())):
 e=input()
 if'i'==e[0]:s=[e.split()[1]]+s
 else:
  if' '==e[6]:
   m=e.split()[1]
   if m in s:s.remove(m)
  elif len(e)%2:s=s[1:]
  else:s.pop()
print(*s)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
que = []

for i in range(n):
    lineArr = input().split()
    ope = lineArr[0]

    if ope == 'insert':
        num = int(lineArr[1])
        que.insert(0, num)
    elif ope == 'delete':
        num = int(lineArr[1])
        try:
            que.remove(num)
        except ValueError:
            pass
    elif ope == 'deleteFirst':
        que.pop(0)
    else:
        que.pop()

print(*que)

Submission status: Time Limit Exceeded

Solution code:
import collections

n = int(input())
que = collections.deque()

for i in range(n):
    lineArr = input().split()
    ope = lineArr[0]

    if ope == 'insert':
        num = int(lineArr[1])
        que.insert(0, num)
    elif ope == 'delete':
        num = int(lineArr[1])
        try:
            que.remove(num)
        except ValueError:
            pass
    elif ope == 'deleteFirst':
        que.popleft()
    else:
        que.pop()

print(*que)

Submission status: Time Limit Exceeded

Solution code:
import collections

n = int(input())
que = collections.deque()

for i in range(n):
    lineArr = input().split()
    ope = lineArr[0]

    if ope == 'insert':
        num = int(lineArr[1])
        que.appendleft(num)
    elif ope == 'delete':
        num = int(lineArr[1])
        try:
            que.remove(num)
        except ValueError:
            pass
    elif ope == 'deleteFirst':
        que.popleft()
    else:
        que.pop()

print(*que)

Submission status: Time Limit Exceeded

Solution code:
class Node(object):
    def __init__(self, num, prev = None, nxt = None):
        self.num = num;
        self.prev = prev;
        self.nxt = nxt;

    def __repr__(self):
        return str(self.num);

class Double_Linked_List(object):
    def __init__(self):
        self.first = self.last = None;

    def __repr__(self):
        s = "";
        current = self.first;
        while current:
            s += str(current);
            current = current.nxt;
            if current:
                s += " ";
        return s;

    def insert(self, num):
        node = Node(num);
        first = self.first;
        if first:
            first.prev = node;
            node.nxt = first;
            self.first = node;
        else:
            self.first = node;
            self.last = node;

    def delete(self, num):
        current = self.first;
        while current:
            if current.num == num:
                prev = current.prev;
                nxt = current.nxt;
                if prev:
                    current.prev = None;
                    prev.nxt = nxt;
                if nxt:
                    current.nxt = None;
                    nxt.prev = prev;
                if not nxt:
                    self.last = prev;
                if not prev:
                    self.first = nxt;
                break;
            else:
                current = current.nxt;

    def delete_first(self):
        first = self.first;
        if first:
            nxt = first.nxt;
            if nxt:
                self.first = nxt;
                nxt.prev = None;
                first.nxt = None;
            else:
                self.first = None;
                self.last = None;

    def delete_last(self):
        last = self.last;
        if last:
            prev = last.prev;
            if prev:
                prev.nxt = None;
                last.prev = None;
                self.last = prev;
            else:
                self.first = None;
                self.last = None;

def main():
    double_linked_list = Double_Linked_List();
    count = int(input());
    for i in range(count):
        command = input().split();
        command_name = command[0];

        if command_name == "deleteFirst":
            double_linked_list.delete_first();
        elif command_name == "deleteLast":
            double_linked_list.delete_last();
        elif command_name == "delete":
            num = int(command[1]);
            double_linked_list.delete(num);
        elif command_name == "insert":
            num = int(command[1]);
            double_linked_list.insert(num);
    print(double_linked_list);

main();

Submission status: Time Limit Exceeded

Solution code:
class Node(object):
    def __init__(self, num, prev = None, nxt = None):
        self.num = num;
        self.prev = prev;
        self.nxt = nxt;

    def __repr__(self):
        return str(self.num);

class Double_Linked_List(object):
    def __init__(self):
        self.first = self.last = None;

    def __repr__(self):
        s = "";
        current = self.first;
        while current:
            s += str(current.num);
            current = current.nxt;
            if current:
                s += " ";
        return s;

    def insert(self, num):
        node = Node(num);
        first = self.first;
        if first:
            first.prev = node;
            node.nxt = first;
            self.first = node;
        else:
            self.first = node;
            self.last = node;

    def delete(self, num):
        current = self.first;
        while current:
            if current.num == num:
                prev = current.prev;
                nxt = current.nxt;
                if prev:
                    current.prev = None;
                    prev.nxt = nxt;
                if nxt:
                    current.nxt = None;
                    nxt.prev = prev;
                if not nxt:
                    self.last = prev;
                if not prev:
                    self.first = nxt;
                break;
            else:
                current = current.nxt;

    def delete_first(self):
        first = self.first;
        if first:
            nxt = first.nxt;
            if nxt:
                self.first = nxt;
                nxt.prev = None;
                first.nxt = None;
            else:
                self.first = None;
                self.last = None;

    def delete_last(self):
        last = self.last;
        if last:
            prev = last.prev;
            if prev:
                prev.nxt = None;
                last.prev = None;
                self.last = prev;
            else:
                self.first = None;
                self.last = None;

def main():
    double_linked_list = Double_Linked_List();
    count = int(input());
    for i in range(count):
        command = input().split();
        command_name = command[0];

        if command_name == "deleteFirst":
            double_linked_list.delete_first();
        elif command_name == "deleteLast":
            double_linked_list.delete_last();
        elif command_name == "delete":
            num = int(command[1]);
            double_linked_list.delete(num);
        elif command_name == "insert":
            num = int(command[1]);
            double_linked_list.insert(num);
    print(double_linked_list);

main();

Submission status: Time Limit Exceeded

Solution code:
class Node(object):
    def __init__(self, num, prev = None, nxt = None):
        self.num = num;
        self.prev = prev;
        self.nxt = nxt;

    def __repr__(self):
        return str(self.num);

class Double_Linked_List(object):
    def __init__(self):
        self.first = self.last = None;

    def insert(self, num):
        node = Node(num);
        first = self.first;
        if first:
            first.prev = node;
            node.nxt = first;
            self.first = node;
        else:
            self.first = node;
            self.last = node;

    def delete(self, num):
        current = self.first;
        while current:
            if current.num == num:
                prev = current.prev;
                nxt = current.nxt;
                if prev:
                    current.prev = None;
                    prev.nxt = nxt;
                if nxt:
                    current.nxt = None;
                    nxt.prev = prev;
                if not nxt:
                    self.last = prev;
                if not prev:
                    self.first = nxt;
                break;
            else:
                current = current.nxt;

    def delete_first(self):
        first = self.first;
        if first:
            nxt = first.nxt;
            if nxt:
                self.first = nxt;
                nxt.prev = None;
                first.nxt = None;
            else:
                self.first = None;
                self.last = None;

    def delete_last(self):
        last = self.last;
        if last:
            prev = last.prev;
            if prev:
                prev.nxt = None;
                last.prev = None;
                self.last = prev;
            else:
                self.first = None;
                self.last = None;

    def get_content(self):
        ret = [];
        current = self.first;
        while current:
            ret.append(current.num);
            current = current.nxt;
        return " ".join(ret);

def main():
    double_linked_list = Double_Linked_List();
    count = int(input());
    for i in range(count):
        command = input().split();
        command_name = command[0];

        if command_name == "deleteFirst":
            double_linked_list.delete_first();
        elif command_name == "deleteLast":
            double_linked_list.delete_last();
        elif command_name == "delete":
            num = command[1];
            double_linked_list.delete(num);
        elif command_name == "insert":
            num = command[1];
            double_linked_list.insert(num);
    print(double_linked_list.get_content());

main();

Submission status: Time Limit Exceeded

Solution code:
class Node(object):
    def __init__(self, num, prev = None, nxt = None):
        self.num = num;
        self.prev = prev;
        self.nxt = nxt;

    def __repr__(self):
        return str(self.num);

class Double_Linked_List(object):
    def __init__(self):
        self.first = self.last = None;

    def insert(self, num):
        node = Node(num);
        first = self.first;
        if first is not None:
            first.prev = node;
            node.nxt = first;
            self.first = node;
        else:
            self.first = node;
            self.last = node;

    def delete(self, num):
        current = self.first;
        while current is not None:
            if current.num == num:
                prev = current.prev;
                nxt = current.nxt;
                if prev is not None:
                    current.prev = None;
                    prev.nxt = nxt;
                if nxt is not None:
                    current.nxt = None;
                    nxt.prev = prev;
                if nxt is None:
                    self.last = prev;
                if prev is None:
                    self.first = nxt;
                break;
            else:
                current = current.nxt;

    def delete_first(self):
        first = self.first;
        if first is not None:
            nxt = first.nxt;
            if nxt is not None:
                self.first = nxt;
                nxt.prev = None;
                first.nxt = None;
            else:
                self.first = None;
                self.last = None;

    def delete_last(self):
        last = self.last;
        if last is not None:
            prev = last.prev;
            if prev is not None:
                prev.nxt = None;
                last.prev = None;
                self.last = prev;
            else:
                self.first = None;
                self.last = None;

    def get_content(self):
        ret = [];
        current = self.first;
        while current is not None:
            ret.append(current.num);
            current = current.nxt;
        return " ".join(ret);

def main():
    double_linked_list = Double_Linked_List();
    count = int(input());
    for i in range(count):
        command = input().split();
        command_name = command[0];

        if command_name == "deleteFirst":
            double_linked_list.delete_first();
        elif command_name == "deleteLast":
            double_linked_list.delete_last();
        elif command_name == "delete":
            num = command[1];
            double_linked_list.delete(num);
        elif command_name == "insert":
            num = command[1];
            double_linked_list.insert(num);
    print(double_linked_list.get_content());

main();

Submission status: Time Limit Exceeded

Solution code:
class Node(object):
    def __init__(self, num, prev = None, nxt = None):
        self.num = num;
        self.prev = prev;
        self.nxt = nxt;

    def __repr__(self):
        return str(self.num);

class Double_Linked_List(object):
    def __init__(self):
        self.first = self.last = None;

    def insert(self, num):
        node = Node(num);
        first = self.first;
        if first is not None:
            first.prev = node;
            node.nxt = first;
            self.first = node;
        else:
            self.first = node;
            self.last = node;

    def delete(self, num):
        current = self.first;
        while current is not None:
            if current.num == num:
                prev = current.prev;
                nxt = current.nxt;
                if prev is not None:
                    current.prev = None;
                    prev.nxt = nxt;
                if nxt is not None:
                    current.nxt = None;
                    nxt.prev = prev;
                if nxt is None:
                    self.last = prev;
                if prev is None:
                    self.first = nxt;
                break;
            else:
                current = current.nxt;

    def delete_first(self):
        first = self.first;
        if first is not None:
            nxt = first.nxt;
            if nxt is not None:
                self.first = nxt;
                nxt.prev = None;
                first.nxt = None;
            else:
                self.first = None;
                self.last = None;

    def delete_last(self):
        last = self.last;
        if last is not None:
            prev = last.prev;
            if prev is not None:
                prev.nxt = None;
                last.prev = None;
                self.last = prev;
            else:
                self.first = None;
                self.last = None;

    def get_content(self):
        ret = [];
        append = ret.append;
        current = self.first;
        while current is not None:
            append(current.num);
            current = current.nxt;
        return " ".join(ret);

def main():
    double_linked_list = Double_Linked_List();
    count = int(input());
    delete_first = double_linked_list.delete_first;
    delete_last = double_linked_list.delete_last;
    delete = double_linked_list.delete;
    insert = double_linked_list.insert;
    for i in range(count):
        command = input().split();
        command_name = command[0];

        if command_name == "insert":
            num = command[1];
            insert(num);
        if command_name == "delete":
            num = command[1];
            delete(num);
        elif command_name == "deleteFirst":
            delete_first();
        elif command_name == "deleteLast":
            delete_last();
    print(double_linked_list.get_content());

main();

Submission status: Time Limit Exceeded

Solution code:
class Node(object):
    def __init__(self, num, prev = None, nxt = None):
        self.num = num;
        self.prev = prev;
        self.nxt = nxt;

class Double_Linked_List(object):
    def __init__(self):
        self.first = self.last = None;

    def insert(self, num):
        node = Node(num);
        first = self.first;
        if first is not None:
            first.prev = node;
            node.nxt = first;
            self.first = node;
        else:
            self.first = node;
            self.last = node;

    def delete(self, num):
        current = self.first;
        while current is not None:
            if current.num == num:
                prev = current.prev;
                nxt = current.nxt;
                if prev is not None:
                    current.prev = None;
                    prev.nxt = nxt;
                if nxt is not None:
                    current.nxt = None;
                    nxt.prev = prev;
                if nxt is None:
                    self.last = prev;
                if prev is None:
                    self.first = nxt;
                break;
            else:
                current = current.nxt;

    def delete_first(self):
        first = self.first;
        if first is not None:
            nxt = first.nxt;
            if nxt is not None:
                self.first = nxt;
                nxt.prev = None;
                first.nxt = None;
            else:
                self.first = None;
                self.last = None;

    def delete_last(self):
        last = self.last;
        if last is not None:
            prev = last.prev;
            if prev is not None:
                prev.nxt = None;
                last.prev = None;
                self.last = prev;
            else:
                self.first = None;
                self.last = None;

    def get_content(self):
        ret = [];
        append = ret.append;
        current = self.first;
        while current is not None:
            append(current.num);
            current = current.nxt;
        return " ".join(ret);

def main():
    double_linked_list = Double_Linked_List();
    count = int(input());
    delete_first = double_linked_list.delete_first;
    delete_last = double_linked_list.delete_last;
    delete = double_linked_list.delete;
    insert = double_linked_list.insert;
    for i in range(count):
        command = input().split();
        command_name = command[0];

        if command_name == "insert":
            num = command[1];
            insert(num);
        if command_name == "delete":
            num = command[1];
            delete(num);
        elif command_name == "deleteFirst":
            delete_first();
        elif command_name == "deleteLast":
            delete_last();
    print(double_linked_list.get_content());

main();

Submission status: Time Limit Exceeded

Solution code:
class Node(object):
    def __init__(self, num, prev = None, nxt = None):
        self.num = num;
        self.prev = prev;
        self.nxt = nxt;

class Double_Linked_List(object):
    def __init__(self):
        self.first = self.last = None;

    def insert(self, num):
        node = Node(num);
        if self.first is not None:
            self.first.prev = node;
            node.nxt = self.first;
            self.first = node;
        else:
            self.first = node;
            self.last = node;

    def delete(self, num):
        current = self.first;
        while current is not None:
            if current.num == num:
                prev = current.prev;
                nxt = current.nxt;
                if prev is not None:
                    current.prev = None;
                    prev.nxt = nxt;
                if nxt is not None:
                    current.nxt = None;
                    nxt.prev = prev;
                if nxt is None:
                    self.last = prev;
                if prev is None:
                    self.first = nxt;
                break;
            else:
                current = current.nxt;

    def delete_first(self):
        if self.first is not None:
            nxt = self.first.nxt;
            if nxt is not None:
                self.first.nxt = None;
                nxt.prev = None;
                self.first = nxt;
            else:
                self.first = None;
                self.last = None;

    def delete_last(self):
        if self.last is not None:
            prev = self.last.prev;
            if prev is not None:
                self.last.prev = None;
                prev.nxt = None;
                self.last = prev;
            else:
                self.first = None;
                self.last = None;

    def get_content(self):
        ret = [];
        append = ret.append;
        current = self.first;
        while current is not None:
            append(current.num);
            current = current.nxt;
        return " ".join(ret);

def main():
    double_linked_list = Double_Linked_List();
    count = int(input());
    delete_first = double_linked_list.delete_first;
    delete_last = double_linked_list.delete_last;
    delete = double_linked_list.delete;
    insert = double_linked_list.insert;
    for i in range(count):
        command = input().split();
        if command[0] == "insert":
            insert(command[1]);
        if command[0] == "delete":
            delete(command[1]);
        elif command[0] == "deleteFirst":
            delete_first();
        elif command[0] == "deleteLast":
            delete_last();
    print(double_linked_list.get_content());

main();

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

n=int(input())
command=[0 for i in range(n)]
k=[0 for i in range(n)]
k_queue= deque([])

for i in range(n):
    s=input()
    if s=="deleteFirst" or s=="deleteLast":
        command[i],k[i]=s,0
    else:
        command[i],k[i]=s.split()
    
    if command[i]=="insert":
        k_queue.appendleft(k[i])
    elif command[i]=="deleteFirst":
        k_queue.popleft()
    elif command[i]=="deleteLast":
        k_queue.pop()
    else:
        try: k_queue.remove(k[i])
        except ValueError: y=0
print( " ".join(map(str, k_queue)))

Submission status: Time Limit Exceeded

Solution code:
import collections

#getting input
commands = []
while True:
    try:
        line = str(input())
    except EOFError:
        break
    commands.append(line)
commands.pop(0)

'''while True:
    line = input()
    if line == '': break
    commands.append(line)
commands.pop(0)'''


#executing commands
output = collections.deque()
for command in commands:
    pair = command.split()
    if len(pair) < 2:
        if pair[0] == "deleteFirst":
            try:
                output.popleft()
            except:pass
        else:
            try:
                output.pop()
            except:pass
        continue

    command = pair[0]
    num = pair[1]

    if command == "insert":
        output.appendleft(num)
    else: #delete
        try:
            output.remove(num)
        except:
            pass

outstr = str()
for num in output:
    outstr += (str(num)+' ')
outstr = outstr.strip()
print(outstr)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys

_list = list()

n = int(sys.stdin.readline())
for i in range(n):
    cmd = sys.stdin.readline().split()
    if cmd[0] == 'insert':
        _list.insert(0, cmd[1])
    elif cmd[0] == 'delete':
        try:
            _list.remove(cmd[1])
        except ValueError:
            #print('skip deleting', cmd[1])
            pass
    elif cmd[0] == 'deleteFirst':
        _list.pop(0)
    elif cmd[0] == 'deleteLast':
        _list.pop()
    else:
        raise ValueError
    # print(_list)

print(' '.join(_list))

Submission status: Time Limit Exceeded

Solution code:

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys

_list = list()

n = int(sys.stdin.readline())
for i in range(n):
    cmd = sys.stdin.readline().split()
    if cmd[0] == 'insert':
        # _list.insert(0, cmd[1])
        _list.append(cmd[1])
    elif cmd[0] == 'delete':
        try:
            tmp = _list[::-1]
            tmp.remove(cmd[1])
            _list = tmp[::-1]
        except ValueError:
            #print('skip deleting', cmd[1])
            pass
    elif cmd[0] == 'deleteFirst':
        _list.pop()
    elif cmd[0] == 'deleteLast':
        _list.pop(0)
    else:
        raise ValueError
    # print(_list)

print(' '.join(_list[::-1]))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
L = []
i = 0
while i < n:
    s = list(map(str, input().split(' ')))
    if s[0] == 'insert':
        L.insert(0, int(s[1]))
    elif s[0] == 'delete':
        if int(s[1]) in L:
            L.remove(int(s[1]))
    elif s[0] == 'deleteFirst':
        del L[0]
    elif s[0] == 'deleteLast':
        del L[-1]
    i += 1
print(' '.join(map(str, L)))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
L = []
i = 0
while i < n:
    s = list(map(str, input().split(' ')))
    if s[0] == 'insert':
        L.insert(0, int(s[1]))
    elif s[0] == 'delete':
        try:
            L.remove(int(s[1]))
        except:
            pass
    elif s[0] == 'deleteFirst':
        del L[0]
    elif s[0] == 'deleteLast':
        del L[-1]
    i += 1
print(' '.join(map(str, L)))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

n = int(input())
q = deque([])
i = 0
while i < n:
    s = list(map(str, input().split(' ')))
    if s[0] == 'insert':
        q.appendleft(int(s[1]))
    elif s[0] == 'delete':
        try:
            q.remove(int(s[1]))
        except ValueError:
            pass
    elif s[0] == 'deleteFirst':
        q.popleft()
    elif s[0] == 'deleteLast':
        q.pop()
    i += 1
ans = ''
while len(q) != 0:
    ans += str(q.popleft()) + ' '
print(ans[:-1])

Submission status: Time Limit Exceeded

Solution code:
class DoubleEndedList:
# クラス内クラスを作る(CellクラスはDoubleEndedListクラスからしか参照しないため)
    class Cell:
        def __init__(self, data, prv = None, nxt = None):
            self.data = data
            self.prv = prv
            self.nxt = nxt

    def __init__(self):
        head = DoubleEndedList.Cell(None)
        head.prv = head
        head.nxt = head
        self.head = head

    def insertFirst(self, data):
        x = DoubleEndedList.Cell(data, self.head, self.head.nxt)
        self.head.nxt = x
        x.nxt.prv = x

    def delete(self, index):
        x = self.head.nxt
        while x is not self.head:
            if x.data == index:
                break
            x = x.nxt
        # indexと一致するdataが見つからなかった時
        if x is self.head:
            return
        x.prv.nxt = x.nxt
        x.nxt.prv = x.prv

    def deleteLast(self):
        x = self.head.prv
        x.prv.nxt = x.nxt
        x.nxt.prv = x.prv
deque = DoubleEndedList()
for i in range(int(input())):
    cmd = input().split()
    if cmd[0] == "insert":
        deque.insertFirst(int(cmd[1]))
    elif cmd[0] == "delete":
        deque.delete(int(cmd[1]))
    elif cmd[0] == "deleteFirst":
        deque.delete(deque.head.nxt.data)
    elif cmd[0] == "deleteLast":
        deque.deleteLast()
    else:
        print("Wrong Command")

n = deque.head.nxt
while n is not deque.head:
    print(n.data, end = "")
    if n.nxt is not deque.head:
        print(" ", end = "")
    else:
        print("")
    n = n.nxt