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

Solution (Python):
import sys

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

class LinkList:
    def __init__(self):
        self.head = None
        self.tail = None

    def insertzero(self,value):
        new  = Node(value)
        p = self.head
        self.head = new
        new.next = p
        if p is None:
            self.tail = new
        else:
            p.prev = new

    def deletefirst(self):
        if self.head is None:
            return
        p = self.head
        if p.next is None:
            self.head = None
            self.tail = None
        else:
            q = p.next
            q.prev = None
            self.head = q

    def deletelast(self):
        if self.tail is None:
            return
        p = self.tail
        q = p.prev
        if q is None:
            self.head =None
            self.tail =None
        else:
            q.next = None
            self.tail = q

    def delete(self,value):
        p = self.head
        while p is not None:
            if p.value == value:
                if p.prev is None:
                    self.deletefirst()
                    return
                elif p.next is None:
                    self.deletelast()
                    return
                else:
                    p.next.prev = p.prev
                    p.prev.next = p.next
                    return
            else:
                p = p.next

    def show(self):
        p = self.head
        
        l = []
        c = 0
        while p is not None:
            l.append(p.value)
            p=p.next
        print(" ".join(l))

    def rshow(self):
        p = self.tail
        c = 0
        while p is not None:
            if c > 0:
                print(" ",end="")
            else:
                c +=1
            print(p.value,end="")
            p = p.prev
        print()

def main():

    list  = LinkList()
    num = int(input())

    for _ in range(num):
        
        line = sys.stdin.readline()
        a = line.rstrip().split()
        if a[0][0] == "i":
            list.insertzero(a[1])
        elif len(a[0]) == 6:
            list.delete(a[1])
        elif len(a[0]) == 11:
            list.deletefirst()
        elif len(a[0]) == 10:
            list.deletelast()
        else:
            pass

    list.show()

if __name__ == '__main__':
    main()

Solution (Python):
import sys
input=sys.stdin.readline
from collections import deque
n=int(input())
l=deque()
for i in range(n):
    c=list(input().split())
    if c[0]=="insert":
        c[1]=int(c[1])
        l.appendleft(c[1])
    elif c[0]=="delete":
        c[1]=int(c[1])
        try:
            l.remove(c[1])
        except:
            pass
    elif c[0]=="deleteFirst":
        a=l.popleft()
    else:
        a=l.pop()

print(*l)

Solution (Python):
from collections import deque
import sys

n = int(input())
li = deque()

def insertitem(item):
    li.appendleft(item)

def deleteitem(item):
    try:
        li.remove(item)
    except ValueError:
        pass

def deletefirst():
    li.popleft()

def deletelast():
    li.pop()

for i in sys.stdin:
    if i[6] == ' ':
        item = i[7:-1]
        if i[5] == 't':
            insertitem(item)
        else:
            deleteitem(item)
    elif i[6] == 'F':
        deletefirst()
    else:
        deletelast()

print(*li)

Solution (Python):
import sys
from collections import deque

def test():

	n = int(sys.stdin.readline())
	DLlist = deque()

	for i in range(n):
	
		command = sys.stdin.readline().split()

		if command[0] == 'insert':
			DLlist.appendleft(int(command[1]))

		elif command[0] == 'deleteFirst':
			DLlist.popleft()

		elif command[0] == 'deleteLast':
			DLlist.pop()

		elif command[0] == 'delete':
			x = int(command[1])
			for j in range(len(DLlist)):
				if DLlist[j] == x:
					DLlist.remove(x)
					break
				

	for i in range(len(DLlist)-1):
		print(f"{DLlist[i]} ",end='')
	print(f"{DLlist[len(DLlist)-1]}")
					

if __name__ == "__main__":
	test()

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


