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):
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):
class Node():
    def __init__(self,num,next=None,prev=None):
        self.num=num
        self.next=next
        self.prev=prev
class Doubly():
    def __init__(self):
        self.start=self.end=None
    def insert(self,x):
        it=Node(x)
        if self.end is None:
            self.end=self.start=it
        else:
            it.next=self.start
            self.start.prev=it
            self.start=it
    def delete(self,x):
        poin=self.start
        while(poin is not None):
            if poin.num==x:
                if poin.prev is None and poin.next is None:
                    self.start = self.end = None
                elif poin is self.start:
                    self.start.next.prev=None
                    self.start=poin.next
                elif poin is self.end:
                    poin.prev.next =None
                    self.end=poin.prev
                else:
                    poin.prev.next=poin.next
                    poin.next.prev=poin.prev
                break
            poin=poin.next
    
    
    
    
    
    def deleteFirst(self):
        if self.start is self.end:
            self.start = self.end = None
        else:
            self.start.next.prev = None
            self.start = self.start.next
    def deleteLast(self):
        if self.start is self.end:
            self.start = self.end = None
        else:
            self.end.prev.next = None
            self.end = self.end.prev
    def print(self):
        var=self.start
        lis=[]
        while var is not None:
            lis.append(var.num)
            var = var.next
        return ' '.join(lis)
from sys import stdin
N=int(input())
DB=Doubly()







for _ in range(N):
        cmd = stdin.readline().strip().split()
        if cmd[0] == 'insert':
            DB.insert(cmd[1])
        elif cmd[0] == 'delete':
            DB.delete(cmd[1])
        elif cmd[0] == 'deleteFirst':
            DB.deleteFirst()
        elif cmd[0] == 'deleteLast':
            DB.deleteLast()
print(DB.print())

Solution (Python):
from collections import deque

n = int(input())
cmdlst = [input() for cmd in range(n)]
lst = deque()

for cmd in cmdlst:
    if cmd == 'deleteFirst':
        lst.popleft()
    elif cmd == 'deleteLast':
        lst.pop()
    else:
        cmd,x = cmd.split()
        if cmd == 'insert':
            lst.appendleft(x)
        else:
            try:
                lst.remove(x)
            except:
                pass
print(*lst)

Solution (Python):
from collections import deque

n = int(input())
data = deque()
for i in range(n):
    command = input().split()
    if command[0] == "insert":
        data.appendleft(command[1])
    elif command[0] == "delete":
        try:
            data.remove(command[1])
        except:
            pass
    elif command[0] == "deleteFirst":
        data.popleft()
    elif command[0] == "deleteLast":
        data.pop()
print(*data)

Solution (Python):
from collections import deque


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

d = deque()

for c in commands:
    if c == "deleteFirst":
        if d:
            d.popleft()

    elif c == 'deleteLast':
        if d:
            d.pop()

    elif 'delete ' in c:
        _, key = c.split()
        key = int(key)
        if key in d:
            d.remove(key)

    elif 'insert ' in c:
        _, key = c.split()
        key = int(key)
        d.appendleft(key)

print(*d)

Solution (Python):
from collections import deque

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):
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:
        c, x = command.split()
        if c == 'insert':
            L.appendleft(x)
        elif c == 'delete':
            try:
                L.remove(x)
            except ValueError:
                pass
print(' '.join(L))

Solution (Python):
from collections import deque

n = int(input())

commnad = ''
key = 0
dll = deque()
cur = 0

for i in range(n):
    tmp = input()
    if ' ' in tmp:
        command, key = tmp.split(' ')
    else:
        command = tmp

    if command == 'insert':
        dll.appendleft(key)
    elif command == 'delete':
        try:
            dll.remove(key)
        except:
            pass
    elif command == 'deleteFirst':
        dll.popleft()
    elif command == 'deleteLast':
        dll.pop()

res = ''
for i in dll:
    res += i
    res += ' '
print(res[:-1])

Solution (Python):
import sys
n = int(sys.stdin.readline())
lines = sys.stdin.readlines()



