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):

'''
本来はlinkedList を自前実装する必要がありそうだが、Python だとDequeで実装できる。
'''
from collections import deque
N = int(input())
linkedList = deque([])
for i in range(N):
    command = input().split()
    if command[0] == "insert":
        linkedList.appendleft(command[1])
    elif command[0] == "delete":
        if command[1] in linkedList:
                linkedList.remove(command[1])
    elif command[0] == "deleteFirst":
        linkedList.popleft()
    elif command[0] == "deleteLast":
        linkedList.pop()
print(" ".join(list(linkedList)))

Solution (Python):
from collections import deque


def main():
    n = int(input())
    Q = deque()

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

        if cmd[0] == "deleteFirst":
            Q.popleft()
        elif cmd[0] == "deleteLast":
            Q.pop()
        elif cmd[0] == "insert":
            Q.appendleft(int(cmd[1]))
        elif cmd[0] == "delete" and int(cmd[1]) in Q:
            Q.remove(int(cmd[1]))

    print(*Q)


if __name__ == "__main__":
    main()

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

class DoublyLinkedList:
    def __init__(self):
        self.nil = Node(None)
        self.nil.prev = self.nil
        self.nil.next = self.nil
    
    def insert(self,key):
        new = Node(key)
        new.next = self.nil.next
        self.nil.next.prev = new
        self.nil.next = new
        new.prev = self.nil
    
    def listSearch(self,key):
        cur = self.nil.next
        while cur != self.nil and cur.key != key:
            cur = cur.next
        return cur
    
    def deleteNode(self, t):
        if t == self.nil:
            return
        t.prev.next = t.next
        t.next.prev = t.prev

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

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

    def deleteKey(self, key):
        self.deleteNode(self.listSearch(key))
if __name__ == '__main__':
    import sys
    input = sys.stdin.readline
    n = int(input())
    d = DoublyLinkedList()
    for _ in range(n):
        c = input().rstrip()
        if c[0] == "i":
            d.insert(c[7:])
        elif c[6] == "F":
            d.deleteFirst()
        elif c[6] =="L":
            d.deleteLast()
        else:
            d.deleteKey(c[7:])
    ans = []
    cur = d.nil.next
    while cur != d.nil:
        ans.append(cur.key)
        cur = cur.next
    print(" ".join(ans))

Solution (Python):
from collections import deque

N = int(input())

d = deque([])
for i in range(N):
    a = input()
    if a == "deleteFirst":
        d.popleft()
    elif a == "deleteLast":
        d.pop()
    else:
        a, b = a.split()
        if a == "insert":
            d.appendleft(b)
        else:
            try:
                d.remove(b)
            except:
                pass

print(*d)

Solution (Python):
from collections import deque

if __name__ == '__main__':
	n = int(input())
	L = deque()

	for i in range(n):
		cmdline = input().split()

		if cmdline[0] == "insert":
			L.appendleft(cmdline[1])
		elif cmdline[0] == "delete":
		    try:
			    L.remove(cmdline[1])
		    except ValueError:
			    pass
		elif cmdline[0] == "deleteFirst":
			L.popleft()
		elif cmdline[0] == "deleteLast":
			L.pop()

	print(" ".join(L))

Solution (Python):
from collections import deque

DLL = deque()

n = int(input())

for i in range(n):
    Line = input().split()
    
    if Line[0] == "insert":
        DLL.appendleft(Line[1])
        
    elif Line[0] == "delete":
        try:
            DLL.remove(Line[1])
        except ValueError:
            None
    elif Line[0] == "deleteFirst":
        DLL.popleft()
    elif Line[0] == "deleteLast":
        DLL.pop()

print(" ".join(DLL))