class DoublyLinkedList:
    def __init__(self):
        self.head = None
        self.tail = None

    def insert(self, key):
        node = Node(key)

        if self.head is None:
            self.head = self.tail = node
        else:
            node.next = self.head
            self.head.prev = node
            self.head = node

    def deleteKey(self, key):
        node = self.head

        while node:
            if node.key == key:
                if node.prev is None and node.next is None:
                    self.head = self.tail = None
                else:
                    
                    if node.prev is not None:  
                        node.prev.next = node.next
                    else:  
                        self.head = self.head.next

                    
                    if node.next is not None:  
                        node.next.prev = node.prev
                    else:  
                        self.tail = self.tail.prev
                break
            node = node.next

    def deleteFirst(self):
        if self.head is self.tail:
            self.head = self.tail = None
        else:
            self.head.next.prev = None
            self.head = self.head.next

    def deleteLast(self):
        if self.head is self.tail:
            self.head = self.tail = None
        else:
            self.tail.prev.next = None
            self.tail = self.tail.prev

    def createAnswerList(self):
        ret = []
        node = self.head

        while node:
            ret.append(node.key)
            node = node.next

        return " ".join(ret)


if __name__ == "__main__":
    from sys import stdin

    count = int(input())

    linked_list = DoublyLinkedList()

    for i in range(count):
        order = stdin.readline().strip().split()
        if order[0] == "insert":
            linked_list.insert(order[1])
        elif order[0] == "delete":
            linked_list.deleteKey(order[1])
        elif order[0] == "deleteFirst":
            linked_list.deleteFirst()
        elif order[0] == "deleteLast":
            linked_list.deleteLast()

    print(linked_list.createAnswerList())

Solution (Python):
import sys

class MyNode:
    __slots__ = ['prev', 'next', 'val']

    def __init__(self, x):
        self.prev = None
        self.next = None
        self.val = x

class MyLinkList:
    __slots__ = ['head', 'tail']

    def __init__(self):
        self.head = None
        self.tail = None

    def generate(self):
        _node = self.head
        while True:
            if _node is None:
                break
            yield _node.val
            _node = _node.next

    def insert(self, x):
        node = MyNode(x)
        if self.head is None:
            self.head = self.tail = node
        else:
            self.head.prev = node
            node.next = self.head
            self.head = node

    def delete(self, x):
        if self.head.val == x:
            return self.deleteFirst()

        node = self.head.next
        while node.next is not None:
            if node.val == x:
                node.prev.next = node.next
                node.next.prev = node.prev
                
                
                return True

            node = node.next

        if self.tail.val == x:
            return self.deleteLast()

        return False

    def deleteFirst(self):
        if self.head == self.tail:
            self.head = self.tail = None
            return True

        self.head = self.head.next
        
        self.head.prev = None

        return True

    def deleteLast(self):
        if self.head == self.tail:
            self.head = self.tail = None
            return True

        self.tail = self.tail.prev
        
        self.tail.next = None

        return True

def _main():
    link_list = MyLinkList()

    n = int(input())
    
    for i in range(n):
        line = sys.stdin.readline()

        if line[0] == 'i':
            link_list.insert(line[7:-1])
        else:
            if line[6] == 'F':
                link_list.deleteFirst()
            elif line[6] == 'L':
                link_list.deleteLast()
            else:
                link_list.delete(line[7:-1])

    lst = [x for x in link_list.generate()]
    print(" ".join(lst))

if __name__ == '__main__':
    _main()

Solution (Python):
from collections import deque
n=int(input())
Deq=deque()
for i in range(n):
    command=input().split()
    if len(command)>1:
        x=command[1]
    if command[0]=="insert":
        Deq.appendleft(x)

    elif command[0]=="delete":
        try:
            Deq.remove(x)
        except:
            pass

    elif command[0]=="deleteFirst":
        Deq.popleft()


    elif command[0]=="deleteLast":
        Deq.pop()
print(*Deq)

Solution (Python):
from collections import deque
import sys

n=int(sys.stdin.readline())
dq = deque()

