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

class Node(object):
  def __init__(self, num, prv = None, nxt = None):
    self.num = num
    self.prv = None
    self.nxt = None

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 = new_elem
        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 D_Linked_List:
    class Node:
        def __init__(self, key, next = None, prev = None):
            self.next = next
            self.prev = prev
            self.key = key
 
    def __init__(self):
        self.nil = D_Linked_List.Node(None)
        self.nil.next = self.nil
        self.nil.prev = self.nil
 
    def insert(self, key):
        node_x = D_Linked_List.Node(key, self.nil.next, self.nil)
        self.nil.next.prev = node_x
        self.nil.next = node_x
 
    def _listSearch(self, key):
        cur_node = self.nil.next
        while (cur_node != self.nil) and (cur_node.key != key):
            cur_node = cur_node.next
        return cur_node
 
    def _deleteNode(self, node):
        if node == self.nil:
            return None
        node.prev.next = node.next
        node.next.prev = node.prev
 
    def deleteFirst(self):
        self._deleteNode(self.nil.next)
 
    def deleteLast(self):
        self._deleteNode(self.nil.prev)
 
    def deleteKey(self, key):
        node = self._listSearch(key)
        self._deleteNode(node)
 
    def show_keys(self):
        cur_node = self.nil.next
        keys = []
        while cur_node != self.nil:
            keys.append(cur_node.key)
            cur_node = cur_node.next
        print(' '.join(keys))
 
 
import sys
 
d_ll = D_Linked_List()
 
for i in sys.stdin:
    if 'insert' in i:
        x = i[7:-1]
        d_ll.insert(x)
    elif 'deleteFirst' in i:
        d_ll.deleteFirst()
    elif 'deleteLast' in i:
        d_ll.deleteLast()
    elif 'delete' in i:
        x = i[7:-1]
        d_ll.deleteKey(x)
    else:
        pass
 
d_ll.show_keys()

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

for _ in range(n):
  c = input()
  if c == "deleteFirst":
    li.popleft()
  elif c == "deleteLast":
    li.pop()
  else:
    c, v = c.split()
    if c == "insert":
      li.appendleft(v)
    else:
      try: 
        li.remove(v)
      except:
        pass

li = list(li)
print(' '.join(map(str, li)))

Solution (Python):
from collections import deque

n = int(input())
dll = deque()
for i in range(n):
    command = input()
    if command == 'deleteFirst':
        dll.popleft()
    elif command == 'deleteLast':
        dll.pop()
    else:
        state, x = command.split()
        if state == 'insert':
            dll.appendleft(x)
        elif state == 'delete':
            try:
                dll.remove(x)
            except:
                pass
print(*dll)

Solution (Python):
from collections import deque


n = int(input())

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

print(' '.join(l))

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

    dll = DoublyLinkedList()

    for _ in range(n):
        com = input().rstrip()
        
        
        if com[0] == 'i':
            dll.insert(com[7:])
        elif com[6] == 'F':
            dll.deleteFirst()
        elif com[6] == 'L':
            dll.deleteLast()
        else:
            dll.deleteKey(com[7:])

    cur = dll.nil.next
    ans = []
    while cur != dll.nil:
        ans.append(cur.key)
        cur = cur.next

    print(' '.join(ans))

Solution (Python):
from collections import deque
c=deque([])
n=int(input())
for i in range(n):
    a=input()
    if "deleteFirst"in a:
        c.popleft()
    elif "deleteLast"in a:
        c.pop()
    else:
        a,b=a.split()
        if "insert"in a:
            c.appendleft(b)
        else:
            try:
                c.remove(b)
            except:
                continue

print(*c)

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


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

nil = Node(None)



def init():
    nil.next = nil
    nil.prev = nil

def insert(x):
    node = Node(x)
    
    node.next = nil.next
    nil.next.prev = node
    nil.next = node
    node.prev = nil

def delete(x):
    
    deleteNode(listSearch(x))

def deleteFirst():
    deleteNode(nil.next)

def deleteLast():
    deleteNode(nil.prev)

def deleteNode(node):
    
    if node is nil:
        return
    node.prev.next = node.next
    node.next.prev = node.prev
    del node

def listSearch(x):
    
    cur = nil.next
    while cur is not nil and cur.data != x:
        cur = cur.next
    return cur


init()

N = int(input())
for i in range(N):
    command = input().strip().split()
    if command[0] == 'deleteFirst':
        deleteFirst()
    elif command[0] == 'deleteLast':
        deleteLast()
    elif command[0] == 'delete':
        delete(command[1])
    elif command[0] == 'insert':
        insert(command[1])