Solution (Python):
import sys, collections
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**10
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    n = I()
    command = [LSS() for _ in range(n)]

    que = collections.deque()
    for i in command:
        if i[0]=='insert':
            que.appendleft(i[1])
        elif i[0]=='delete':
            if i[1] in que:
                que.remove(i[1])
        elif i[0]=='deleteFirst':
            que.popleft()
        elif i[0]=='deleteLast':
            que.pop()

    print(*que)

if __name__ == '__main__':
    resolve()

Solution (Python):
from collections import deque
n=int(input())
que=deque()

for i in range(n):
    command=input()
    if command=="deleteFirst":que.popleft()
    elif command=="deleteLast":que.pop()
    else:
        x,y=command.split()
        if x=="insert":que.appendleft(y)
        else:
            if y in que:que.remove(y)

print(*que)

Solution (Python):
from sys import stdin
from collections import deque


n = int(input())


command = stdin


output = deque([])

for a in command:
    if a[0] == 'i':
        output.appendleft(int(a[7:]))
    elif a[0:7] == 'delete ':
        try:
            output.remove(int(a[7:]))
        except ValueError:
            pass
    elif a[6] == 'F':
        output.popleft()
    else:
        output.pop()


print(*list(output))

Solution (Python):
from collections import deque


n = int(input())
a = deque()

for i in range(n):
        s = input()
        if s == 'deleteFirst':
                a.popleft()
        elif s == 'deleteLast':
                a.pop()
        else:     
            s ,t = s.split()
            if s == 'insert':
                a.appendleft(t)
            elif s == 'delete':
                if t in  a:
                    a.remove(t)
        
print(*a, sep = ' ')

Solution (Python):
from collections import deque
n = int(input())
deq = deque([])
for i in range(n):
    A = input().split()
    a = A[0]
    if len(A) == 2:
        b = A[1]
    if a == "insert":
        deq.appendleft(b)
    elif a == "deleteFirst":
        deq.popleft()
    elif a == "deleteLast":
        deq.pop()
    else:
        try:
            deq.remove(b)
        except:
            continue
print(" ".join(list(deq)))

Solution (Python):
from collections import deque
dq = deque()
for mak in range(int(input())) :
    com = input().split()
    if   com[0] == 'insert'      : dq.appendleft(com[1])
    elif com[0] == 'deleteFirst' : dq.popleft()
    elif com[0] == 'deleteLast'  : dq.pop()
    else : 
        if com[1] in dq :
            dq.remove(com[1])
print(' '.join(dq))

Solution (Python):
import math
import sys
import itertools
from collections import deque


def main():
  n = int(input())

  d = deque()
  for _ in range(n):
    order = input().split()

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

  print(*d)

if __name__ == '__main__':
  main()

Solution (Python):
from collections import deque
import sys

if __name__ == '__main__':
    n = int(input())
    d = deque()

    for command in sys.stdin.readlines():

        if command.startswith("insert"):
            d.appendleft(int(command[len("insert"):]))
        elif command.startswith("delete "):
            if int(command[len("delete"):]) in d:
                d.remove(int(command[-2]))
        elif command.startswith("deleteLast"):
            d.pop()
        elif command.startswith("deleteFirst"):
            d.popleft()
        else:
            print(command)

        
    print(*d)

Solution (Python):
from collections import deque

que = deque([])
n = int(input())
for i in range(0, 	n):
	a = input()
	if a == 'deleteFirst':
		que.popleft()


	elif a == 'deleteLast':
		que.pop()


	else:
		a, b = a.split()
		b = int(b)
		if a == 'insert':
			que.appendleft(b)


		elif a == 'delete':
			if que.count(b) != 0:
				que.remove(b)


print(*que)

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


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