def command(cm):
	if cm[0]=='insert':
		dq.appendleft(cm[1])
	if cm[0]=='delete':
		try:dq.remove(cm[1])
		except ValueError:pass
	if cm[0]=='deleteFirst':
		dq.popleft()
	if cm[0]=='deleteLast':
		dq.pop()

for i in range(n):
	command(sys.stdin.readline().split())
print(' '.join(dq))

Solution (Python):
class Elm:
    def __init__(self, value, ptr_prev, ptr_next):
        self.value = value
        self.prev = ptr_prev
        self.next = ptr_next



class DoublyLinkedList:
    def __init__(self):
        self.nil = Elm("NIL", None, None)
        self.nil.prev = self.nil 
        self.nil.next = self.nil 
        
def insertVal(self, value):
    new_elm = Elm(value, self.nil, self.nil.next)
    new_elm.next.prev = new_elm
    self.nil.next = new_elm

def deleteVal(self, value):
    ptr = self.nil.next
    while ptr.value != value:
        if ptr.next == self.nil:
            return
        ptr = ptr.next



    ptr.prev.next = ptr.next
    ptr.next.prev = ptr.prev

def deleteFirst(self):
    self.nil.next = self.nil.next.next
    self.nil.next.prev = self.nil

def deleteLast(self):
    self.nil.prev = self.nil.prev.prev
    self.nil.prev.next = self.nil

def show(self):
    ptr = self.nil.next
    while ptr!=self.nil:

        if ptr.next == self.nil:
            print(ptr.value)
            return
        print(ptr.value, end=" ")
        ptr = ptr.next
        
def selectOps(dll, command):

    if command[0] == "insert":
        insertVal(dll, command[1])
    elif command[0] == "delete":
        deleteVal(dll, command[1])        
    elif command[0] == "deleteFirst":
        deleteFirst(dll)

    else:
        deleteLast(dll)



    
import sys

n = int(input())
l = DoublyLinkedList()

for i in range(n):
    command = sys.stdin.readline().split()


    if command[0] == "insert":
        insertVal(l, command[1])
    elif command[0] == "delete":
        deleteVal(l, command[1])        
    elif command[0] == "deleteFirst":
        deleteFirst(l)

    else:
        deleteLast(l)
show(l)

Solution (Python):

from sys import stdin


class DoublyLinkedList():

    class Cell():
        def __init__(self, value):
            self.value = value
            self.next = None
            self.previous = None

    def __init__(self):
        self.head = DoublyLinkedList.Cell(None)
        self.tail = DoublyLinkedList.Cell(None)
        self.head.next = self.tail
        self.tail.previous = self.head

    def insert(self, v):
        new_cell = DoublyLinkedList.Cell(v)
        new_cell.next = self.head.next
        new_cell.previous = self.head
        self.head.next.previous = new_cell
        self.head.next = new_cell

    def delete(self, v):
        tmp_cell = self.head.next
        while tmp_cell.value is not None:
            if tmp_cell.value == v:
                tmp_cell_next = tmp_cell.next
                tmp_cell_previous = tmp_cell.previous
                tmp_cell_previous.next = tmp_cell_next
                tmp_cell_next.previous = tmp_cell_previous
                break
            tmp_cell = tmp_cell.next

    def deleteFirst(self):
        self.head.next = self.head.next.next
        self.head.next.previous = self.head

    def deleteLast(self):
        self.tail.previous = self.tail.previous.previous
        self.tail.previous.next = self.tail


def print_res(dl):
    res = []
    cur = dl.head.next
    while cur.next is not None:
        res.append(cur.value)
        cur = cur.next
    print(" ".join(res))


def main():
    dl = DoublyLinkedList()
    line_num = int(stdin.readline())
    for i in range(line_num):
        command = stdin.readline().strip()
        if command == "deleteFirst":
            dl.deleteFirst()
        elif command == "deleteLast":
            dl.deleteLast()
        elif command.startswith("insert"):
            dl.insert(command.split(" ")[1].strip())
        else:
            dl.delete(command.split(" ")[1].strip())
    print_res(dl)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque

def OpeList():
    L = deque()
    num_command = int(input())

    for i in range(num_command):
        input_command = input()
        if input_command == 'deleteFirst':
            L.popleft()
        elif input_command == 'deleteLast':
            L.pop()
        else:
            com, num =  input_command.split()
            if com == 'insert':
                L.appendleft(num)
            elif com == 'delete':
                try:
                    
                    
                    L.remove(num)
                except:
                    pass
    print(' '.join(L))

OpeList()

Solution (Python):

import sys
from collections import deque


def input(): return sys.stdin.readline().strip()


def insert(x, l):
    l.appendleft(x)


def delete(x, l):
    if len(l) == 0:
        return
    index = l.index(x) if x in l else None
    if index is None:
        return
    else:
        l.remove(x)


def delete_first(l):
    if len(l) == 0:
        return
    l.popleft()


def delete_last(l):
    if len(l) == 0:
        return
    l.pop()


n = int(input())
l = deque()

for _ in range(n):
    command = input().split(' ')

    if command[0] == 'insert':
        insert(command[1], l)

    elif command[0] == 'delete':
        delete(command[1], l)

    elif command[0] == 'deleteFirst':
        delete_first(l)

    elif command[0] == 'deleteLast':
        delete_last(l)

print(' '.join(l))

Solution (Python):
import sys
input = sys.stdin.readline

NEXT = 2
DATA = 1
PREV = 0
def dll_processor(operations):

    front = None
    end = None

    for o in operations:
        if o[0] == "insert":
            front, end = insert(front, end, o[1])
        elif o[0] == "delete":
            front, end = delete(front, end, o[1])
        elif o[0] == "deleteFirst":
            front, end = delete_first(front, end)
        elif o[0] == "deleteLast":
            front, end = delete_last(front, end)
        

    return get_list(front)

def get_list(front):
    if not front:
        return []

    l = []
    target = front
    while True:
        l.append(target[DATA])
        if not target[NEXT]:
            break
        target = target[NEXT]
    return l

def insert(front, end, target):
    node = [None, target, None]
    if front:
        front[PREV] = node
        node[NEXT] = front
        return node, end
    else:
        return node, node

def delete(front, end,target):

    delete_node = front
    while not delete_node[DATA] == target:
        delete_node = delete_node[NEXT]
        if delete_node == None:
            return front, end

    if delete_node[PREV] == None:
        delete_node[NEXT][PREV] = None
        return delete_node[NEXT], end
    elif delete_node[NEXT] == None:
        delete_node[PREV][NEXT] = None
        return front, delete_node[PREV]
    else:
        delete_node[NEXT][PREV] = delete_node[PREV]
        delete_node[PREV][NEXT] = delete_node[NEXT]
        return front, end

def delete_last(front, end):

    if not end[PREV]:
        return None, None
    else:
        end[PREV][NEXT] = None
        return front, end[PREV]

def delete_first(front, end):

    if not front[NEXT]:
        return None, None
    else:
        front[NEXT][PREV] = None
        return front[NEXT], end

def main():
    n_list = int(input())
    target_list = [input().split() for i in range(n_list)]
    print(*dll_processor(target_list))

if __name__ == "__main__":
    main()

Solution (Python):


from collections import deque

n = int(input())
que = deque()
command = ""
key = 0
for i in range(n):
    line = input()
    if " " in line:
        command, key = line.split()
    else:
        command = line

    if command == "insert":
        que.appendleft(key)
    elif command == "delete":
        try:
            que.remove(key)
        except:
            pass
    elif command == "deleteFirst":
        que.popleft()
    elif command == "deleteLast":
        que.pop()
print(" ".join(que))

Solution (Python):
from collections import deque





  












































n = int(input())
commands = [input() for _ in range(n)]