while nil.next is not nil:
    if nil.next.next is not nil:
        print(nil.next.data, end=" ")
    else:
        print(nil.next.data)
    nil.next = nil.next.next

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())
ans = deque()
for i in range(n):
    t = input().split()
    op = t[0]
    if op == "insert":
        ans.appendleft(t[1])
    elif op == "delete":
        try:
            ans.remove(t[1])
        except ValueError:
            pass
    elif op == "deleteFirst":
        ans.popleft()
    elif op == "deleteLast":
        ans.pop()

print(" ".join(ans))

Solution (Python):
from collections import deque


a = deque()
n = int(input())
for i in range(n):
    command = input().split()
    com = command[0]
    if com == "insert":
        a.appendleft(command[1])
    elif com == "delete":
        if command[1] in a:
            a.remove(command[1])
    elif com == "deleteFirst":
        a.popleft()
    elif com == "deleteLast":
        a.pop()
print(" ".join(map(str, a)))

Solution (Python):

from collections import deque
box = deque()
n = int(input())
for _ in range(n):
    a = input()
    if a[0] == "i":
        box.appendleft(a[7:])
    else:
        if a[6] == " ":
            num = a[7:]
            if num in box:box.remove(num)
        elif a[6] == "F":box.popleft()
        else:box.pop()
            
print(*box)

Solution (Python):

from collections import deque

n = int(input().rstrip())
num = deque()
for loop in range(n):
    l = input().rstrip().split()
    if l[0] == "deleteFirst":
        num.popleft()
    elif l[0] == "deleteLast":
        num.pop()
    else:
        if l[0] == "insert":
            num.appendleft(l[1])
        else:
            try:
                num.remove(l[1])
            except ValueError:
                pass

print(' '.join(num))

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

 
doubly_linked_list = deque()
for i in range(N):
    commands = input().split()
    command = commands[0]
    if len(commands) == 2:
        key = commands[1]
        if command[0] == 'd':
            try :
                doubly_linked_list.remove(key)
                continue
            except:
                continue
        else: 
            doubly_linked_list.appendleft(key)
            continue
    else :
        if command[6] == 'F':
            doubly_linked_list.popleft()
            continue
        else:
            doubly_linked_list.pop()
            continue
 
    

print(' '.join(doubly_linked_list))

Solution (Python):
from collections import deque

if __name__ == '__main__':
    que = deque()
    for _ in range(int(input())):
        buff = input().split()
        if buff[0] == "insert":
            que.appendleft(buff[1])
        elif buff[0] == "delete":
            try:
                que.remove(buff[1])
            except ValueError:
                pass
        elif buff[0] == "deleteFirst":
            que.popleft()
        else:
            que.pop()

    print(" ".join(que))

Solution (Python):
import sys,collections
q=collections.deque();input()
for s in sys.stdin:
 if"n"in s:q.appendleft(s.split()[1])
 elif"F"in s:q.popleft()
 elif"L"in s:q.pop()
 else:
  try:q.remove(s.split()[1])
  except:pass
print(*q)

Solution (Python):
from collections import deque

num = int(input())
list = deque([])

for i in range(num):
        command = input().split()
        if command[0] == "insert":
                list.insert(0, command[1])
        elif command[0] == "delete":
                if command[1] in list:
                        list.remove(command[1])
        elif command[0] == "deleteFirst":
                list.popleft()
        elif command[0] == "deleteLast":
                list.pop()

print(" ".join(list))

Solution (Python):
from collections import deque

n = int(input())
Q = deque([])
for i in range(n):
    command = input()
    if command[0] == 'i':
        Q.appendleft(command[7:])
    elif command[6] == 'F':
        Q.popleft()
    elif command[6] == 'L':
        Q.pop()
    else:
        if Q.count(command[7:]) == 0: continue
        Q.remove(command[7:])

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

Solution (Python):



class DoublyLinkedNode:

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

    def print(self):
        print(self.value, end = "")

class DoublyLinkedList:

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

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

    def delete(self, x):
        node = self.head
        while node is not None:
            if node.value == x:
                break
            node = node.next
        else:
            return

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

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

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

    def print(self):
        node = self.head
        while node is not None:
            node.print()
            node = node.next
            if node is not None:
                print(" ", end = "")
        print()

def _main():
    n = int(input())
    dll = DoublyLinkedList(n)

    import sys

    for l in sys.stdin:
        line = l.split()
        if line[0] == "insert":
            dll.insert(int(line[1]))
        elif line[0] == "delete":
            dll.delete(int(line[1]))
        elif line[0] == "deleteFirst":
            dll.delete_first()
        elif line[0] == "deleteLast":
            dll.delete_last()

    dll.print()

if __name__ == "__main__":
    _main()

Solution (Python):
from collections import deque

n = int(input())
my_Linkedlist = deque()

