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
query = int(input())
l = deque()
for _ in range(query):
    command = input().strip()
    if command == "deleteFirst":
        try:
            l.popleft()
        except:
            pass
    elif command == "deleteLast":
        try:
            l.pop()
        except:
            pass
    else:
        command, value = command.split(" ")
        if command == "insert":
            l.appendleft(int(value))
        elif command == "delete":
            try:
                l.remove(int(value))
            except:
                pass
print(*l)

Solution (Python):
from collections import deque

dq=deque()
n=int(input())

for i in range(n):
	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):
from collections import deque

dq=deque()
n=int(input())

for i in range(n):
	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):
from collections import deque
n = int(input())
dlist = deque()
for i in range(n):
    code = input().split()
    if code[0] == "insert":
        dlist.insert(0,code[1])
    if code[0] == "delete":
        try:
            dlist.remove(code[1])
        except:
            continue
    if code[0] == "deleteFirst":
        dlist.popleft()
    if code[0] == "deleteLast":
        dlist.pop()
print(*dlist,sep=" ")

Solution (Python):
import sys
from collections import deque
 
n = int(sys.stdin.readline())
que = deque()
for i in range(n):
    line = sys.stdin.readline()
    op = line.rstrip().split()
    if "insert" == op[0]:
        que.appendleft(op[1])
    elif "delete" == op[0]:
        if op[1] in que:
            que.remove(op[1])
    elif "deleteFirst" == op[0]:
        que.popleft()
    else:
        que.pop()
 
print(*que)

Solution (Python):
from collections import deque

dq=deque()
n=int(input())

for i in range(n):
	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):


from collections import deque

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

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

    print(" ".join(L))

Solution (Python):
from collections import deque
dq = deque()
for _ in range(int(input())):
    ss = input()
    if ss == "deleteFirst":
        del dq[0]
    elif ss == "deleteLast":
        del dq[-1]
    else:
        c, s = ss.split()
        s = int(s)
        if c == "insert":
            dq.appendleft(s)
        elif c == "delete":
            if s in dq:
                dq.remove(s)
print(*dq)

Solution (Python):
import sys
from collections import deque
readline = sys.stdin.readline
dq = deque()
for _ in range(int(input())):
    ss = readline().strip()
    if ss == "deleteFirst":
        del dq[0]
    elif ss == "deleteLast":
        del dq[-1]
    else:
        c, s = ss.split()
        s = int(s)
        if c == "insert":
            dq.appendleft(s)
        elif c == "delete":
            if s in dq:
                dq.remove(s)
print(*dq)

Solution (Python):
import sys
from collections import deque
readline = sys.stdin.readline
dq = deque()
for _ in range(int(input())):
    c = readline().split()
    if c[0] == "deleteFirst":
        del dq[0]
    elif c[0] == "deleteLast":
        del dq[-1]
    else:
        if c[0] == "insert":
            dq.appendleft(c[1])
        elif c[0] == "delete":
            if c[1] in dq:
                dq.remove(c[1])
print(*dq)

Solution (Python):
import sys
from collections import deque
readline = sys.stdin.readline
dq = deque()
for _ in range(int(input())):
    c = readline().split()
    if c[0] == "deleteFirst":
        dq.popleft()
    elif c[0] == "deleteLast":
        dq.pop()
    else:
        if c[0] == "insert":
            dq.appendleft(c[1])
        elif c[0] == "delete":
            if c[1] in dq:
                dq.remove(c[1])
print(*dq)

Solution (Python):
import collections
d=collections.deque()
for _ in range(int(input())):
 e=input()
 if'i'==e[0]:d.appendleft(e.split()[1])
 else:
  if' '==e[6]:
   m=e.split()[1]
   if m in d:d.remove(m)
  elif len(e)%2:d.popleft()
  else:d.pop()
print(*d)

