Problem:
Doubly Linked List Your task is to implement a double linked list. Write a program which performs the following operations:  insert x: insert an element with key x into the front of the list.  delete x: delete the first element which has the key of x from the list. If there is not such element, you need not do anything.  deleteFirst: delete the first element from the list.  deleteLast: delete the last element from the list.  Input The input is given in the following format:  n  command 1   command 2  ...  command n   In the first line, the number of operations n is given. In the following n lines, the above mentioned operations are given in the following format:  insert x  delete x  deleteFirst  deleteLast  Output Print all the element (key) in the list after the given operations. Two consequtive keys should be separated by a single space. Constraints  The number of operations ≤ 2,000,000  The number of delete operations ≤ 20  0 ≤ value of a key ≤ 10 9  The number of elements in the list does not exceed 10 6  For a delete, deleteFirst or deleteLast operation, there is at least one element in the list.  Sample Input 1 7
insert 5
insert 2
insert 3
insert 1
delete 3
insert 6
delete 5 Sample Output 1 6 1 2 Sample Input 2 9
insert 5
insert 2
insert 3
insert 1
delete 3
insert 6
delete 5
deleteFirst
deleteLast Sample Output 2 1

Solution (Python):

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

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

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

print(*list)

Solution (Python):

import sys
     
A=[]
fp =0
bp =0
 
n=int(raw_input())
while n:
    s = raw_input()
    if s[0] == 'i':
        A.append(int(s[7:]))
        fp += 1
    elif s[6] == ' ':
        try:
            i = A[::-1].index(int(s[7:]))
            if i!=-1:
                del A[-i-1]
                fp -=1
        except:
            pass
    elif s[6] == 'F':
        A.pop()
        fp -=1
    elif s[6] == 'L':
        bp +=1
    n -=1
 
for e in A[bp:fp+1][::-1]:
    print int(e),

Solution (Python):

import sys
     
A=[]
fp =0
bp =0
 
n=int(raw_input())
while n:
    s = raw_input()
    if s[0] == 'i':
        A.append(int(s[7:]))
        fp += 1
    elif s[6] == ' ':
        try:
            i = A[::-1].index(int(s[7:]))
            if i!=-1:
                del A[-i-1]
                fp -=1
        except:
            pass
    elif s[6] == 'F':
        A.pop()
        fp -=1
    elif s[6] == 'L':
        bp +=1
    n -=1
 
for e in A[bp:fp+1][::-1]:
    print int(e),

Solution (Python):
from collections import deque

n = int(input())

linked_list = deque([])

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

    if "insert" in command:
        x = command.split()[-1]
        linked_list.appendleft(x)
    elif command == "deleteFirst":
        linked_list.popleft()
    elif command == "deleteLast":
        linked_list.pop()
    elif "delete" in command:
        x = command.split()[-1]
        if x in linked_list:
            linked_list.remove(x)

print(*linked_list)

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

Solution (Python):
import sys


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


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

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

    def search(self, x):
        node = self.head.next
        while node is not self.head and node.key != x:
            node = node.next
        return node

    def delete_key(self, x):
        node = self.search(x)
        self._delete(node)

    def _delete(self, node):
        if node is self.head:
            return None
        node.prev.next = node.next
        node.next.prev = node.prev

    def deleteFirst(self):
        self._delete(self.head.next)

    def deleteLast(self):
        self._delete(self.head.prev)

    def getKeys(self):
        node = self.head.next
        keys = []
        while node is not self.head:
            keys.append(node.key)
            node = node.next
        return " ".join(keys)


L = DoublyLinkedList()
n = int(input())
for i in sys.stdin:
    if 'insert' in i:
        x = i[7:-1]
        L.insert(x)
    elif 'deleteFirst' in i:
        L.deleteFirst()
    elif 'deleteLast' in i:
        L.deleteLast()
    elif 'delete' in i:
        x = i[7:-1]
        L.delete_key(x)
    else:
        pass