class DoublyLinkedList:
    def __init__(self):
        self.n = 0
        self.dummy = Node(None)
        self.dummy.prev = self.dummy
        self.dummy.next = self.dummy

    def __getitem__(self, i):
        if i < 0 or i >= self.n:
            raise IndexError()
        return self._get_node(i).val 

    def __setitem__(self, i):
        if i < 0 or i >= self.n:
            raise IndexError()
        return self._get_node(i).val

    def __len__(self):
        return self.n

    def __bool__(self):
        return self.n != 0

    def __iter__(self):
        self.p = self.dummy
        return self

    def __next__(self):
        self.p = self.p.next
        if self.p == self.dummy:
            raise StopIteration
        return self.p.val

    def append(self, val):
        ptr = self.dummy.prev
        new_ptr = Node(val)
        new_ptr.prev = ptr.prev
        new_ptr.next = ptr
        new_ptr.next.prev = new_ptr
        new_ptr.prev.next = new_ptr
        self.n += 1

    def appendleft(self, val):
        ptr = self.dummy.next
        new_ptr = Node(val)
        new_ptr.prev = ptr.prev
        new_ptr.next = ptr
        new_ptr.next.prev = new_ptr
        new_ptr.prev.next = new_ptr
        self.n += 1

    def pop(self):
        ptr = self.dummy.prev
        ptr.prev.next = ptr.next
        ptr.next.prev = ptr.prev
        self.n -= 1

    def popleft(self):
        ptr = self.dummy.next
        ptr.prev.next = ptr.next
        ptr.next.prev = ptr.prev
        self.n -= 1

    def _get_node(self, i):
        if i < self.n // 2:
            ptr = self.dummy.next
            for _ in range(i):
                ptr = ptr.next
        else:
            ptr = self.dummy
            for _ in range(i, self.n):
                ptr = ptr.prev
        return ptr

    def _remove(self, i):
        if i < 0 or i >= self.n:
            raise IndexError()
        ptr = self._get_node(i)
        ptr.prev.next = ptr.next
        ptr.next.prev = ptr.prev
        self.n -= 1
        return ptr.val

    def _add(self, i, val):
        if i < 0 or i > self.n:
            raise IndexError()
        ptr = self._get_node(i)
        new_ptr = Node(val)
        new_ptr.prev = ptr.prev
        new_ptr.next = ptr
        new_ptr.next.prev = new_ptr
        new_ptr.prev.next = new_ptr
        self.n += 1

    def delete(self, val):
        ptr = self.dummy
        for _ in range(self.n):
            ptr = ptr.next
            if ptr.val == val:
                ptr.prev.next = ptr.next
                ptr.next.prev = ptr.prev
                self.n -= 1
                return True
        return False


dls = DoublyLinkedList()
input()
for query in sys.stdin:
    if query[0] == "i":
        val = query[7:-1]
        dls.appendleft(val)
    elif query[6] == "F":
        dls.popleft()
    elif query[6] == "L":
        dls.pop()
    else:
        val = query[7:-1]
        dls.delete(val)

print(*list(dls))

Solution (Python):
from collections import deque
a = int(input())

dl = deque()
for i in range(a):
    o = input().split()

    if o[0] == 'delete':
        try:
            dl.remove(o[1])
        except ValueError:
            pass
    elif o[0] == 'deleteFirst':
        dl.popleft()
    elif o[0] == 'deleteLast':
        dl.pop()
    else:
        dl.appendleft(o[1])

print(" ".join([str(i) for i in dl]))

Solution (Python):
from collections import deque

def readinput():
    n = int(input())
    commands = []
    for i in range(n):
        command = input().split()
        commands.append(command)
    return n,commands

def main(n,commands):
    l = deque([])
    for i in range(n):
        command = commands[i]
        if (command[0]=='insert'):
            op = command[1]
            l.appendleft(op)
        elif(command[0]=='delete'):
            op = command[1]
            try:
                l.remove(op)
            except:
                pass
        elif(command[0]=='deleteFirst'):
            l.popleft()
        elif(command[0]=='deleteLast'):
            l.pop()
        else:
            pass
        

    return l
    