q = deque()
for command in commands:
  op = command.split()
  if op[0] == "insert":
    q.appendleft(op[1])
  elif op[0] == "delete":
    try:
      q.remove(op[1])
    except ValueError as err:
      pass
  elif op[0] == "deleteFirst":
    q.popleft()
  elif op[0] == "deleteLast":
    q.pop()
print(" ".join(map(str, q)))

Solution (Python):
import sys
from collections import deque

queue = deque()
n = int(input())
for _ in range(n):
    op = sys.stdin.readline().strip().split()
    if op[0] == 'deleteFirst':
        queue.popleft()
    elif op[0] == 'deleteLast':
        queue.pop()
    elif op[0] == 'insert':
        queue.appendleft(int(op[1]))
    elif op[0] == 'delete':
        try:
            queue.remove(int(op[1]))
        except ValueError:
            pass

print(' '.join(map(str, queue)))

Solution (Python):
from collections import deque
from enum import Enum
import sys
import math
 
BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001
 
 
LIST = deque()
 
num_command = int(input())
 
for loop in range(num_command):
 
    input_command = input()
 
    if input_command == 'deleteFirst':
        LIST.popleft()
    elif input_command == 'deleteLast':
        LIST.pop();
    else:
        com,num = input_command.split()
 
        if com == 'insert':
            LIST.appendleft(num)
        elif com == 'delete':
            try:
                LIST.remove(num)
            except:
                pass
 
print(' '.join(LIST))

Solution (Python):
import collections
a=collections.deque()
for _ in range(int(input())):
    b=input()
    if b[0]=='i':a.appendleft(b[7:])
    elif b[6]==' ':
        try:a.remove(b[7:])
        except:pass
    elif len(b)>10:a.popleft()
    elif len(b)>6:a.pop()
print(*a)

Solution (Python):
from collections import deque

n = int(input())
linked_list = deque()

for _ in range(n):
    row = input().split()
    if len(row) == 1:
        command = row[0]
    else:
        command, key = row
    
    if command == 'deleteFirst' and linked_list:
        del linked_list[0]
    elif command == 'deleteLast' and linked_list:
        del linked_list[-1]
    elif command == 'delete':
        if key in linked_list:
            linked_list.remove(key)
    else:
        linked_list.appendleft(key)

print(*linked_list)

Solution (Python):
from collections import deque

if __name__ == '__main__':
    res = deque([])
    n_order = int(input())
    for i in range(n_order):
        order = input().split()
        if 'insert' == order[0]: 
            res.appendleft(order[1])
        elif 'delete' == order[0]:
            if order[1] in res:
                res.remove(order[1])
        elif 'deleteFirst' == order[0]:
            res.popleft()
        elif 'deleteLast' == order[0]: 
            res.pop()
    print(' '.join(res))

Solution (Python):
from collections import deque


if __name__ == "__main__":
    operation_num = int(input())
    double_linked_list: deque = deque()

    for _ in range(operation_num):
        operation = input()

        if ("deleteFirst" == operation):
            double_linked_list.popleft()
        elif ("deleteLast" == operation):
            double_linked_list.pop()
        else:
            command, num = operation.split(" ")
            if ("insert" == command):
                double_linked_list.appendleft(num)
            else:
                try:
                    double_linked_list.remove(num)
                except ValueError:
                    pass

    print(" ".join(double_linked_list))

Solution (Python):
if __name__ == '__main__':
    N = int(input())
    

    import collections
    d = collections.deque()

    for i in range(N):
        val = input().split()

        if val[0] == 'insert':
            
            d.appendleft(val[1])
        elif val[0] == 'delete':
            
            try:
                d.remove(val[1])
            except ValueError:
                pass
            
        elif val[0] == 'deleteFirst':
            
            d.popleft()
            
        elif val[0] == 'deleteLast':
            
            d.pop()

    
    print(' '.join(d))

Solution (Python):
from collections import deque