print(L.getKeys())

Solution (Python):
import sys


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


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

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

    def search(self, x):
        node = self.head.next
        while node is not self.head and node.key != x:
            node = node.next
        return node

    def delete_key(self, x):
        node = self.search(x)
        if node is self.head:
            return None
        self._delete(node)

    def _delete(self, node):
        node.prev.next = node.next
        node.next.prev = node.prev

    def deleteFirst(self):
        self._delete(self.head.next)

    def deleteLast(self):
        self._delete(self.head.prev)

    def getKeys(self):
        node = self.head.next
        keys = []
        while node is not self.head:
            keys.append(node.key)
            node = node.next
        return " ".join(keys)


L = DoublyLinkedList()
n = int(input())
for i in sys.stdin:
    if 'insert' in i:
        x = i[7:-1]
        L.insert(x)
    elif 'deleteFirst' in i:
        L.deleteFirst()
    elif 'deleteLast' in i:
        L.deleteLast()
    elif 'delete' in i:
        x = i[7:-1]
        L.delete_key(x)
    else:
        pass
print(L.getKeys())

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

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

for line in sys.stdin:
    op = line.strip().split()
    cmd = op[0]

    if cmd[0] == 'i':
        x = op[1]
        ary.appendleft(x)

    elif len(cmd) == 6:
        x = op[1]
        if x in ary:
            ary.remove(x)

    elif len(cmd) == 10:
        ary.pop()

    else:
        ary.popleft()

print(*ary)

Solution (Python):
import sys
from collections import deque

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

for line in sys.stdin:
    op = line.strip().split()
    cmd = op[0]

    if cmd == 'insert':
        x = op[1]
        ary.appendleft(x)

    elif cmd == 'delete':
        x = op[1]
        if x in ary:
            ary.remove(x)

    elif cmd == 'deleteLast':
        ary.pop()

    else:
        ary.popleft()

print(*ary)

Solution (Python):
from collections import deque
a = deque([])
for i in range(int(input())):
    c = input().split()
    if c[0] == "insert":
        a.appendleft(c[1])
    elif c[0] == "delete":
        try:
            a.remove(c[1])
        except ValueError:
            pass
    elif c[0] == "deleteFirst":
        a.popleft()
    else:
        a.pop()
for i,e in enumerate(a):
    if i == len(a) - 1:
        print(e)
    else:
        print(e, end=" ")

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

class LinkedList(object):

    def __init__(self):
        self.dummy = Node(None)
        self.dummy.next = self.dummy
        self.dummy.prev = self.dummy

    def insert(self, x):
        node = Node(x, self.dummy.next, self.dummy)

        self.dummy.next.prev = node
        self.dummy.next = node
    
    def delete(self, x):
        node = self.dummy.next
        while node != self.dummy:
            if node.value == x:
                self.__remove_node(node)
                break
            else:
                node = node.next
    
    def deleteFirst(self):
        self.__remove_node(self.dummy.next)

    def deleteLast(self):
        self.__remove_node(self.dummy.prev)

    def __remove_node(self, node):
        if node != self.dummy:
            node.prev.next = node.next
            node.next.prev = node.prev

    def items(self):
        ret = []
        node = self.dummy.next
        while node != self.dummy:
            ret.append(node.value)
            node = node.next
        return ret

def main():
    from sys import stdin
    store = LinkedList()
    for i in range(int(input())):
        parts = stdin.readline().strip().split()
        cmd = parts[0]
        if cmd == 'insert':
            store.insert(parts[1])
        elif cmd == 'delete':
            store.delete(parts[1])
        elif cmd == 'deleteFirst':
            store.deleteFirst()
        elif cmd =='deleteLast':
            store.deleteLast()

    print(' '.join(store.items()))