class Node:
  def __init__(self,v):
    self.val = v
    self.prev = None
    self.next = None
class DLL():
  def __init__(self):
    self.tail = Node(None)
    self.tail.next = self.tail
    self.tail.prev = self.tail
  def insert(self,x):
    n = Node(x)
    pre = self.tail.next
    n.next = pre
    n.prev = self.tail
    pre.prev = n
    self.tail.next = n
  def delete(self,x):
    node = self.tail.next
    while node != self.tail:
      if node.val == x:
        p = node.prev
        n = node.next
        p.next = n
        n.prev = p
        return
      node = node.next
  def deleteFirst(self):
    pre = self.tail.next
    if pre == self.tail:
      return
    node = self.tail.next
    p = node.prev
    n = node.next
    p.next = n
    n.prev = p
  def deleteLast(self):
    if self.tail.next == self.tail:
      return
    node = self.tail.prev
    p = node.prev
    n = node.next
    p.next = n
    n.prev = p
  def inspect(self):
    node = self.tail.next
    ret = []
    while node != self.tail:
      ret.append(str(node.val))
      node = node.next
    print(" ".join(ret))
dll=DLL()
for i in range(n):
  q = lines[i]
  if q[0] == "i":
      dll.insert(int(q.split()[1]))
  elif q[6] == " ":
      dll.delete(int(q.split()[1]))
  elif q[6] == "L":
      dll.deleteLast()
  elif q[6] == "F":
      dll.deleteFirst()
dll.inspect()

Solution (Python):
class Node(object):
    def __init__(self, num, prv = None, nxt = None):
        self.num = num
        self.prv = prv
        self.nxt = nxt
 
 
class DoublyLinkedList(object):
    def __init__(self):
        self.start = self.last = None
 
    def insert(self, num):
        new_elem = Node(num)
 
        if self.start is None:
            self.start = self.last = new_elem
        else:
            new_elem.nxt = self.start
            self.start.prv = new_elem
            self.start = new_elem
 
    def delete_num(self, target):
        it = self.start
        while it is not None:
            if it.num == target:
                if it.prv is None and it.nxt is None:
                    self.start = self.last = None
                else:
                    if it.prv is not None:
                        it.prv.nxt = it.nxt
                    else:
                        self.start = self.start.nxt
 
                    if it.nxt is not None:
                        it.nxt.prv = it.prv
                    else:
                        self.last = self.last.prv
                break
            it = it.nxt
 
    def delete_start(self):
        if self.start is self.last:
            self.start = self.last = None
        else:
            self.start.nxt.prv = None
            self.start = self.start.nxt
 
    def delete_last(self):
        if self.start is self.last:
            self.start = self.last = None
        else:
            self.last.prv.nxt = None
            self.last = self.last.prv
 
    def get_content(self):
        ret = []
        it = self.start
 
        while it is not None:
            ret.append(it.num)
            it = it.nxt
 
        return ' '.join(ret)
 
 
def _main():
    from sys import stdin
 
    n = int(input())
 
    lst = DoublyLinkedList()
     
    for _ in range(n):
        cmd = stdin.readline().strip().split()
        if cmd[0] == 'insert':
            lst.insert(cmd[1])
        elif cmd[0] == 'delete':
            lst.delete_num(cmd[1])
        elif cmd[0] == 'deleteFirst':
            lst.delete_start()
        elif cmd[0] == 'deleteLast':
            lst.delete_last()
 
    print(lst.get_content())
 
 
if __name__ == '__main__':
    _main()

Solution (Python):
from collections import deque

if __name__ == "__main__":
    N = int(input())

    num_list = deque([])
    for i in range(N):
        command = input()
        if "insert" in command:
            key = command.split()[1]
            num_list.appendleft(key)
        else:
            if "First" in command:
                num_list.popleft()
            elif "Last" in command:
                num_list.pop()
            else:
                key = command.split()[1]
                if (key in num_list):
                    num_list.remove(key)

    print(" ".join(list(num_list)))

Solution (Python):
import sys

class Node: 
    def __init__(self, x, y = None, z = None): 
        self.data = x 
        self.prev = y 
        self.next = z 