for i in range(n):
  input_str=input().split()
  if input_str[0]=="insert":
      my_Linkedlist.appendleft(input_str[1])
  elif input_str[0]=="delete":
      if my_Linkedlist.count(input_str[1])>0:
          my_Linkedlist.remove(input_str[1]) 
  elif input_str[0]=="deleteFirst":
      my_Linkedlist.popleft()
  elif input_str[0]=="deleteLast":
    my_Linkedlist.pop()
    
print(*my_Linkedlist)

Solution (Python):
from collections import deque


N = int(input())
b = deque()

for i in range(N):
    a = input().split()
    
    if a[0] == 'insert':
        b.appendleft(a[1])
        
    elif a[0] == 'delete':
        try:
            b.remove(a[1])
        except ValueError:
            pass
            
    elif a[0] == 'deleteFirst':
        b.popleft()
        
    else:
        b.pop()
        

print(*b)

Solution (Python):
from collections import deque
import sys


def main():
    d = deque()
    input()
    for c in sys.stdin:
        if c[0] == "i":
            d.appendleft(c[7:-1])
        elif c[6] == " ":
            if c[7:-1] in d:
                d.remove(c[7:-1])
        elif c[6] == "F":
            d.popleft()
        else:
            d.pop()
    print(" ".join(d))


if '__main__' == __name__:
    main()

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

n = int(stdin.readline().rstrip())
lst = deque()
for _ in range(n):
    q = stdin.readline().rstrip().split()
    if q[0] == "insert":
        lst.appendleft(int(q[1]))
    elif q[0] == "delete":
        try:
            lst.remove(int(q[1]))
        except:
            pass
    elif q[0] == "deleteFirst":
        lst.popleft()
    elif q[0] == "deleteLast":
        lst.pop()

print(*lst)

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, n = c.split()
        if c == 'insert':
            a.appendleft(n)
        elif c == 'delete':
            try:
                a.remove(n)
            except:
                pass

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

Solution (Python):

"""
連結リスト
"""
from collections import deque

N = int(input())

que = deque()
for i in range(N):
    instr =  input()
    if instr == 'deleteFirst':
        que.popleft()
    elif instr == 'deleteLast':
        que.pop()
    else:
        com, num = instr.split()
        if com == 'insert':
            que.appendleft(num)
        elif com == 'delete':
            try:
                que.remove(num)
            except ValueError:
                pass
print(*que)

Solution (Python):
from collections import deque


def main():
    queue = deque()
    for _ in range(int(input())):
        command = input().split()
        if len(command) == 2:
            if command[0] == 'insert':
                queue.appendleft(command[1])
            else:   
                try:
                    queue.remove(command[1])
                except ValueError:
                    pass
        elif command[0] == 'deleteFirst':
            queue.popleft()
        else:   
            queue.pop()

    print(' '.join(queue))


if __name__ == '__main__':
    main()

Solution (Python):





from collections import deque

import sys
input = sys.stdin.readline
import cProfile


def main():
    q = deque()
    n =  int(input())
    skipFlag = False

    while n > 0:
        if skipFlag == False:
            l = list(map(str,input().split()))
            if len(l) == 0:
                break
        skipFlag = False
        cmd = l[0]
        if ( cmd == 'insert'):
            d = int(l[1])
            if n <= 1:
                q.appendleft(d)
                n -= 1
                continue
            l = list(map(str,input().split()))
            if len(l) == 0:
                q.appendleft(d)
                break
            if (l[0] == 'deleteFirst' ):
                n -= 1
                continue
            elif (len(q) == 0 and l[0] == 'deleteLast'):
                n -= 1
                continue
            else:
                skipFlag = True
                q.appendleft(d)
        elif ( cmd == 'delete'):
            try:
                q.remove(int(l[1]))
            except:
                None
        elif ( cmd == 'deleteFirst'):
            q.popleft()
        else:
            q.pop()
        n -= 1

    print (*q)

if __name__ == '__main__':
   main()

Solution (Python):
from collections import deque

LIST = deque()

n = int(input())

for _ in range(n):
    input_command = input()

    if input_command == 'deleteFirst':
        LIST.popleft()
    elif input_command == 'deleteLast':
        LIST.pop()
    else:
        com, num = input_command.split()  

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

print(' '.join(LIST))

Solution (Python):



import sys
from collections import deque


def solve():
    
    dll = deque()

    
    lines = sys.stdin.readlines()

    for i in range(1, int(lines[0]) + 1):
        
        ope = lines[i].split()

        
        if ope[0] == 'insert':
            dll.appendleft(ope[1])

        
        elif ope[0] == 'delete':
            if ope[1] in dll:
                dll.remove(ope[1])
        elif ope[0] == 'deleteFirst':
            dll.popleft()
        elif ope[0] == 'deleteLast':
            dll.pop()

    print(*dll, sep=' ')


if __name__ == '__main__':
    solve()

Solution (Python):
from collections import deque

queue = deque()
n=int(input())

