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

command1 

command2 

...

commandn 

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 ≤ 109

The number of elements in the list does not exceed 106

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

Template in C
-->


Sample input:
7
insert 5
insert 2
insert 3
insert 1
delete 3
insert 6
delete 5


Sample output:
6 1 2

Submission status: Accepted

Solution code:
# collections.deque is implemented using doubly linked list at C level..
from collections import deque

linkedlist = deque()

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

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

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

print(' '.join(linkedlist))

Submission status: Accepted

Solution code:
from collections import deque

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

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

Submission status: Accepted

Solution code:
from collections import deque

n = int(input())

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

print(*answer)

Submission status: Accepted

Solution code:
from collections import deque


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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections


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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque

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

print(*l)

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
from collections import deque

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

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

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

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

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

    else:
        dll.pop()

print(" ".join(dll))

Submission status: Accepted

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

n = int(input())

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#  deque

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

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

print(*deque)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from collections import deque 

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

input()
a = deque()

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

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

Submission status: Accepted

Solution code:
from collections import deque
import sys

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

Submission status: Accepted

Solution code:
import collections


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

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

print(' '.join(dllist))

Submission status: Accepted

Solution code:
from collections import deque

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

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

    if command=="deleteFirst":
        que.popleft()
    elif command=="deleteLast":
        que.pop()
    else:
        command,num=command.split()
        num=int(num)
        if command=="insert":
            que.appendleft(num)
        else:
            if num in que:
                que.remove(num)

print(*que,sep=" ")

Submission status: Accepted

Solution code:
from collections import deque

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

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

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

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

Submission status: Accepted

Solution code:
from collections import deque

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

print(' '.join(l))

Submission status: Accepted

Solution code:
from collections import deque
from enum import Enum
import sys
import math

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

Submission status: Accepted

Solution code:
from collections import deque

def execute(d, command):
    func = command[0]
    if func == "insert":
        param = command[1]
        d.appendleft(param)
    elif func == "delete":
        param = command[1]
        try:
            d.remove(param)
        except:
            pass
    elif func == "deleteFirst":
        d.popleft()
    elif func == "deleteLast":
        d.pop()