Solution (Python):
import collections
d=collections.deque()
for _ in range(int(input())):
 e=input()
 if'i'==e[0]:d.appendleft(e.split()[1])
 else:
  if' '==e[6]:
   m=e.split()[1]
   if m in d:d.remove(m)
  elif len(e)%2:d.popleft()
  else:d.pop()
print(*d)

Solution (Python):
import collections,sys
d=collections.deque()
input()
for e in sys.stdin.readlines():
 if'i'==e[0]:d.appendleft(e.split()[1])
 else:
  if' '==e[6]:
   m=e.split()[1]
   if m in d:d.remove(m)
  elif len(e)%2:d.pop()
  else:d.popleft()
print(*d)

Solution (Python):
import collections,sys
d=collections.deque()
input()
for e in sys.stdin.readlines():
 if'i'==e[0]:d.appendleft(e.split()[1])
 else:
  if' '==e[6]:
   m=e.split()[1]
   if m in d:d.remove(m)
  elif len(e)%2:d.pop()
  else:d.popleft()
print(*d)

Solution (Python):
import collections,sys
d=collections.deque()
input()
for e in sys.stdin.readlines():
 if'i'==e[0]:d.appendleft(e.split()[1])
 else:
  if' '==e[6]:
   m=e.split()[1]
   if m in d:d.remove(m)
  elif len(e)%2:d.pop()
  else:d.popleft()
print(*d)

Solution (Python):
import collections

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

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

    if line[0] == 'i':
        que.appendleft(int(line[7:]))
    elif line[6] == ' ':
        try:
            que.remove(int(line[7:]))
        except ValueError:
            pass
    elif line[6] == 'F':
        que.popleft()
    else:
        que.pop()

print(*que)

Solution (Python):
import collections,sys
d=collections.deque()
input()
for e in sys.stdin:
 if'i'==e[0]:d.appendleft(e.split()[1])
 else:
  if' '==e[6]:
   m=e.split()[1]
   if m in d:d.remove(m)
  elif len(e)%2:d.pop()
  else:d.popleft()
print(*d)

Solution (Python):
import collections,sys
d=collections.deque()
input()
for e in sys.stdin:
 if'i'==e[0]:d.appendleft(e.split()[1])
 else:
  if' '==e[6]:
   m=e.split()[1]
   if m in d:d.remove(m)
  elif len(e)%2:d.pop()
  else:d.popleft()
print(*d)

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):
class Node(object):
    def __init__(self, num, prev = None, nxt = None):
        self.num = num;
        self.prev = prev;
        self.nxt = nxt;

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

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

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

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

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

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

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

main();

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

Solution (Python):
from collections import deque

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

print(' '.join(dq))

Solution (Python):
import collections
hoge = collections.deque()
n = int(input())
for _ in range(n):
    commands = input().split()
    if len(commands) == 1:
        if commands[0] == 'deleteFirst':
            hoge.popleft()
        else:
            hoge.pop()
    else:
        com, x = commands
        if com == 'insert':
            hoge.insert(0, x)
        else:
            if x in hoge:
                hoge.remove(x)
print (' '.join(hoge))

Solution (Python):

from collections import deque

dll = deque()

n = int(input())
for _ in range(n):
  raw = input().split()
  command = raw[0]
  if command == 'delete': 
    value = raw[1]
    try:
      dll.remove(value)
    except ValueError as err:
      continue
  elif command == 'insert':
    value = raw[1]
    dll.appendleft(value)
  elif command == 'deleteFirst':
    try:
      dll.popleft()
    except IndexError as err:
      continue
  elif command == 'deleteLast':
    try:
      dll.pop()
    except IndexError as err:
      continue

print(" ".join(dll))

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

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

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

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

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

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

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

Solution (Python):
from collections import deque

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

Solution (Python):
import collections

a = collections.deque()