def main2():
    n = int(input())
    l = deque([])
    for i in range(n):
        command = input().split()
        if (command[0]=='insert'):
            op = command[1]
            l.appendleft(op)
        elif(command[0]=='delete'):
            op = command[1]
            try:
                l.remove(op)
            except:
                pass
        elif(command[0]=='deleteFirst'):
            l.popleft()
        elif(command[0]=='deleteLast'):
            l.pop()
        else:
            pass
        

    return l
if __name__=='__main__':
    
    
    ans = main2()
    print(' '.join(ans))

Solution (Python):
from collections import deque
import sys

n = int(input())
deq = deque()

for _ in range(n):
    cmd = input()
    if cmd[0] == "i":
        deq.appendleft(cmd[7:])
    elif cmd[6] == "F":
        deq.popleft()
    elif cmd[6] == "L":
        deq.pop()
    else:
        try:
            deq.remove(cmd[7:])
        except:
            pass

print(" ".join(deq))

Solution (Python):
from collections import deque
q=deque()
n=int(input())

for i in range(n):
    cmd=input()
    if cmd=="deleteFirst":
        try:
            q.popleft()
        except:
            pass
        
    elif cmd=="deleteLast":
        try:
            q.pop()
        except:
            pass
        
    else:
        cmd,number=cmd.split()
        if cmd=="delete":
            try:
                q.remove(number)
            except:
                pass
            
        elif cmd=="insert":
            q.appendleft(number)
        
print(*q)

Solution (Python):
from sys import stdin

class Node:
    def __init__(self, value: int):
        self.value = value
        self.previous_node = None
        self.next_node = None


class LinkedList():
    def __init__(self):
        self.first = None
        self.last = None
    
    def delete_node(self, node: Node):
        if node.next_node != None:
            node.next_node.previous_node = node.previous_node
        if node.previous_node != None:
            node.previous_node.next_node = node.next_node
        if node == self.first:
            self.first = node.next_node
        if node == self.last:
            self.last = node.previous_node

    def value_generator(self):
        node = self.first
        while node != None:
            yield node.value
            node = node.next_node

    def insert(self, x: int):
        node = Node(x)
        node.next_node = self.first
        if self.first != None:
            self.first.previous_node = node
        self.first = node
        if self.last == None:
            self.last = node

    def delete(self, x: int):
        target_node = self.first
        while target_node != None and target_node.value != x:
            target_node = target_node.next_node
        if target_node != None:
            self.delete_node(target_node)


def main():
    linked_list = LinkedList()
    insert,delete,delete_node = linked_list.insert,linked_list.delete,linked_list.delete_node
    _ = int(stdin.readline())
    for line in (line.split() for line in stdin.readlines()):
        command = line[0]
        if command == "insert":
            insert(int(line[1]))
        elif command == "delete":
            delete(int(line[1]))
        elif command == "deleteFirst":
            delete_node(linked_list.first)
        elif command == "deleteLast":
            delete_node(linked_list.last)
    print(*linked_list.value_generator())


main()

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

Solution (Python):
from collections import deque
n = int(input())

que = deque()
for _ in range(n):
    cmd = input()

    if cmd == 'deleteFirst':
        try:
            que.popleft()
        except:
            pass
    elif cmd == 'deleteLast':
        try:
            que.pop()
        except:
            pass
    else:
        cmd, x = cmd.split()
        if cmd == 'insert':
            que.appendleft(x)
        elif cmd == 'delete':
            try:
                que.remove(x)
            except:
                pass

print(*que)

Solution (Python):
from collections import deque
n =  int(input())

mylist = deque()


for i in range(n):
    a = input()
    if a == "deleteFirst":
        mylist.popleft()
    elif a == "deleteLast":
        mylist.pop()
    else:
        a,key = a.split()
        if a == "insert":
            mylist.appendleft(key)
        else:
            try:
                mylist.remove(key)
            except:
                pass

print(' '.join(mylist))