def main():
    lst = deque()
    n = int(input())
    for _ in range(n):
        inputs = input().split(' ')
        command = inputs[0]
        if command == 'insert':
            lst.appendleft(inputs[1])
        elif command == 'delete':
            try:
                lst.remove(inputs[1])
            except Exception:
                pass
        elif command == 'deleteFirst':
            lst.popleft()
        elif command == 'deleteLast':
            lst.pop()
    result = ''
    while len(lst) > 0:
        result += lst.popleft() + ' '
    print(result.strip())


if __name__ == '__main__':
    main()

Solution (Python):
"""


 = sys.stdin.readline().rstrip()
 = list(sys.stdin.readline().rstrip())


 = int(sys.stdin.readline())
 = map(int, sys.stdin.readline().split())
 = list(map(int, sys.stdin.readline().split()))
 = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  


a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654


char_list = list("abcd") 
"""

import math
import sys
import itertools
import queue
from fractions import gcd
from collections import deque


mod = 1000000007

if __name__ == "__main__":
    n = int(sys.stdin.readline())
    commands = {"insert": lambda l,x:l.appendleft(x),
    "delete": lambda l,x:l.remove(x) if x in l else x,
    "deleteFirst": lambda l:l.popleft(),
    "deleteLast": lambda l:l.pop(),
    }
    l=deque()
    for i in range(n):
        c = sys.stdin.readline().split()
        if c[0]=="deleteFirst" or c[0]=="deleteLast":
            commands[c[0]](l)
        elif c[0] in commands:
            commands[c[0]](l,c[1])
            
    l_list=list(l)
    print(*l_list)

Solution (Python):
from collections import deque
from sys import stdin
N = int(stdin.readline().rstrip())
d = deque()
for i in range(N):
    op_key = stdin.readline().rstrip().split()
    if op_key[0] == 'insert':
        d.appendleft(op_key[1])
    elif op_key[0] == 'delete':
        try:
            d.remove(op_key[1])
        except:
            pass
    elif op_key[0] == 'deleteFirst':
        d.popleft()
    elif op_key[0] == 'deleteLast':
        d.pop()
print(*d)

Solution (Python):
from collections import deque

n=int(input())
q=deque([])

for i in range(n):
    cm=input()
    if cm!="deleteFirst" and cm!="deleteLast":
        cm,x=cm.split()
    
    if cm=="insert":
        q.appendleft(x)
    elif cm=="delete":
        try:
            q.remove(x)
        except:
            continue
    elif cm=="deleteFirst":
        q.popleft()
    else:
        q.pop()
print(*q)

Solution (Python):
from collections import deque
import sys
n = int(input())
deq = deque()
sub_deq = {}
for cmd in sys.stdin.readlines():
    if cmd == 'deleteFirst\n':
        while deq:
            data = deq.pop()
            if data[1]:
                data[1] = 0
                break
    elif cmd == 'deleteLast\n':
        while deq:
            data = deq.popleft()
            if data[1]:
                data[1] = 0
                break
    else:
        cmd, val = cmd.split()
        if cmd == 'insert':
            data = [val, 1]
            deq.append(data)
            if val not in sub_deq:
                sub_deq[val] = [data]
            else:
                sub_deq[val].append(data)
        elif cmd == 'delete':
            if val in sub_deq:
                deq_v = sub_deq[val]
                while deq_v:
                    data = deq_v.pop()
                    if data[1]:
                        data[1] = 0
                        break
sys.stdout.write(" ".join(data[0] for data in reversed(deq) if data[1]))
sys.stdout.write('\n')

Solution (Python):
import sys
from collections import deque

input = sys.stdin.readline

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

for _ in range(n):
    command = input().split()
    if command[0] == 'insert':
        que.appendleft(command[1])
    elif command[0] == 'delete':
        if command[1] in que:
            que.remove(command[1])
    elif command[0] == 'deleteFirst':
        que.popleft()
    else:
        que.pop()

print(' '.join(map(str, que)))

Solution (Python):



import collections