for i 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):
import sys


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

    def __init__(self):
        self.head = self.Node()
        self.tail = self.Node()
        self.head.next = self.tail
        self.tail.prev = self.head

    def insert(self, v):
        head = self.head
        node = self.Node(v, head, head.next)
        head.next.prev = node
        head.next = node

    def delete(self, node):
        if node in (self.head, self.tail):
            return
        else:
            node.prev.next, node.next.prev = node.next, node.prev

    def delete_first(self):
        self.delete(self.head.next)

    def delete_last(self):
        self.delete(self.tail.prev)

    def delete_first_value(self, v):
        node = self.find(v, self.head)
        if node is not None:
            self.delete(node)

    def find(self, v, cur=None):
        if cur is None:
            cur = self.head

        while cur != self.tail:
            cur = cur.next
            if cur.value == v:
                return cur

        return None

    def __str__(self):
        cur = self.head.next
        vs = ''
        while cur != self.tail:
            vs += cur.value
            cur = cur.next
            if cur != self.tail:
                vs += ' '

        return vs


def run():
    steps = int(input())  
    dl = DoubleLinkedList()

    for command in sys.stdin:
        if command.startswith('insert'):
            dl.insert(command[7:-1])
        elif command.startswith('deleteFirst'):
            dl.delete_first()
        elif command.startswith('deleteLast'):
            dl.delete_last()
        elif command.startswith('delete'):
            dl.delete_first_value(command[7:-1])
        else:
            raise ValueError()

    print(dl)


if __name__ == '__main__':
    run()

Solution (Python):
from collections import deque
d = deque()
for _ in range(int(input())):
  a = input()
  if "i" == a[0]: d.appendleft(int(a[7:]))
  elif "F" == a[6]: d.popleft()
  elif "L" == a[6]: d.pop()
  else:
    try: d.remove(int(a[7:]))
    except: pass
print(*d)

Solution (Python):
from collections import deque
import sys

n = int(input())
result = deque()
func = {'insert':result.appendleft,\
        'delete':result.remove,\
        'deleteFirst':result.popleft,\
        'deleteLast':result.pop}

for line in sys.stdin.readlines():
    c = line.split()
    if len(c) > 1:
        try:
            func[c[0]](int(c[1]))
        except:
            continue
    else:
        func[c[0]]()

print(*result)

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

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 len(e)%2:d.pop()
   else:d.popleft()
 print(*d)
s()

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 len(e)%2:d.pop()
   else:d.popleft()
 print(*d)
s()

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 len(e)%2:d.pop()
   else:d.popleft()
 print(*d)
if'__main__'==__name__:s()

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 len(e)%2:d.pop()
   else:d.popleft()
 print(*d)
if'__main__'==__name__:s()

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 len(e)%2:d.pop()
   else:d.popleft()
 print(*d)
if'__main__'==__name__:s()

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 len(e)%2:d.pop()
   else:d.popleft()
 print(*d)
if'__main__'==__name__:s()

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 len(e)&1:d.pop()
   else:d.popleft()
 print(*d)
if'__main__'==__name__:s()

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 len(e)&1:d.pop()
   else:d.popleft()
 print(*d)
if'__main__'==__name__:s()

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):
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):
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())
a = deque()
for i in range(n):
    c = input()
    if c == "deleteFirst":
        a.popleft()
    elif c == "deleteLast":
        a.pop()
    else:
        c, v = c.split()
        if c == "insert":
            a.appendleft(v)
        else:
            try:
                a.remove(v)
            except:
                pass
print(" ".join(a))

Solution (Python):



from collections import deque

keys = deque([])

n = int(input())

for i in range(n):
    command = input()
    if command == 'deleteFirst':
        keys.popleft()
    elif command == 'deleteLast':
        keys.pop()
    else:
        command, x = command.split(' ')
        if command == 'insert':
            keys.appendleft(int(x))
        elif command == 'delete':
            if int(x) in keys:
                keys.remove(int(x))

print(' '.join([str(k) for k in keys]))

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


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


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