Solution (Python):
from collections import deque
n = int(input())
l = deque()
for i in range(n):
    command = input()
    if command == "deleteFirst":
        l.popleft()
    elif command == "deleteLast":
        l.pop()
    else:
        order, num = command.split()
        if order == "insert":
            l.appendleft(num)
        elif order == "delete":
            try:
                l.remove(num)
            except:
                pass

print(" ".join(l))

Solution (Python):
from collections import deque
import sys

input = sys.stdin.readline

n = int(input())

doubly_linked_list = deque()

for _ in range(n):
    command = input().split()
    if command[0] == "insert":
        doubly_linked_list.appendleft(command[1])
    elif command[0] == "delete":
        try:
            doubly_linked_list.remove(command[1])
        except:
            pass
    elif command[0] == "deleteFirst":
        doubly_linked_list.popleft()
    elif command[0] == "deleteLast":
        doubly_linked_list.pop()

print(" ".join(doubly_linked_list))

Solution (Python):
from collections import deque 

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

for loop in range(n):
    command=input()
    
    if command=='deleteFirst':
        l.popleft()
    elif command=='deleteLast':
        l.pop()
    else:
        com,num=command.split()

        if com=='insert':
            l.appendleft(num)
        elif com=='delete':
            try:l.remove(num)
            except:pass
    
print(*l)

Solution (Python):
from collections import deque
import sys
n = int(sys.stdin.readline())

lines = [input() for i in range(n)]

deq = deque()
for i in range(n):
    o = lines[i].split()
    if o[0] == 'insert':
        deq.appendleft(o[1])
    elif o[0] == 'delete':
        try:
            deq.remove(o[1])
        except:
            continue
    elif o[0] == 'deleteFirst':
        deq.popleft()
    elif o[0] == 'deleteLast':
        deq.pop()

print(' '.join(deq))

Solution (Python):
import collections
N=int(input())
A=collections.deque()
for i in range(N):
    c=input()
    if c[0]=='i':
        A.appendleft(int(c.split()[1]))
    elif c[6]==' ':
        if int(c.split()[1]) in A:
            A.remove(int(c.split()[1]))
    elif c[6]=='F':
        A.popleft()
    elif c[6]=='L':
        A.pop()
print(*A)

Solution (Python):
from collections import deque
que = deque()
for _ in range(int(input())):
    op, v = (input().split() + ["1"])[:2]
    if op == "insert":
        que.appendleft(v)
    elif op == "delete":
        try:
            que.remove(v)
        except:
            pass
    elif op == "deleteFirst":
        que.popleft()
    elif op == "deleteLast":
        que.pop()
print(' '.join(que))

Solution (Python):
import sys
import collections

N = int(input())

L = collections.deque()
for _ in range(N):
    com = (sys.stdin.readline().rstrip()).split()
    if com[0] == 'insert':
        L.appendleft(int(com[1]))
    elif com[0] == 'delete':
        try:
            L.remove(int(com[1]))
        except:
            pass
    elif com[0] == 'deleteFirst':
        L.popleft()
    elif com[0] == 'deleteLast':
        L.pop()

print(*L)

Solution (Python):


from collections import deque
n = int(input())
c = [input() for _ in range(n)]
d = deque()

for com in c:
    if com[0] == 'i':
        d.appendleft(com[7:])
    else:
        s = com[6]
        if s == 'F':
            d.popleft()
        elif s == 'L':
            d.pop()
        else:
            try:
                d.remove(com[7:])
            except ValueError:
                pass
print(' '.join(d))

Solution (Python):

from collections import deque
import sys

d=deque()

n=int(input())

for i in range(n):
    s=input()
    if s=='deleteFirst':
        d.popleft()
    elif s=='deleteLast':
        d.pop()
    else:
        ss,num=s.split()
        if ss == 'insert':
            d.appendleft(num)
        else:
            try:
                d.remove(num)
            except:
                pass

print(' '.join(d))