for i in range(n):
    command=input()
    if command == 'deleteFirst':
        queue.popleft()
    elif command == 'deleteLast':
        queue.pop()
    else:
        ope, num = command.split()
        if ope == 'delete':
            try:
                queue.remove(num)
            except:
                pass
        else:
            queue.appendleft(num)
result=list(queue)
print(*result)

Solution (Python):
import sys
from collections import deque

def main():
    line = sys.stdin.readlines()
    deq = deque()
    for i in range(1, int(line[0]) + 1):
        input_command = line[i]

        if input_command[0] == 'i':         
            deq.appendleft(input_command[7:-1])
        elif input_command[0] == 'd':
            if input_command[6] == ' ':     
                key = input_command[7:-1]
                if key in deq:
                    deq.remove(key)
            elif input_command[6] == 'F':   
                deq.popleft()
            elif input_command[6] == 'L':   
                deq.pop()

    print(' '.join(deq))
    return


main()

Solution (Python):
from collections import deque
x=deque([])
for i in range(int(input())):
	a=input().split()
	if a[0]=='insert':
		x.insert(0,a[1])
	elif a[0]=='delete':
		if a[1] in x:
			x.remove(a[1])		
	elif a[0]=='deleteFirst':
		x.popleft()
	elif a[0]=='deleteLast':
		x.pop()
print(' '.join(x))

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

Solution (Python):
from collections import deque

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

Solution (Python):
import sys
head = -1
tail = -1
dll = []
for i in range(int(sys.stdin.readline())):
    line = sys.stdin.readline()

    if line[6]=='F':
        if head==tail:
            dll=[]
            head=-1
            tail=-1
        else:
            nxt=dll[head][2] 
            if nxt>=0:  dll[nxt][1]=-1
            if tail==head: tail=-1
            head=nxt
    elif line[6]=='L':
        if head==tail:
            dll=[]
            head=-1
            tail=-1
        else:
            prv=dll[tail][1]
            if prv>=0:  dll[prv][2]=-1
            if tail==head: head=-1
            tail=prv

    elif line[0]=='i': 
        x = int(line[7:])
        dll.append([x, -1, head])
        llen = len(dll)-1
        if head==-1:   tail=llen
        else:            dll[head][1]=llen
        head=llen
    elif line[0]=='d': 
        x = int(line[7:])
        cur=head
        while cur>=0:
            if dll[cur][0]==x:
                nxt=dll[cur][2]
                prv=dll[cur][1]
                dll[nxt][1]=prv
                dll[prv][2]=nxt
                if nxt==-1: tail=prv
                if prv==-1: head=nxt
                break
            cur=dll[cur][2]

print(dll[head][0], end='')
cur = dll[head][2]
while cur>=0:
    print('',dll[cur][0], end='')
    cur=dll[cur][2]
print()

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

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

for i in range(n):
    cmd = sys.stdin.readline().strip().split()
    if cmd[0] == "deleteFirst":
        deq.popleft()
    elif cmd[0] == "deleteLast":
        deq.pop()
    else:
        x = int(cmd[1])
        if cmd[0] == "insert":
            deq.appendleft(x)
        elif cmd[0] == "delete":
            if x not in deq: continue
            deq.remove(x)
print(*deq)

Solution (Python):
from collections import deque
import sys

class doubly_linked_list():
    def __init__(self):
        
        self.linked_list = deque([])
        
    
    def insert(self, x):
        
        self.linked_list.appendleft(x)
        
    
    def delete(self, x):
        
        if x in self.linked_list:
            self.linked_list.remove(x)
            
    
    def delete_first(self):
        
        
            
        self.linked_list.popleft()
            
    
    def delete_last(self):
        
        
            
        self.linked_list.pop()
            

def get_method_and_run(cls, myinput):
    
    
    tmp = myinput().rstrip('\n')
    if tmp.find(" ") == -1:
        method = tmp
    else:
        method, x = tmp.split(" ")
    
    
    if method == "insert":
        cls.insert(x)
    elif method == "delete":
        cls.delete(x)
    elif method == "deleteFirst":
        cls.delete_first()
    elif method == "deleteLast":
        cls.delete_last()
    else:
        pass

if __name__ == "__main__":
    myinput = sys.stdin.readline
    
    n = int(myinput().rstrip('\n'))
    dll = doubly_linked_list()  
    [ get_method_and_run(dll, myinput) for i in range(n)]
    print(" ".join(dll.linked_list))

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

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

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


def doublylink(n):
    l = deque([])
    for _ in range(n):
        f = input().split()

        if f[0] == 'insert':
            l.appendleft(f[1])
        elif f[0] == 'delete':
            if f[1] in l:
                l.remove(f[1])
        elif f[0] == 'deleteFirst':
            l.popleft()
        elif f[0] == 'deleteLast':
            l.pop()

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


if __name__ == '__main__':
    N = int(input())
    doublylink(N)