if __name__ == '__main__':
    main()

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, 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
N = int(input())
de = deque()
for n in range(N):
    com = input().split()
    if com[0] == "insert":
        de.appendleft(com[1])
    elif com[0] == "delete":
        try:
            de.remove(com[1])
        except:
            pass
    elif com[0] == "deleteFirst":
        de.popleft()
    else:
        de.pop()
print(*de)

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

def run():
    n = int(input())
    dq = deque()
    for _ in range(n):
        order = input()
        if order[0] == 'i':
            dq.appendleft(order[7:])
        elif order[6] == ' ':
            key = order.split()[1]
            if not key in dq:
                continue
            for i in range(len(dq)):
                if dq[i] == key:
                    dq.remove(key)
                    break
        elif order[6] == 'F':
            dq.popleft()
        elif order[6] == 'L':
            dq.pop()

    print(' '.join(dq))

if __name__ == '__main__':
    run()

Solution (Python):
from sys import stdin

class DList:
    class Cell:
        def __init__(self, k):
            self.key  = k
            self.prev = None
            self.next = None

    def __init__(self):
        self.head = DList.Cell(None)
        self.last = DList.Cell(None)
        self.head.next = self.last
        self.last.prev = self.head

    def insert(self, x):
        c = DList.Cell(x)
        c.prev = self.head
        c.next = self.head.next
        c.next.prev = c
        self.head.next = c

    def delete(self, x):
        c = self.__find(x)
        if c != None:
            self.__delete(c)

    def __delete(self, c):
        c.prev.next = c.next
        c.next.prev = c.prev

    def __find(self, x):
        c = self.head.next
        while c != None and c.key != x:
            c = c.next
        return c

    def deleteFirst(self):
        self.__delete(self.head.next)
    def deleteLast(self):
        self.__delete(self.last.prev)

    def __iter__(self):
        self.it = self.head.next
        return self
    def __next__(self):
        if self.it == self.last:
            raise StopIteration
        k = self.it.key
        self.it = self.it.next
        return k

dlist = DList()

n = int(stdin.readline())
for i in range(n):
    cmd = stdin.readline()
    if   cmd.startswith('insert'):
        dlist.insert(cmd[7:-1])
    elif cmd.startswith('deleteFirst'):
        dlist.deleteFirst()
    elif cmd.startswith('deleteLast'):
        dlist.deleteLast()
    elif cmd.startswith('delete'):
        dlist.delete(cmd[7:-1])

print(' '.join(dlist))

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])  
    if cmd[0]=='delete':
        try:l.remove(cmd[1])
        except :pass
    if cmd[0]=='deleteFirst':
        l.popleft()
    if cmd[0]=='deleteLast':
        l.pop()
print(*l)

Solution (Python):
n = int(raw_input())
x = [0 for i in range(n)]
pf = -1
pb = 0

li = []
for i in range(n):
    s = (raw_input().split())
    if s[0] == 'insert':
        pf += 1
        x[pf]=int(s[1])
    elif s[0] == 'delete':
        try:
            p = x[pf::-1].index(int(s[1]))
            x[pf-p] = 0
            while x[pf]==0:
                pf -= 1
            while x[pb]==0:
                pb += 1
        except:
            pass
    elif s[0] == 'deleteFirst':
        x[pf]=0
        pf -= 1
    elif s[0] == 'deleteLast':
        x[pb] = 0
        pb += 1
for e in x[pb:pf+1][::-1]:
    if e != 0:
        print e,

Solution (Python):
import sys
    
A=[]
fp =0
bp =0

n=int(raw_input())
while n:
    s = raw_input()
    if s[0] == 'i':
        A.append(int(s[7:]))
        fp += 1
    elif s[6] == ' ':
        try:
            i = A[::-1].index(int(s[7:]))
            if i!=-1:
                del A[-i-1]
                fp -=1
        except:
            pass
    elif s[6] == 'F':
        A.pop()
        fp -=1
    elif s[6] == 'L':
        bp +=1
    n -=1