"""
def main():
    num2 = 5
    plus(num2)
    print(num2)

def plus(num2):
    num2 += 2
    return num2

if __name__=="__main__":
    main()
    """

Solution (Python):
from collections import deque
import sys


deq = deque()

q = int(input())

for _ in range(q):
    s = input()
    if s == 'deleteFirst':
        deq.popleft()
    elif s == 'deleteLast':
        deq.pop()
    else:
        ss, num = s.split()
        if ss == 'insert':
            deq.appendleft(num)
        else:
            try:
                deq.remove(num)
            except:
                pass

print(" ".join(deq))

Solution (Python):







from collections import deque

n = int(input())
mylist = deque()
for i in range(n):
    myinput = input().split()
    if len(myinput) == 1:
        order = myinput[0]
        x = 0
    else:
        order, x = myinput

    
    if order == 'insert': 
        mylist.appendleft(x)
    elif order == 'delete':
        try:
            mylist.remove(x)
        except:
            pass
    elif order == 'deleteFirst':
        
        mylist.popleft()
    elif order == 'deleteLast':
        mylist.pop()
    
print(" ".join(map(str, mylist)))

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

class DoublyLinkedList(object):
    def __init__(self):
        self.first = Node()
        self.first.prev = self.first
        self.first.next = self.first

    def insert(self, key):
        x = Node(key)
        x.next = self.first.next
        self.first.next.prev = x
        self.first.next = x
        x.prev = self.first

    def search(self, key):
        cur = self.first.next
        while cur != self.first and cur.key != key:
            cur = cur.next

        return cur

    def delete(self, key):
        self.delete_node(self.search(key))

    def delete_node(self, node):
        if node == self.first:
            return
        else:
            node.prev.next = node.next
            node.next.prev = node.prev

    def delete_first(self):
        self.delete_node(self.first.next)

    def delete_last(self):
        self.delete_node(self.first.prev)


if __name__ == "__main__":
    import sys
    input = sys.stdin.readline 

    linklist = DoublyLinkedList()

    n = int(input())

    for _ in range(n):
        inputs = input().rstrip()
        if inputs[0] == 'i':
            linklist.insert(inputs[7:])
        elif inputs[6] == 'F':
            linklist.delete_first()
        elif inputs[6] == 'L':
            linklist.delete_last()
        else:
            linklist.delete(inputs[7:])

    result = []
    cur = linklist.first.next
    while cur != linklist.first:
        result.append(cur.key)
        cur = cur.next

    print(' '.join(result))

Solution (Python):
from collections import deque

n = int(input())
Q= deque()

for _ in range(n):
    str = input()
    if str == "deleteFirst":
        Q.popleft()
    elif str == "deleteLast":
        Q.pop()
    else:
        cmd,key = str.split()
        if cmd == "insert":
            Q.appendleft(key)
        elif cmd == "delete":
            try:
                Q.remove(key)
            except:
                pass
print(" ".join(list(Q)))

Solution (Python):
from collections import deque

n = int(input())

que = deque()

for i in range(n):
    order = input()

    if order == "deleteFirst":
        que.popleft()
    elif order == "deleteLast":
        que.pop()

    else:
        order,value = order.split()
        value = int(value)
        if order == "insert":
            que.appendleft(value)
        elif order == "delete":
            try:
                que.remove(value)
            except:
                pass

print(*que)

Solution (Python):
import sys
import array
from collections import deque


def main():
    n = int(input())
    
    l = deque()
    for _ in range(n):
        c = sys.stdin.readline().split()
        if c[0] == "insert":
            l.appendleft(int(c[1]))
        elif c[0] == "delete":
            if int(c[1]) in l:
                l.remove(int(c[1]))
        elif c[0] == "deleteFirst":
            l.popleft()
        elif c[0] == "deleteLast":
            l.pop()
    print(*l)


if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque

n = int(input())

que = deque()