class LinkedList: 
    def __init__(self): 
        self.top = Node(None) 
        self.top.next = self.top
        self.top.prev = self.top
    def insert(self, x):
        a = Node(x)
        self.top.next.prev = a
        a.next = self.top.next
        self.top.next = a
        a.prev = self.top
    def search(self, x):
        nil = self.top
        node = nil.next
        while node != nil and node.data != x:
            node = node.next
        return node
    def _delete(self, node):
        if node != self.top:
            np = node.prev
            nn = node.next
            np.next = nn
            nn.prev = np
    def delete_data(self, x):
        node = self.search(x)
        self._delete(node)
    def deleteFirst(self):
        nil = self.top
        self._delete(nil.next)
    def deleteLast(self):
        nil = self.top
        self._delete(nil.prev)
    def output(self):
        nil = self.top
        node = nil.next
        datalist = []
        while node != nil:
            datalist.append(node.data)
            node = node.next
        print(" ".join(datalist))

n = int(input()) 
L = LinkedList()
nil = L.top
for cmd in sys.stdin:
    cmd = cmd.strip("\n")
    if "insert" in cmd:
        L.insert(cmd[7:])
    elif "deleteFirst" in cmd:
        L.deleteFirst()
    elif "deleteLast" in cmd: 
        L.deleteLast()
    elif "delete " in cmd:
        L.delete_data(cmd[7:])

L.output()

Solution (Python):
from collections import deque

n = int(input())
dq = deque()
for _ in range(n):
    query = input()
    if query == "deleteFirst":
        dq.popleft()
    elif query == "deleteLast":
        dq.pop()
    else:
        op, arg = query.split()
        if op == "insert":
            dq.appendleft(arg)
        else:
            tmp = deque()
            while dq:
                item = dq.popleft()
                if item == arg:
                    break
                else:
                    tmp.append(item)
            while tmp:
                dq.appendleft(tmp.pop())
print(*dq)

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):
from collections import deque
import sys
input = sys.stdin.readline

n = int(input())
lst = deque([])
for i in range(n):
    inp = input().split()
    op = inp[0]
    try:
        x = inp[1]
    except:
        pass
    if op == 'insert':
        lst.appendleft(x)
    if op == 'delete':
        try:
            lst.remove(x)
        except:
            pass
    if op == 'deleteFirst':
        lst.popleft()
    if op == 'deleteLast':
        lst.pop()
print(*lst)

Solution (Python):
from collections import deque

d=deque()

N=int(input())

for n 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)
        elif a=="delete":
            if d.count(b):
                d.remove(b)
                
print(' '.join(list(d)))

Solution (Python):
from collections import deque

L = deque()

n = int(input())

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

    if input_com == "deleteFirst":
        L.popleft()
    elif input_com == "deleteLast":
        L.pop()
    else:
        com, num = input_com.split()

        if com == "insert":
            L.appendleft(num)
        elif com == "delete":
            try:
                L.remove(num)
            except:
                pass

print(*L)

Solution (Python):

from collections import deque
class DoubleLinkyList:
    def __init__(self):
        self.lst=[]
    def insert(self,x):
        self.lst.insert(0,x)
    def delete_x(self,x):
        if x in self.lst:
            self.lst.pop(self.lst.index(x))
    def deleteFirst(self):
        self.lst.pop(0)
    def deleteLast(self):
        self.lst.pop(-1)
        
n = int(input())
dll = deque()

for i in range(n):
    A = input().split(" ")
    if A[0] == "insert":
        dll.appendleft((A[1]))
        
    if A[0] == "delete" and A[1] in dll:
        dll.remove((A[1]))
    if A[0]=="deleteFirst":
        dll.popleft()
    if A[0]=="deleteLast":
        dll.pop()

result = list(dll)
print(" ".join(result))

Solution (Python):
class Element:
    def __init__(self, x):
        self.x = x
        self.lh = None
        self.rh = None