for e in A[bp:fp+1][::-1]:
    print int(e),

Solution (Python):
import sys,gc
    
A=[]
fp =0
bp =0

n=int(raw_input())
while n:
    s = raw_input()
    if s[0] == 'i':
        A.append(int(s[7:]))
        fp += 1
    elif s[6] == ' ':
        try:
            i = A[::-1].index(int(s[7:]))
            if i!=-1:
                del A[-i-1]
                fp -=1
        except:
            pass
    elif s[6] == 'F':
        A.pop()
        fp -=1
    elif s[6] == 'L':
        bp +=1
    n -=1

for e in A[bp:fp+1][::-1]:
    print int(e),

Solution (Python):

from __future__ import division, print_function
from sys import stdin
from collections import deque

n = int(stdin.readline())
dll = deque()
fcode = ord('F')
lcode = ord('L')
while n:
    n -= 1
    cmd = stdin.readline().rstrip()
    if cmd.startswith('i'):
        dll.appendleft(int(cmd[7:]))
    elif cmd.startswith('d'):
        c = ord(cmd[6])
        if c == fcode:
            dll.popleft()
        elif c == lcode:
            dll.pop()
        else:
            try:
                dll.remove(int(cmd[7:]))
            except ValueError:
                pass
print(*dll)

Solution (Python):
n = int(raw_input())
ls = []
bp = 0
for i in range(n):
	cmd = raw_input()
	if cmd[0] == "i":
		ls.append(cmd[7:])
	elif cmd[6] == " ":
		try:
			x = ls[::-1].index(cmd[7:])
			if x!=-1:
				del ls[-x-1]
		except:
			pass
	elif cmd[6] == "F":
		ls.pop()
	else:
		bp += 1

print " ".join(map(str, ls[bp:][::-1]))

Solution (Python):
n = int(raw_input())
q = []
bottom = 0
for i in range(n):
    cmd = raw_input()
    if cmd[0] == 'i':
        q.append(cmd[7:])
    elif cmd[6] == ' ':
        try:
            q.pop(~q[::-1].index(cmd[7:]))
        except:
            pass
    elif cmd[6] == 'F':
        q.pop()
    else:
        bottom += 1

print(' '.join(q[bottom:][::-1]))

Solution (Python):
from sys import stdin

n = int(stdin.readline())
q = []
bottom = 0
for i in range(n):
    cmd = stdin.readline()[:-1]
    if cmd[0] == 'i':
        q.append(cmd[7:])
    elif cmd[6] == ' ':
        try:
            q.pop(~q[::-1].index(cmd[7:]))
        except:
            pass
    elif cmd[6] == 'F':
        q.pop()
    else:
        bottom += 1

print(' '.join(q[bottom:][::-1]))

Solution (Python):
import sys
from collections import deque

n = int(sys.stdin.readline())
q = deque()
for i in range(n):
    cmd = sys.stdin.readline()[:-1]
    if cmd[0] == 'i':
        q.appendleft(cmd[7:])
    elif cmd[6] == ' ':
        try:
            q.remove(cmd[7:])
        except:
            pass
    elif cmd[6] == 'F':
        q.popleft()
    else:
        q.pop()

print(' '.join(q))

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

class DoublyLinkedList(object):
  def __init__(self):
    self.start = self.last = None

  def insert(self, num):
    new_element = Node(num)

    if self.start is None: 
      self.start = self.last = new_element
    else:
      new_element.next = self.start
      self.start.prv = new_element
      self.start = new_element

  def delete(self, target):
    it = self.start
    while it is not None: 
      if it.num == target:
        if it.prv is None and it.next is None: 
          self.start = self.last = None
        else:
          if it.prv is not None: 
            it.prv.next = it.next
          else: 
            self.start = self.start.next
          if it.next is not None:  
            it.next.prv = it.prv
          else: 
            self.last = self.last.prv
        break
      it = it.next

  def deleteFirst(self):
    if self.start is self.last:
      self.start = self.last = None
    else:
      self.start.next.prv = None
      self.start = self.start.next

  def deleteLast(self):
    if self.start is self.last:
      self.start = self.last = None
    else:
      self.last.prv.next = None
      self.last = self.last.prv

  def contentToArray(self):
    n_list = []
    it  = self.start
    while it is not None:
      n_list.append(it.num)
      it = it.next
    return ' '.join(n_list)