for i in range(n):
    order = input()

    if order == "deleteFirst":
        que.popleft()
    elif order == "deleteLast":
        que.pop()

    else:
        order,value = order.split()
        value = int(value)
        if order == "insert":
            que.appendleft(value)
        elif order == "delete":
            try:
                que.remove(value)
            except:
                pass

print(*que)

Solution (Python):
from collections import deque

dll = deque()

n = int(input())
for _ in range(n):
    command = input()
    if command[0] == "i":
        dll.appendleft(command[7:])
    elif command[0] == "d":
        if command[6] == ' ':
            key = command[7:]
            if key in dll:
                dll.remove(key)
        elif command[6] == "F":
            dll.popleft()
        else:
            dll.pop()

print(' '.join(dll))

Solution (Python):
from collections import deque

def main():
    dq = deque()
    n = int(input())
    for _ in range(n):
        com = input().split()
        if com[0]=='insert':dq.appendleft(com[1])
        elif com[0]=='delete':
            try:dq.remove(com[1])
            except:pass
        elif com[0]=='deleteFirst':dq.popleft()
        else:dq.pop()
    print(' '.join(dq))



if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque
import sys

c=sys.stdin

def main():
    q = deque()
    N=int(input())

    for i in range(N):
        order=c.readline()
        if order[6]==" ":
            if order[0]=="i":
                q.appendleft(int(order[7:]))
            else:
                try :
                    q.remove(int(order[7:]))
                except:
                    pass
        else:
            if order[6]=="F":
                q.popleft()
            else:
                q.pop()
    print(*list(q))

if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque
import sys

input = sys.stdin.readline

n = int(input())
Qs = deque()

for i in range(n):
    
    OPs = input().split()
    
    op = OPs[0]
    
    if op[0] == 'i':
        Qs.appendleft(OPs[1])
        continue
    if op == 'delete':
        try:
            Qs.remove(OPs[1])
        except ValueError:
            pass
        continue
    if op == 'deleteFirst':
        Qs.popleft()
        continue
    if op == 'deleteLast':
        Qs.pop()
        continue

print(" ".join(map(str, Qs)))

Solution (Python):
from collections import deque

n = int(input())
a = deque()

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()
    else:
        a.pop()
print(*a)

Solution (Python):
from collections import deque

n = int(input())
A = deque()

for i in range(n):
  order = input().split()
  if order[0] == "insert":
    A.appendleft(order[1])
  elif order[0] == "delete":
    try:
      A.remove(order[1])
    except ValueError:
      pass
  elif order[0] == "deleteFirst":
    A.popleft()
  elif order[0] == "deleteLast":
    A.pop()

print(" ".join(map(str, A)))

Solution (Python):
from collections import deque

lst = deque()

for l in range(int(input())):
  dat_set = input().split()
  command = dat_set[0]
  try:
    if command == 'insert':
      lst.appendleft(dat_set[-1])
    elif command == 'delete':
      lst.remove(dat_set[-1])
    elif command == 'deleteFirst':
      lst.popleft()
    elif command == 'deleteLast':
      lst.pop()
  except:
    pass

print(*lst)

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

print(" ".join(q))

Solution (Python):
from collections import deque

n = int(input())
Q = deque()
for _ in range(n):
    query = input()
    if query=='deleteFirst':
        Q.popleft()
    elif query=='deleteLast':
        Q.pop()
    else:
        q, x = query.split()
        if q=='insert':
            Q.appendleft(x)
        else:
            try:
                Q.remove(x)
            except ValueError:
                pass

print(' '.join(Q))

Solution (Python):
from collections import deque

times = int(input())
stack = deque()
for i in range(times):
    op = input().split()
    if op[0] == "insert":
        stack.appendleft(op[1])
    if op[0] == "delete":
        if op[1] in stack:
            stack.remove(op[1])
    if op[0] == "deleteFirst":
        stack.popleft()
    if op[0] == "deleteLast":
        stack.pop()
print(*stack)