class MyDeque:
    def __init__(self):
        self.top = self.tail = None
    
    def insert(self, x):
        elm = Element(x)
        if self.top is None:
            self.top = self.tail = elm
        else:
            elm.rh = self.top
            self.top.lh = self.top = elm
        
    def delete(self, x):
        elm = self.top
        while elm is not None:
            if elm.x == x:
                if elm.lh is None and elm.rh is None:
                    self.top = self.tail = None
                else:    
                    if elm.lh is None:
                        self.top = elm.rh
                    else:
                        elm.lh.rh = elm.rh
                        
                    if elm.rh is None:
                        self.tail = elm.lh
                    else:
                        elm.rh.lh = elm.lh
                break
            
            elm = elm.rh
            
    def delete_first(self):
        if self.top is self.tail:
            self.top = self.tail = None
        else:
            self.top = self.top.rh
            self.top.lh = None
                
    def delete_last(self):
        if self.top is self.tail:
            self.top = self.tail = None
        else:
            self.tail = self.tail.lh
            self.tail.rh = None
                
    
    def print_que(self):
        elm = self.top
        q=[]
        while elm is not None:
            q.append(elm.x)
            elm = elm.rh
        print(*q)

from sys import stdin

que = MyDeque()
N = int(input())
for _ in range(N):
    cmd = stdin.readline().strip().split()
    if cmd[0] == 'insert':
        que.insert(cmd[1])
        
        
    elif cmd[0] == 'delete':
        que.delete(cmd[1])
        
        
    elif cmd[0] == 'deleteFirst':
        que.delete_first()
        
        
    elif cmd[0] == 'deleteLast':
        que.delete_last()
        
    
que.print_que()

Solution (Python):
import sys

class Node:
    """
    key: Value of the node
    next: Pointer to the next node
    prev: Pointer to the previous node
    """

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


class DoublyLinkedList:
    def __init__(self):
        """
        next always point to the very first node in a list
        prev always point to the last node in a list
        """
        self.next = None
        self.prev = None

    def insert(self, key):
        """
        :param key: key to insert into a list (inserted to the first place in the list)
        :return: None
        """
        newNode = Node(key)
        
        if self.next is None:
            newNode.next = None
            newNode.prev = self
            self.next = newNode
            self.prev = newNode
        
        else:
            newNode.next = self.next
            newNode.prev = self
            self.next.prev = newNode
            self.next = newNode

    def listSearch(self, key):
        """
        :param key: key of a note to search for
        :return: Node (the first Node from the beginning of a list)
        """
        cur = self.next
        while cur is not None and cur.key != key:
            cur = cur.next
        return cur

    def deleteNode(self, node):
        """
        :param node: Node to delete
        :return: None
        """
        if node is None:
            return
        
        elif node.next is None:
            node.prev.next = None
            self.prev = node.prev
        
        else:
            node.prev.next = node.next
            node.next.prev = node.prev

    def delete(self, key):
        """
        Delete the first node that has the key.
        :param key: key that the node to be deleted has
        :return: None
        """
        self.deleteNode(self.listSearch(key))

    def deleteFirst(self):
        """
        Delete the first node in a list
        :return: None
        """
        cur = self.next
        self.deleteNode(cur)

    def deleteLast(self):
        """
        Delete the last node in a list
        :return: None
        """
        cur = self.prev
        self.deleteNode(cur)

    def showKeys(self):
        """
        Print keys of nodes in a list divided by a space
        :return: None
        """
        cur = self.next
        keys = []
        while cur is not None:
            keys.append(cur.key)
            cur = cur.next
        keys = map(str, keys)
        print(' '.join(keys))

n = int(input())
d = DoublyLinkedList()


for i in sys.stdin:
    if 'insert' in i:
        x = i[7:-1]
        d.insert(x)
    elif 'deleteFirst' in i:
        d.deleteFirst()
    elif 'deleteLast' in i:
        d.deleteLast()
    elif 'delete' in i:
        x = i[7:-1]
        d.delete(x)
    else:
        pass

d.showKeys()