def main():
  from sys import stdin
  N = int(input())

  linkedList = DoublyLinkedList() 

  for i in range(N):
    command = stdin.readline().rstrip().split()
    if command[0] == 'insert':
      linkedList.insert(command[1])
    elif command[0] == 'delete':
      linkedList.delete(command[1])
    elif command[0] == 'deleteFirst':
      linkedList.deleteFirst()
    elif command[0] == 'deleteLast':
      linkedList.deleteLast()

  print(linkedList.contentToArray())

if __name__ == "__main__":
  main()

Solution (Python):
from collections import deque

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

for loop 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):
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とkeyに分けるとTimeLimitExceededになるので、comだけで処理しました
        (別のいい方法があれば教えて欲しいです)
        com, *key = input().split()
        この場合keyがリストになるので、com[7:]をkey[0]で置き換える必要があります。
        (xがない命令ではkey=[]となるだけ)
        '''
        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 sys import stdin

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

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

def Insert(key):
    node = Node(key)
    node.next = sent.next
    sent.next.prev = node
    sent.next = node
    node.prev = sent

def Search(key):
    p = sent.next
    while p.value != None:
        if p.value == key:
            return p
        else:
            p = p.next
    return False

def Delete(key):
    p = Search(key)
    if p != False:
        p.prev.next = p.next
        p.next.prev = p.prev

def DeleteF():
    p = sent.next
    p.prev.next = p.next
    p.next.prev = p.prev

def DeleteL():
    p = sent.prev
    p.prev.next = p.next
    p.next.prev = p.prev

sent = Sentinel()
sent.next = sent
sent.prev = sent

n = int(input())
for i in range(n):
    L = stdin.readline().split()
    if L[0] == 'insert':
        Insert(int(L[1]))
    elif L[0] == 'delete':
        Delete(int(L[1]))
    elif  L[0] == 'deleteFirst':
        DeleteF()
    elif  L[0] == 'deleteLast':
        DeleteL()

p = sent.next
tl = []
while True:
    tl.append(p.value)
    p = p.next
    if p.value == None:
        break
print(' '.join(str(x) for x in tl))

Solution (Python):
from collections import deque

DLL = deque()

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

Solution (Python):
from collections import deque
import sys

stack = deque()

def linked_list(stack,command,x=None):

    if command == "insert":
        stack.appendleft(x)

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

    elif command == "deleteFirst":
        stack.popleft()

    elif command == "deleteLast":
        stack.pop() 


N = int(input())


for i in range(N):

    
    input_command = input()


    if input_command == "deleteFirst":
        stack.popleft()

    elif input_command == "deleteLast":
        stack.pop() 

    else:
        command,x = input_command.split()
        if command == "insert":
            stack.appendleft(x)

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

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

Solution (Python):
from collections import deque


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


if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque

N = int(input())
ans = deque()

while N > 0:
    N -= 1

    tmp = input().split()

    if tmp[0] == "delete":
        
        
        
        
        

        try:
            ans.remove(tmp[1])
        except:
            pass

    elif tmp[0] == "deleteFirst":
        ans.popleft()

    elif tmp[0] == "deleteLast":
        ans.pop()

    else:
        ans.appendleft(tmp[1])

print(*ans)

Solution (Python):
from collections import deque


def print_list(que):
    for i, q in enumerate(que):
        if i != len(que)-1:
            print(q, end=' ')
        else:
            print(q)


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


if __name__ == "__main__":
    main()