def insert(double_lst, key):
    node = Node(key, double_lst.nil, double_lst.nil.next)
    double_lst.nil.next.prev = node
    double_lst.nil.next = node


def search(double_lst, key):
    cur = double_lst.nil.next

    while cur != double_lst.nil and cur.key != key:
        cur = cur.next

    return cur


def delete_node(double_lst, node):
    if node == double_lst.nil:
            return

    node.prev.next = node.next
    node.next.prev = node.prev


def delete_first(double_lst):
    delete_node(double_lst, double_lst.nil.next)


def delete_last(double_lst):
    delete_node(double_lst, double_lst.nil.prev)


def delete_key(double_lst, key):
    node = search(double_lst, key)
    delete_node(double_lst, node)


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

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

        if order[0] == "insert":
            insert(ll, int(order[1]))
        elif order[0] == "delete":
            delete_key(ll, int(order[1]))
        elif order[0] == "deleteFirst":
            delete_first(ll)
        else:
            delete_last(ll)

    cur = ll.nil
    while cur.next != ll.nil:
        cur = cur.next
        if cur.next == ll.nil:
            print(cur.key)
        else:
            print(cur.key, end=" ")


if __name__ == '__main__':
    main()

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

def pr(A,n):
    for i in range(n):
        if i!=n-1:
            print(A[i],end=" ")
        else:
            print(A[i])

n=int(input())
que_r=deque()
data=deque()

for i in range(n):
    s=input()
    l=len(s)
    if s[0]=="i":
        que_r.appendleft(s[7:])
    elif s[6]==" ":
        try:
            que_r.remove(s[7:])
        except: pass
    elif l>10:
        que_r.popleft()
    elif l>6:
        que_r.pop()

pr(que_r,len(que_r))

Solution (Python):
from collections import deque

n=int(input())
que_r=deque()

for i in range(n):
    s=input()
    l=len(s)
    if s[0]=="i":
        que_r.appendleft(s[7:])
    elif s[6]==" ":
        try:
            que_r.remove(s[7:])
        except: pass
    elif l>10:
        que_r.popleft()
    elif l>6:
        que_r.pop()

print(*que_r)

Solution (Python):
from collections import deque


n=int(input())
que_r=deque()

for i in range(n):
    s=input()
    l=len(s)
    if s[0]=="i":
        que_r.appendleft(s[7:])
    elif s[6]==" ":
        try:
            que_r.remove(s[7:])
        except: pass
    elif l>10:
        que_r.popleft()
    elif l>6:
        que_r.pop()

print(*que_r)

Solution (Python):
from collections import deque

def pr(a):
    for i in range(len(a)):
        if i == len(a)-1:
            print(a[i])
        else:
            print(a[i],end = " ")

n = int(input())
q = deque()
for i in range(n):
    x = input()
    l = len(x)
    if x[0] == "i":
        q.insert(0,x[7:])
    elif x[6] == " ":
        try:
            q.remove(x[7:])
        except:
            pass
    elif l > 10:
        q.popleft()
    elif l > 6:
        q.pop()

print(*q)

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

Solution (Python):
from collections import deque


n = int(input())
L = deque()

for i in range(n):
    com = input()
    if com[0] == 'i':
        C = com.split()
        L.appendleft(int(C[1]))
    else:
        if com[6] == 'F':
            L.popleft()
        elif com[6] == 'L':
            L.pop()
        else:
            C = com.split()
            x = int(C[1])
            try:
                L.remove(x)
            except:
                pass

print(*L)

Solution (Python):
from collections import deque

n = int(input())
L = deque()

for i in range(n):
    com = input()
    if com[0] == 'i':
        C = com.split()
        L.appendleft(int(C[1]))
    else:
        if com[6] == 'F':
            L.popleft()
        elif com[6] == 'L':
            L.pop()
        else:
            C = com.split()
            x = int(C[1])
            try:
                L.remove(x)
            except:
                pass

print(*L)

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