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

Solution in Python3 (judge verdict: Time Limit Exceeded)

import time

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

class MyDLL:
    def __init__(self):
        self.linkedlist = []
        self.length = 0
    
    def isEmpty(self):
        if self.length==0:
            return True
        return False
    
    def insert(self, x):
        # 連結リストの先頭にキー x を持つ要素を継ぎ足す。
        if self.isEmpty():
            # 連結リストが空の時
            new = Node(x, None, None)
            self.linkedlist.insert(0, new)         
        else:
            # 連結リストが空ではない時
            old = self.linkedlist[0] # 今まで先頭にいたノード
            new = Node(x, None, old) # 新しく先頭になるノード
            old.prev = new           # 今まで先頭にいたノードの前に新しいノードを登録
            self.linkedlist.insert(0, new)
        self.length += 1 # 連結リストの長さを更新

    def delete(self, x):
        # キー x を持つ最初の要素を連結リストから削除する。そのような要素が存在しない場合は何もしない。
        for node in self.linkedlist:
            if node.key==x:# キー x を持つ最初の要素    
                if node.prev is None:# 先頭だった時
                    node.next.prev = None # 次の要素の前の要素をNoneにする
                elif node.next is None:# 最後尾だった時
                    node.prev.next = None
                else:# 途中だった時
                    prev_node = node.prev
                    next_node = node.next
                    prev_node.next = next_node
                    next_node.prev = prev_node
                self.linkedlist.remove(node)# listから削除 
                self.length -= 1     
                break # キーxを持つ最初だけで良いのでbreak
        
    def deleteFirst(self):
        # リストの先頭の要素を削除する。
        if not self.isEmpty():
            first = self.linkedlist.pop(0)
            if self.length>1:
                first.next.prev = None
            self.length -= 1 
        else:
            print("List is empty")
        
    def deleteLast(self):
        # リストの末尾の要素を削除する。        
        if not self.isEmpty():
            last = self.linkedlist.pop()
            if self.length>1:
                last.prev.next = None
            self.length -= 1 
        else:
            print("List is empty")
        


def main():
    n = int(input())
    commands = (input().split() for i in range(n))
    commands = ({'order': c[0], 'value': c[1]} if len(c)==2 else {'order': c[0]} for c in commands)
    dll = MyDLL()
    for c in commands:        
        if c['order']=='insert':
            dll.insert(c['value'])
        elif c['order']=='delete':
            dll.delete(c['value'])
        elif c['order']=='deleteFirst':
            dll.deleteFirst()
        elif c['order']=='deleteLast':
            dll.deleteLast()
        else:
            print("the command dont't exist")
    print(' '.join([node.key for node in dll.linkedlist]))


if __name__=='__main__':
    # start = time.time()
    main()
    # elapsed_time = time.time() - start
    # print ("elapsed_time:{0}".format(elapsed_time) + "[sec]")