def doublylinkedlist(d, op):
    if "insert" in op:
        d.appendleft(op.split()[1])
    elif op == "deleteFirst":
        d.popleft()
    elif op == "deleteLast":
        d.pop()
    else:
        try:
            d.remove(op.split()[1])
        except:
            pass
        
    return d

n = int(input())
d = collections.deque()
for _ in range(n):
    doublylinkedlist(d, input())

print(" ".join(d))

Solution (Python):
"""
双方向連結リスト
どっちからでもつけられるよ
"""
from collections import deque
d = deque([])
n = int(input())
for _ in range(n):
    
    com = input()
    if com == "deleteFirst" and len(d)>0:
        d.popleft()
    elif com == "deleteLast" and len(d)>0:
        d.pop()
    elif com != "deleteFirst" and com != "deleteLast":
        com, num = com.split(" ")
        if com == "insert":
            d.appendleft(int(num))
        elif com == "delete":
            try:
                d.remove(int(num))
            except:
                continue
    
ans = " ".join([str(s) for s in list(d)])
print(ans)

Solution (Python):
from collections import deque

if __name__ == '__main__':
	n = int(input())
	tmp = deque()
	for i in range(n):
		command = input()
		if command == "deleteFirst":
			tmp.popleft()
		elif command == "deleteLast":
			tmp.pop()
		else:
			c,d = command.split()
			if c == "insert":
				tmp.appendleft(int(d))
			elif c == "delete":
				try:
					tmp.remove(int(d))
				except:
					pass
	print(*tmp)

Solution (Python):
from enum import Enum
from queue import Queue
import collections

import sys
import math

BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001

A = collections.deque()
n = int(input())
for _ in range(n):
    b = input()
    if b[0] == 'i':
        A.appendleft(b[7:])
    elif b[6] == ' ':
        try:A.remove(b[7:])
        except:pass
    elif len(b)>10:
        A.popleft()
    elif len(b)>6:
        A.pop()
print(*A)

Solution (Python):
from collections import deque

def main():
    n = int(input())
    d = deque()
    for _ in range(n):
        command = input()
        if command == "deleteFirst":
            d.popleft()
        elif command == "deleteLast":
            d.pop()
        else:
            com, num = command.split(" ")
            if com == "insert":
                d.appendleft(num)
            elif com == "delete":
                try:
                    d.remove(num)
                except:
                    pass
    print(" ".join(d))


main()

Solution (Python):
from collections import deque


def main():
    ans = deque()
    n = int(input())
    for i in range(n):
        c = input()
        if c == "deleteFirst":
            ans.popleft()
        elif c == "deleteLast":
            ans.pop()
        else:
            c, x = c.split()
            if c == "insert":
                ans.insert(0, x)
            else:
                try:
                    ans.remove(x)
                except ValueError:
                    pass
    print(*ans)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque



n = int(input())



que = deque()

for _ in range(n):

    command = input()

    if   command == "deleteFirst":
        que.popleft()
    elif command == "deleteLast":
        que.pop()
    else:
        command, x = command.split()

        if   command == "insert":
            que.appendleft(int(x))

        elif command == "delete":
            try:
                que.remove(int(x))
            except:
                pass



print(*que)

Solution (Python):
from collections import deque
n = int(input())
target_list = deque()
for ni in range(n):
    cmd = input().split()
    if cmd[0] == "deleteFirst":
        target_list.popleft()
    elif cmd[0] == "deleteLast":
        target_list.pop()
    elif cmd[0] == "insert":
        target_list.appendleft(cmd[1])
    elif cmd[0] == "delete":
        try:
            target_list.remove(cmd[1])
        except ValueError:
            pass
print(*target_list)

Solution (Python):
import collections
num=collections.deque([])

for i in range(int(input())):
	com=input().split()
	if com[0]=='insert':
		num.appendleft(com[1])
	elif com[0]=='delete':
		try:
			num.remove(com[1])
		except ValueError:
			pass
	elif com[0]=='deleteFirst':
		num.popleft()
	elif com[0]=='deleteLast':
		num.pop()
print(' '.join(num))