Solution (Python):
from collections import deque
import sys
input = sys.stdin.readline
n = int(input())
d = deque()
for _ in range(n):
    l = input().strip()
    if "deleteFirst" == l:d.popleft();continue
    elif "deleteLast" == l: d.pop();continue
    c, s = l.split()
    if c == "insert": d.appendleft(s);continue
    elif s in d: d.remove(s)
print(*d)

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):
from collections import deque 

n = int(input())

data = deque()

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

    elif command[0] == "deleteLast":
        data.pop()

print(" ".join(data))

Solution (Python):

from collections import deque

linkedlist = deque()

for _ in range(int(input())):
    input_command = input()
    if input_command == 'deleteFirst':
        linkedlist.popleft()

    elif input_command == 'deleteLast':
        linkedlist.pop()
    
    else:
        com, num = input_command.split()
        if com == 'insert':
            linkedlist.appendleft(num)

        elif com == 'delete':
            try:
                linkedlist.remove(num)
            except:
                pass
            

print(' '.join(linkedlist))

Solution (Python):
from collections import deque

def doubly(n):
    list = deque()
    for i in range(n):
        line = input().split()
        p = line[0]
        while True:
            try:
                key = line[1]
                break
            except IndexError:
                break
        if p == "insert":
            list.appendleft(key)
        elif p == "deleteFirst":
            list.popleft()
        elif p == "deleteLast":
            list.pop()
        else:
            while True:
                try:
                    list.remove(key)
                    break
                except ValueError:
                    break
    print(' '.join(list))

if "__main__" == __name__:
    n = int(input())
    doubly(n)

Solution (Python):
from collections import deque

n = int(input())

answer = deque()
for i in range(n):
    command = input()
    if " " in command:
        command, key = command.split()
    
    if command == "insert":
        answer.appendleft(key)
    elif command == "delete":
        if key in answer:
            answer.remove(key)
    elif command == "deleteFirst":
        answer.popleft()
    elif command == "deleteLast":
        answer.pop()

print(*answer)

Solution (Python):
from collections import deque


def main():
    d = deque()
    for _ in range(int(input())):
        command = input()
        if command[0] == 'i':
            d.appendleft(command[7:])
        elif command[6] == ' ':
            try:
                d.remove(command[7:])
            except ValueError:
                pass
        elif len(command) == 11:
            d.popleft()
        else:
            d.pop()
    print(*d)


if __name__ == '__main__':
    main()

Solution (Python):
import collections


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

for _ in range(n):
    input_value = input().split()
    if input_value[0] == 'insert':
        Q.appendleft(input_value[1])
    elif input_value[0] == 'delete':
        if input_value[1] in Q:
            Q.remove(input_value[1])
    elif input_value[0] == 'deleteFirst':
        Q.popleft()
    elif input_value[0] == 'deleteLast':
        Q.pop()
    else:
        raise Error("illegal state")

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

Solution (Python):
n = int(input())
import collections
d = collections.deque([])
for _ in range(n):
    s = input().split()
    if s[0] == "deleteFirst":
        d.popleft()
    elif s[0] == "deleteLast":
        d.pop()
    elif s[0] == "insert":
        d.appendleft(s[1])
    else:
        try:
            d.remove(s[1])
        except:
            pass
print(" ".join(d))

Solution (Python):
from collections import deque

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

print(*l)

Solution (Python):
import collections,sys
def s():
 d=collections.deque()
 input()
 for e in sys.stdin:
  if'i'==e[0]:d.appendleft(e[7:-1])
  else:
   if' '==e[6]:
    m=e[7:-1]
    if m in d:d.remove(m)
   elif'i'==e[7]:d.popleft()
   else:d.pop()
 print(*d)
if'__main__'==__name__:s()

Solution (Python):
from collections import deque

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

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

    if   p[0] == "insert":
        dll.appendleft(p[1])

    elif p[0] == "delete":
        try:
            dll.remove(p[1])
        except:
            pass

    elif p[0] == "deleteFirst":
        dll.popleft()

    else:
        dll.pop()

print(" ".join(dll))

Solution (Python):
from collections import deque
que = deque([])

n = int(input())