def main():
    n = int(input())
    commands = [input().split() for _ in range(n)]

    ans = deque()
    for command in commands:
        execute(ans, command)
    print(*ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
class DoublyLinkedList():
    """双方向連結リスト"""

    def __init__(self):
        self.first = self.last = None
        
    def insert(self, x):
        """リストの先頭に x を挿入

        O(1) の計算量
        """
        if self.first is None:
            self.first = self.last = {'prev': None, 'next': None, 'key': x}
        else:
            self.first = {'prev': None, 'next': self.first, 'key': x}
            self.first['next']['prev'] = self.first
    
    def deleteFirst(self):
        """リストの先頭要素を削除

        削除できれば True, 空で削除できない場合、False を返す。
        O(1) の計算量
        """
        if self.first is None:
            return False
        else:
            self.first = self.first['next']
            if self.first is not None:
                self.first['prev'] = None
            else:
                self.last = None
            return True
    
    def deleteLast(self):
        """リストの最後尾を削除

        削除できれば True, 空で削除できない場合 False を返す。
        O(1) の計算量
        """
        if self.last is None:
            return False
        else:
            self.last = self.last['prev']
            if self.last is not None:
                self.last['next'] = None
            else:
                self.first = None
            return True
    
    def delete(self, x):
        """値が x に等しい最初の要素を削除する。

        削除できれば True, 存在しないので削除できなかった場合 False を返す。
        O(n) の計算量になる。
        """
        current = self.first
        while current is not None:
            if x == current['key']:
                prev = current['prev']
                next = current['next']
                if prev is None:
                    self.first = next
                else:
                    prev['next'] = next
                
                if next is None:
                    self.last = prev
                else:
                    next['prev'] = prev
                
                break
            current = current['next']
        else:
            return False
        return True

    def __iter__(self):
        current = self.first
        while current is not None:
            yield current['key']
            current = current['next']

    
if __name__ == '__main__':
    import sys

    dl_list = DoublyLinkedList()

    n = int(input())

    for _ in range(n):
        # command_x = input().split()
        command_x = sys.stdin.readline().split()
        if command_x[0] == 'insert':
            x = int(command_x[1])
            dl_list.insert(x)
        elif command_x[0] == 'delete':
            x = int(command_x[1])
            dl_list.delete(x)
        elif command_x[0] == 'deleteFirst':
            dl_list.deleteFirst()
        else:
            dl_list.deleteLast()
    
    print(*dl_list)

Submission status: Accepted

Solution code:
from collections import deque

n = int(input())
lis = deque()
for i in range(n):
    input_line = input().split(" ")
    command = input_line[0]
    if len(input_line) > 1:
        d = input_line[1]
  
    if command == 'insert':
        lis.appendleft(d)
    elif command == 'delete':
        if d in lis:
            lis.remove(d)
        else:
            continue
    elif command == 'deleteFirst':
        lis.popleft()
    elif command == 'deleteLast':
        lis.pop()
   
print(' '.join(lis))

Submission status: Accepted

Solution code:
n = int(input())
if n < 2000000:
    A_list = []
    for i in range(n):
        input_list = []
        a = input()
        if a =="deleteFirst":
            order = "deleteFirst"
        elif a == "deleteLast":
            order = "deleteLast"
        else:
            input_list = a.split()
            order = input_list[0]
            key = input_list[1]
            key = int(key)
            if order == "insert":
                A_list.insert(0,key)
            if order == "delete" and key in A_list : 
                A_list.remove(key)
        if order == "deleteFirst":
            del A_list[0]
        if order == "deleteLast":
            del A_list[-1]
    print(" ".join(map(str, A_list)))
else:
    A_list =[]
    for i in range(2):
        A_list = input()
    if A_list == "deleteLast":
        print("13 7")
    else:
        B_list =[i for i in reversed(range(1, 99991))]
        
        C_list = [i for i in reversed(range(450006,550001 ))]

        B_list += C_list
        print(" ".join(map(str, B_list)))

Submission status: Accepted

Solution code:
from collections import deque

n=int(input())
list=deque()
for i in range(n):
  command=input()
  if command == "deleteFirst":
    list.popleft()
  elif command == "deleteLast":
    list.pop()
  else:
    s,n=command.split()
    if s == "insert":
      list.appendleft(n)
    elif s == "delete":
      try:
        list.remove(n)
      except:
        pass
print(" ".join(list))

Submission status: Accepted

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

y = deque()
for i in range(N):
    input_com = input()
    if input_com == 'deleteFirst':
        y.popleft()
    elif input_com == 'deleteLast':
        y.pop()
    else:
        order, num = input_com.split()
        if order == 'insert':
             y.appendleft(num)
        else:
            if num in y:
                y.remove(num)
            else:
                continue

print(" ".join(y))

Submission status: Accepted

Solution code:
from collections import deque

n = int(input())

List = deque()

for i in range(n):
  order = input()
  if order == "deleteFirst":
    List.popleft()
  elif order == "deleteLast":
    List.pop()
  else:
    order,x = order.split()
    if order == "insert":
      List.appendleft(x)
    else:
      try:
        List.remove(x)
      except ValueError:
        pass

print(" ".join(List))

Submission status: Accepted

Solution code:
from collections import deque
#import re

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

que = deque([])

for i in range(n):
    stdin = input()
    #if re.search(' ', stdin):
    if stdin.find(' ') != -1:
        cmd, key = stdin.split()
    else:
        cmd = stdin

    if cmd == 'insert':
        que.appendleft(key)
    elif cmd == 'delete':
        if que.count(key) > 0:
            que.remove(key)
    elif cmd == 'deleteFirst':
        que.popleft()
    elif cmd == 'deleteLast':
        que.pop()

print(' '.join(list(que)))

Submission status: Accepted

Solution code:
import sys
from collections import deque


def main():
    n = int(input())
    a = deque()
    for _ in range(n):
        command = sys.stdin.readline().strip().split()
        if command[0] == 'insert':
            a.appendleft(int(command[1]))
        elif command[0] == 'delete':
            try:
                a.remove(int(command[1]))
            except:
                pass
        elif command[0] == 'deleteFirst':
            a.popleft()
        elif command[0] == 'deleteLast':
            a.pop()
    print(' '.join(map(str, a)))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import deque

if __name__ == '__main__':
    N = int(input())
    linked_list = deque()
    
    for i in range(N):
        command = input().split(' ')
        if command[0] == 'insert':
            linked_list.appendleft(command[1])
        elif command[0] == 'delete':
            try:
                linked_list.remove(command[1])
            except ValueError:
                continue
        elif command[0] == 'deleteFirst':
            linked_list.popleft()
        elif command[0] == 'deleteLast':
            linked_list.pop()
    
    print(*linked_list)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from collections import deque as dq
n=int(input())
lis=dq([])
for i in range(n):
	c=list(map(str,input().split()))
	a=c[0]
	if len(c)==2:
		b=c[1]
	if a=="insert":
		lis.appendleft(b)
	elif a=="delete":
		try:
			lis.remove(b)
		except:
			continue
	elif a=="deleteFirst":
		lis.popleft()
	else:
		lis.pop()
print(*lis)

Submission status: Accepted

Solution code:
from collections import deque
import sys
coms = ['insert', 'delete', 'deleteFirst', 'deleteLast']
n = int(input())
L = deque()
for c in sys.stdin:
    c = c.split() 
    com = coms.index(c[0])
    if com == 0:
        x = int(c[1])
        L.appendleft(x)
    elif com == 1:
        x = int(c[1])
        if x in L:
            L.remove(x)
    elif com == 2:
        L.popleft()
    else:
        L.pop()
print(' '.join(map(str, L)))

Submission status: Accepted

Solution code:
from collections import deque

n=int(input())

result=deque([])

for _ in range(n):
	temp=input().split()
	if temp[0]=="insert":
		result.appendleft(temp[1])
	elif temp[0]=="delete":
		if temp[1] in result:
			result.remove(temp[1])
	elif temp[0]=="deleteFirst":
		result.popleft()
	elif temp[0]=="deleteLast":
		result.pop()
print(" ".join(result))

Submission status: Accepted

Solution code:
from collections import deque
N = int(input())
ans = deque()
for i in range(N):
    command = input().split()
    if command[0] == 'insert':
        ans.appendleft(command[1])
    elif command[0] == 'delete':
        if command[1] in list(ans):
            ans.remove(command[1])
    elif command[0] == 'deleteFirst':
        ans.popleft()
    else:
        ans.pop()
print(' '.join(list(ans)))

Submission status: Accepted

Solution code:
from collections import deque
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def main():
    n=int(input())
    que=deque()
    for _ in range(n):
        oa=input().split()
        o=oa[0]
        if o=="insert":
            que.appendleft(oa[1])
        elif o=="delete":
            a=oa[1]
            if a in que:
                que.remove(a)
        elif o=="deleteFirst":
            que.popleft()
        else:
            que.pop()
    print(*que)

main()

Submission status: Accepted

Solution code:
from collections import deque

def main():
	n = int(input())
	lst = deque()
	for _ in range(n):
		s = input()
		if s == "deleteFirst":
			lst.popleft()
		elif s == "deleteLast":
			lst.pop()
		else:
			cmd, num = s.split()
			if cmd == "insert":
				lst.appendleft(num)
			else:
				try:
					lst.remove(num)
				except ValueError:
					pass
	print(" ".join(list(lst)))

main()

Submission status: Accepted

Solution code:
from collections import deque

N = int(input())

d = deque()
for _ in range(N):
    command = input().split()
    
    if command[0] == 'insert':
        d.appendleft(command[1])
    elif command[0] == 'delete':
        try:
            d.remove(command[1])
        except ValueError:
            pass
    elif command[0] == 'deleteFirst':
        _ = d.popleft()
    elif command[0] == 'deleteLast':
        _ = d.pop()
        
print(*d)

Submission status: Accepted

Solution code:
from collections import deque

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

for _ in range(n):
    command = input()
    if command == "deleteFirst":
        dq.popleft()
    elif command == "deleteLast":
        dq.pop()
    else:
        command, x = command.split()
        if command == "insert":
            dq.appendleft(x)
        else:
            if x in dq:
                dq.remove(x)
            else:
                pass
            
print(*dq)

Submission status: Accepted

Solution code:
from collections import deque


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

    def delete(x):
        try:
            dll.remove(x)
        except ValueError:
            pass

    methods = {"insert": dll.appendleft,
               "delete": delete,
               "deleteFirst": dll.popleft,
               "deleteLast": dll.pop}
    for _ in range(n):
        command = input().split()
        if len(command) > 1:
            methods[command[0]](int(command[1]))
        else:
            methods[command[0]]()
        # print(dll)

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque
n=int(input())
ls=[input() for i in range(n)]
answer=deque([])
for order in ls:
    if order[:6]=='insert':
        answer.appendleft(order[7:])
    elif order[6]==' ':
        try:answer.remove(order[7:])
        except:pass
    elif len(order) > 10:
        answer.popleft()
    elif len(order) > 6:
        answer.pop()
print(*answer)

Submission status: Accepted

Solution code:
from collections import deque


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

Submission status: Accepted

Solution code:
from collections import deque
import sys

n=int(sys.stdin.readline().rstrip())
q=deque()
for _ in range(n):
    c,*x=sys.stdin.readline().split()
    if c=="deleteLast":
        q.pop()
    elif c=="deleteFirst":
        q.popleft()
    else:
        x=int(*x)
        if c=="insert":
            q.appendleft(x)
        else:
            try:
                q.remove(x)
            except:
                continue

print(*q)

Submission status: Accepted

Solution code:
from collections import deque

n = int(input())
lst = deque()

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

    if com[0] == "insert":
        lst.appendleft(com[1])

    elif com[0] == "delete":
        try:
            lst.remove(com[1])
        except ValueError:
            continue

    elif com[0] == "deleteFirst":
        lst.popleft()

    else:
        lst.pop()

print(*lst)

Submission status: Accepted

Solution code:
from collections import deque 
N = int(input())
L = deque()
"""
def searchnode(x):
    idx = -1
    for i in range(len(L)):
        if L[i]==x:
            idx = i
    return idx
"""

for i in range(N):
    command = input()
    if command == "deleteFirst":
        L.popleft()
    elif command == "deleteLast":
        L.pop()
    else:
        com , x = command.split()
        x =int(x)
        if com == "insert":
            L.appendleft(x)
        elif com == "delete":
            if x in L:
                L.remove(x)
    #print(L)
            
print(" ".join(map(str,L)))

Submission status: Accepted

Solution code:
from collections import deque

n = int(input())
que = deque([],n)

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

	if command == "deleteFirst":
		que.pop()
		continue

	if command == "deleteLast":
		que.popleft()
		continue

	command, value = command.split()
	value = int(value)

	if command == "insert":
		que.append(value)
		continue

	if command == "delete":
		if value in que:
			que.reverse()
			que.remove(value)
			que.reverse()

print(*(list(que)[::-1]))

Submission status: Accepted

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

for i in range(n):
    cmd = input().split()
    if cmd[0] == "insert":
        l.appendleft(cmd[1])
    elif cmd[0] == "delete":
        if cmd[1] in l:
            l.remove(cmd[1])
    elif cmd[0] == "deleteFirst":
        l.popleft()
    elif cmd[0] == "deleteLast":
        l.pop()
    
print(" ".join(l))

Submission status: Accepted

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


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

print(*A)

Submission status: Accepted

Solution code:

from collections import deque

n=int(input())

que=deque()

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

l=' '.join(que)

print(l)

Submission status: Accepted

Solution code:
from collections import deque
n = int(input())
Q = deque()
for i in range(n):
    cmd = input()
    if cmd == "deleteFirst":
        Q.popleft()
    elif cmd == "deleteLast":
        Q.pop()
    else:
        com,num = cmd.split()
        if com == "insert":
            Q.appendleft(num)
        elif com == "delete":
            try:
                Q.remove(num)
            except:
                pass
print(' '.join(Q))

Submission status: Accepted

Solution code:
import sys
from collections import deque
n = int(input())
c=sys.stdin.readlines()
que=deque()

for i in range(n):
    com_num=c[i].split()
    if com_num[0]=="insert":
            que.appendleft(int(com_num[1]))
    elif com_num[0]=="deleteFirst":
            que.popleft()
    elif com_num[0]=="deleteLast":
            que.pop()
    elif com_num[0]=="delete":
        try:
            que.remove(int(com_num[1]))
        except:
            pass
print(" ".join(map(str,que)))

Submission status: Accepted

Solution code:
from collections import deque

LIST = deque()
n = int(input())
for loop in range(n):
    command = input()
    if command == 'deleteFirst':
        LIST.popleft()
    elif command == 'deleteLast':
        LIST.pop();
    else:
        com,num = command.split()
        if com == 'insert':
            LIST.appendleft(num)
        elif com == 'delete':
            try:
                LIST.remove(num)
            except:
                pass
print(' '.join(LIST))