for _ in range(n):
    input_command = input()
    if input_command == "deleteFirst":
        que.popleft()
    elif input_command == "deleteLast":
        que.pop()
    else:
        command, x = input_command.split()
        if command == "insert":
            que.appendleft(x)
        elif command == "delete":
            try:
                que.remove(x)
            except:
                pass
print(" ".join(que))

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

Solution (Python):


import collections
import sys
deque = collections.deque([])

n = int(input())
for i in range(n):
    query = sys.stdin.readline().split()
    if query[0] == "insert":
        deque.appendleft(int(query[1]))
    elif query[0] == "deleteFirst":
        deque.popleft()
    elif query[0] == "deleteLast":
        deque.pop()
    elif query[0] == "delete":
        try:
            deque.index(int(query[1]))
        except:
            pass
        else:
            deque.remove(int(query[1]))

print(*deque)

Solution (Python):
from collections import deque
n = int(input())
my_list = deque()
for t in range(n):
    i = input().split()
    if i[0] == "insert":
        my_list.appendleft(i[1])
    elif i[0] == "delete" and i[1] in my_list:
        my_list.remove(i[1])
    elif i[0] == "deleteFirst":
        my_list.popleft()
    elif i[0] == "deleteLast":
        my_list.pop()
print(" ".join(my_list))

Solution (Python):
from collections import deque
lis = deque()
num = int(input())
for _ in range(num):
  command = input().split()
  if command[0]=='deleteFirst':
    lis.popleft()
  elif command[0]=='deleteLast':
    lis.pop()
  elif command[0]=='insert':
    lis.insert(0, command[1])
  elif command[0]=='delete':
    try:
        lis.remove(command[1])
    except:
        pass
print(' '.join(lis))

Solution (Python):
import sys
from collections import deque 

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

input()
a = deque()

while True:
	op = input().split()
	if not op:
		break
	if op[0] == 'insert':
		a.appendleft(int(op[1]))
	elif op[0] == 'delete':
		try:
			a.remove(int(op[1]))
		except:
			pass
	elif op[0] == 'deleteFirst':
		a.popleft()
	elif op[0] == 'deleteLast':
		a.pop()

while True:
	x = a.popleft()
	print(x, end="")
	if len(a)>0:
		print(" ", end="")
	else:
		print("")
		break

Solution (Python):
from collections import deque
import sys

def main():
    y = deque()
    input()
    for order in sys.stdin:
        if order[0] == "i":
            y.appendleft(order[7:-1])
        elif order[6] == " ":
            try:
                y.remove(order[7:-1])
            except:
                pass
        elif order[6] == "F":
            y.popleft()
        else:
            y.pop()
    print(" ".join(y))
    
if __name__ == "__main__":
    main()

Solution (Python):
import collections


num_op = int(input())
dllist = collections.deque()

for i in range(0, num_op):
    op = input().split(' ')
    if op[0] == 'insert':
        dllist.appendleft(op[1])
    elif op[0] == 'delete':
        if op[1] in dllist:
            dllist.remove(op[1])
    elif op[0] == 'deleteFirst':
        dllist.popleft()
    elif op[0] == 'deleteLast':
        dllist.pop()

print(' '.join(dllist))

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:
        command,num=command.split()
        num=int(num)
        if command=="insert":
            que.appendleft(num)
        else:
            if num in que:
                que.remove(num)

print(*que,sep=" ")

Solution (Python):
from collections import deque

N = int(input())
L = deque()

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

    if C[0] == "insert":
        L.appendleft(C[1])

    elif C[0] == "delete":
        try:
            L.remove(C[1])
        except ValueError:
                pass
    elif C[0] == "deleteFirst":
        L.popleft()
    elif C[0] == "deleteLast":
        L.pop()
    
print(" ".join(L))

Solution (Python):
from collections import deque

n=int(input())
l=deque()
for i in range(n):
    com=input()
    if(com=="deleteFirst"):
        l.popleft()
    elif(com=="deleteLast"):
        l.pop()
    else:
        com,val=com.split()
        if(com=="insert"):
            l.appendleft(val)
        elif(com=="delete"):
            try:
                l.remove(val)
            except:
                pass

print(' '.join(l))