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:
class Cell:
    def __init__(self, x):
        self.x = x
        self.next = None
        self.prev = None

class DoublyLinkedList:
    def __init__(self):
        self.start = Cell(None)
        self.start.next = None
        self.start.prev = None

    def InsCell(self, x):
        new = Cell(x)
        if self.start.next != None:
            new.next = self.start.next
            self.start.next.prev = new
        else:
            self.start.prev = new
            new.next = self.start
        self.start.next = new
        new.prev = self.start

    def DelCell(self, x):
        tmp = self.start
        while tmp.x != x:
            if tmp.next == self.start:
                return
            tmp = tmp.next
        tmp.prev.next = tmp.next
        tmp.next.prev = tmp.prev
        tmp = None

    def DelFisrtCell(self):
        tmp = self.start.next
        if tmp.next == self.start:
            self.start.next = None
            self.start.prev = None
        else:
            self.start.next = tmp.next
            tmp.next.prev = self.start
        tmp = None

    def DelLastCell(self):
        tmp = self.start.prev
        if tmp.prev == self.start:
            self.start.next = None
            self.start.prev = None
        else:
            self.start.prev = tmp.prev
            tmp.prev.next = self.start
        tmp = None

    def PrintCell(self):
        ans = []
        tmp = self.start.next
        if tmp == None:
            print("")
        else:
            while tmp != self.start:
                ans.append(tmp.x)
                tmp = tmp.next
            print(" ".join(ans))

import sys
input = sys.stdin.readline

n = int(input())
l = DoublyLinkedList()

for _ in range(n):
    c = input().split()
    if c[0] == "insert":
        l.InsCell(c[1])
    elif c[0] == "delete":
        l.DelCell(c[1])
    elif c[0] == "deleteFirst":
        l.DelFisrtCell()
    elif c[0] == "deleteLast":
        l.DelLastCell()

l.PrintCell()

Submission status: Accepted

Solution code:
def resolve():
    from collections import deque
    n = int(input())
    que = deque()
    for _ in range(n):
        cv = input().split()
        if cv[0] == "insert":
            x = int(cv[1])
            que.appendleft(x)
        elif cv[0] == "delete":
            x = int(cv[1])
            if x in que:
                que.remove(x)
        elif cv[0] == "deleteFirst":
            que.popleft()
        elif cv[0] == "deleteLast":
            que.pop()
    ans = list(que)
    print(*ans)


resolve()

Submission status: Accepted

Solution code:
from collections import deque
import sys
input = sys.stdin.readline


class DoublyLinkedList:
    def __init__(self):
        self.doubly_linked_list = deque()

    def insert(self, item):
        self.doubly_linked_list.appendleft(item)

    def delete(self, item):
        if item in self.doubly_linked_list:
            self.doubly_linked_list.remove(item)

    def delete_first(self):
        if self.doubly_linked_list:
            self.doubly_linked_list.popleft()

    def delete_last(self):
        if self.doubly_linked_list:
            self.doubly_linked_list.pop()


doubly_linked_list = DoublyLinkedList()


def command_controller(command, n):
    global doubly_linked_list

    if command == 'insert':
        doubly_linked_list.insert(n)
    if command == 'deleteFirst':
        doubly_linked_list.delete_first()
    if command == 'deleteLast':
        doubly_linked_list.delete_last()
    if command == 'delete':
        doubly_linked_list.delete(n)


n = int(input())
for _ in range(n):
    command = list(input().split())
    command_controller(command[0], command[-1])
print(*doubly_linked_list.doubly_linked_list)

Submission status: Accepted

Solution code:
from collections import deque
d = deque()
for a in range(int(input())):
    com = input().split()
    if com[0] == "insert":
        d.appendleft(com[1])
    elif com[0] == "delete":
        if com[1] in d:
            d.remove(com[1])
    elif com[0] == "deleteFirst":
        d.popleft()
    elif com[0] == "deleteLast":
        d.pop()
    else:
        break
print(*d)

Submission status: Accepted

Solution code:
from sys import stdin
from collections import deque
input = stdin.readline

# 自作dequeでは実行時間的に通らないので...

n = int(input())
d: deque = deque([])

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

    if command[0] == "insert":
        x = int(command[1])
        d.appendleft(x)

    elif command[0] == "delete":
        x = int(command[1])
        if d.count(x):
            d.remove(x)

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

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

while d:
    tmp = d.popleft()

    if d:
        print(tmp, end=" ")
    else:
        print(tmp)

Submission status: Accepted

Solution code:
import sys
from collections import deque

def main():
    A = deque()
    input()
    for e in sys.stdin:
        if e[0]=='i': #insert xxx
            A.appendleft(e[7:-1])
        else:
            if e[6]==' ': #delete xxx
                m = e[7:-1]
                if m in A: 
                    A.remove(m)
            elif e[6]=='F':
                A.popleft()
            else:
                A.pop()
    print(' '.join(A))

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    from collections import deque

    n = int(input())
    A = deque([])
    for i in range(n):
        command = input()
        if command[6] == "F":
            A.popleft()
        elif command[6] == "L":
            A.pop()
        elif command[0] == "i":
            A.appendleft(int(command[7:]))
        else:
            try:
                A.remove(int(command[7:]))
            except ValueError:
                continue

    print(*A)

Submission status: Accepted

Solution code:
# ALDS1_3_C.py
'''
本来はlinkedList を自前実装する必要がありそうだが、Python だとDequeで実装できる。
'''
from collections import deque
N = int(input())
linkedList = deque([])
for i in range(N):
    command = input().split()
    if command[0] == "insert":
        linkedList.appendleft(command[1])
    elif command[0] == "delete":
        if command[1] in linkedList:
                linkedList.remove(command[1])
    elif command[0] == "deleteFirst":
        linkedList.popleft()
    elif command[0] == "deleteLast":
        linkedList.pop()
print(" ".join(list(linkedList)))

Submission status: Accepted

Solution code:
from collections import deque


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

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

        if cmd[0] == "deleteFirst":
            Q.popleft()
        elif cmd[0] == "deleteLast":
            Q.pop()
        elif cmd[0] == "insert":
            Q.appendleft(int(cmd[1]))
        elif cmd[0] == "delete" and int(cmd[1]) in Q:
            Q.remove(int(cmd[1]))

    print(*Q)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
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())
    d = DoublyLinkedList()
    for _ in range(n):
        c = input().rstrip()
        if c[0] == "i":
            d.insert(c[7:])
        elif c[6] == "F":
            d.deleteFirst()
        elif c[6] =="L":
            d.deleteLast()
        else:
            d.deleteKey(c[7:])
    ans = []
    cur = d.nil.next
    while cur != d.nil:
        ans.append(cur.key)
        cur = cur.next
    print(" ".join(ans))

Submission status: Accepted

Solution code:
from collections import deque

N = int(input())

d = deque([])
for i in range(N):
    a = input()
    if a == "deleteFirst":
        d.popleft()
    elif a == "deleteLast":
        d.pop()
    else:
        a, b = a.split()
        if a == "insert":
            d.appendleft(b)
        else:
            try:
                d.remove(b)
            except:
                pass

print(*d)

Submission status: Accepted

Solution code:
from collections import deque

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

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

		if cmdline[0] == "insert":
			L.appendleft(cmdline[1])
		elif cmdline[0] == "delete":
		    try:
			    L.remove(cmdline[1])
		    except ValueError:
			    pass
		elif cmdline[0] == "deleteFirst":
			L.popleft()
		elif cmdline[0] == "deleteLast":
			L.pop()

	print(" ".join(L))

Submission status: Accepted

Solution code:
from collections import deque

DLL = deque()

n = int(input())

for i in range(n):
    Line = input().split()
    
    if Line[0] == "insert":
        DLL.appendleft(Line[1])
        
    elif Line[0] == "delete":
        try:
            DLL.remove(Line[1])
        except ValueError:
            None
    elif Line[0] == "deleteFirst":
        DLL.popleft()
    elif Line[0] == "deleteLast":
        DLL.pop()

print(" ".join(DLL))

Submission status: Accepted

Solution code:
import sys, collections
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**10
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    n = I()
    command = [LSS() for _ in range(n)]

    que = collections.deque()
    for i in command:
        if i[0]=='insert':
            que.appendleft(i[1])
        elif i[0]=='delete':
            if i[1] in que:
                que.remove(i[1])
        elif i[0]=='deleteFirst':
            que.popleft()
        elif i[0]=='deleteLast':
            que.pop()

    print(*que)

if __name__ == '__main__':
    resolve()

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

print(*que)

Submission status: Accepted

Solution code:
from sys import stdin
from collections import deque

# 入力
n = int(input())

# command = [stdin.readline()[:-1] for a in range(n)]
command = stdin

# process
output = deque([])

for a in command:
    if a[0] == 'i':
        output.appendleft(int(a[7:]))
    elif a[0:7] == 'delete ':
        try:
            output.remove(int(a[7:]))
        except ValueError:
            pass
    elif a[6] == 'F':
        output.popleft()
    else:
        output.pop()

# 出力
print(*list(output))

Submission status: Accepted

Solution code:
from collections import deque


n = int(input())
a = deque()

for i in range(n):
        s = input()
        if s == 'deleteFirst':
                a.popleft()
        elif s == 'deleteLast':
                a.pop()
        else:     
            s ,t = s.split()
            if s == 'insert':
                a.appendleft(t)
            elif s == 'delete':
                if t in  a:
                    a.remove(t)
        
print(*a, sep = ' ')

Submission status: Accepted

Solution code:
from collections import deque
n = int(input())
deq = deque([])
for i in range(n):
    A = input().split()
    a = A[0]
    if len(A) == 2:
        b = A[1]
    if a == "insert":
        deq.appendleft(b)
    elif a == "deleteFirst":
        deq.popleft()
    elif a == "deleteLast":
        deq.pop()
    else:
        try:
            deq.remove(b)
        except:
            continue
print(" ".join(list(deq)))

Submission status: Accepted

Solution code:
from collections import deque
dq = deque()
for mak in range(int(input())) :
    com = input().split()
    if   com[0] == 'insert'      : dq.appendleft(com[1])
    elif com[0] == 'deleteFirst' : dq.popleft()
    elif com[0] == 'deleteLast'  : dq.pop()
    else : 
        if com[1] in dq :
            dq.remove(com[1])
print(' '.join(dq))

Submission status: Accepted

Solution code:
import math
import sys
import itertools
from collections import deque
# import numpy as np

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

  d = deque()
  for _ in range(n):
    order = input().split()

    if order[0] == 'insert':
      d.appendleft(order[1])
    elif order[0] == 'delete':
      try:
        d.remove(order[1])
      except ValueError:
        pass
    elif order[0] == 'deleteFirst':
      d.popleft()
    elif order[0] == 'deleteLast':
      d.pop()

  print(*d)

if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
from collections import deque
import sys

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

    for command in sys.stdin.readlines():

        if command.startswith("insert"):
            d.appendleft(int(command[len("insert"):]))
        elif command.startswith("delete "):
            if int(command[len("delete"):]) in d:
                d.remove(int(command[-2]))
        elif command.startswith("deleteLast"):
            d.pop()
        elif command.startswith("deleteFirst"):
            d.popleft()
        else:
            print(command)

        # print(*d, command, e)
    print(*d)

Submission status: Accepted

Solution code:
from collections import deque

que = deque([])
n = int(input())
for i in range(0, 	n):
	a = input()
	if a == 'deleteFirst':
		que.popleft()
#		print(a)
#		print(que)
	elif a == 'deleteLast':
		que.pop()
#		print(a)
#		print(que)
	else:
		a, b = a.split()
		b = int(b)
		if a == 'insert':
			que.appendleft(b)
#			print('{0} {1}'.format(a,b))
#			print(que)
		elif a == 'delete':
			if que.count(b) != 0:
				que.remove(b)
#			print('{0} {1}'.format(a, b))

print(*que)

#while not len(que) == 0:
#	if len(que) == 1:
#		print(que.pop())
#	else:
#		print(que.pop(), end=' ')

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline


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


class DoublyLinkedList:
    def __init__(self):
        self.n = 0
        self.dummy = Node(None)
        self.dummy.prev = self.dummy
        self.dummy.next = self.dummy

    def __getitem__(self, i):
        if i < 0 or i >= self.n:
            raise IndexError()
        return self._get_node(i).val 

    def __setitem__(self, i):
        if i < 0 or i >= self.n:
            raise IndexError()
        return self._get_node(i).val

    def __len__(self):
        return self.n

    def __bool__(self):
        return self.n != 0

    def __iter__(self):
        self.p = self.dummy
        return self

    def __next__(self):
        self.p = self.p.next
        if self.p == self.dummy:
            raise StopIteration
        return self.p.val

    def append(self, val):
        ptr = self.dummy.prev
        new_ptr = Node(val)
        new_ptr.prev = ptr.prev
        new_ptr.next = ptr
        new_ptr.next.prev = new_ptr
        new_ptr.prev.next = new_ptr
        self.n += 1

    def appendleft(self, val):
        ptr = self.dummy.next
        new_ptr = Node(val)
        new_ptr.prev = ptr.prev
        new_ptr.next = ptr
        new_ptr.next.prev = new_ptr
        new_ptr.prev.next = new_ptr
        self.n += 1

    def pop(self):
        ptr = self.dummy.prev
        ptr.prev.next = ptr.next
        ptr.next.prev = ptr.prev
        self.n -= 1

    def popleft(self):
        ptr = self.dummy.next
        ptr.prev.next = ptr.next
        ptr.next.prev = ptr.prev
        self.n -= 1

    def _get_node(self, i):
        if i < self.n // 2:
            ptr = self.dummy.next
            for _ in range(i):
                ptr = ptr.next
        else:
            ptr = self.dummy
            for _ in range(i, self.n):
                ptr = ptr.prev
        return ptr

    def _remove(self, i):
        if i < 0 or i >= self.n:
            raise IndexError()
        ptr = self._get_node(i)
        ptr.prev.next = ptr.next
        ptr.next.prev = ptr.prev
        self.n -= 1
        return ptr.val

    def _add(self, i, val):
        if i < 0 or i > self.n:
            raise IndexError()
        ptr = self._get_node(i)
        new_ptr = Node(val)
        new_ptr.prev = ptr.prev
        new_ptr.next = ptr
        new_ptr.next.prev = new_ptr
        new_ptr.prev.next = new_ptr
        self.n += 1

    def delete(self, val):
        ptr = self.dummy
        for _ in range(self.n):
            ptr = ptr.next
            if ptr.val == val:
                ptr.prev.next = ptr.next
                ptr.next.prev = ptr.prev
                self.n -= 1
                return True
        return False


dls = DoublyLinkedList()
input()
for query in sys.stdin:
    if query[0] == "i":
        val = query[7:-1]
        dls.appendleft(val)
    elif query[6] == "F":
        dls.popleft()
    elif query[6] == "L":
        dls.pop()
    else:
        val = query[7:-1]
        dls.delete(val)

print(*list(dls))

Submission status: Accepted

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

dl = deque()
for i in range(a):
    o = input().split()

    if o[0] == 'delete':
        try:
            dl.remove(o[1])
        except ValueError:
            pass
    elif o[0] == 'deleteFirst':
        dl.popleft()
    elif o[0] == 'deleteLast':
        dl.pop()
    else:
        dl.appendleft(o[1])

print(" ".join([str(i) for i in dl]))

Submission status: Accepted

Solution code:
from collections import deque

def readinput():
    n = int(input())
    commands = []
    for i in range(n):
        command = input().split()
        commands.append(command)
    return n,commands

def main(n,commands):
    l = deque([])
    for i in range(n):
        command = commands[i]
        if (command[0]=='insert'):
            op = command[1]
            l.appendleft(op)
        elif(command[0]=='delete'):
            op = command[1]
            try:
                l.remove(op)
            except:
                pass
        elif(command[0]=='deleteFirst'):
            l.popleft()
        elif(command[0]=='deleteLast'):
            l.pop()
        else:
            pass
        #print(' '.join(l))

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

    return l
if __name__=='__main__':
    #n, commands=readinput()
    #ans = main(n,commands)
    ans = main2()
    print(' '.join(ans))

Submission status: Accepted

Solution code:
from collections import deque
import sys

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

for _ in range(n):
    cmd = input()
    if cmd[0] == "i":
        deq.appendleft(cmd[7:])
    elif cmd[6] == "F":
        deq.popleft()
    elif cmd[6] == "L":
        deq.pop()
    else:
        try:
            deq.remove(cmd[7:])
        except:
            pass

print(" ".join(deq))

Submission status: Accepted

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

for i in range(n):
    cmd=input()
    if cmd=="deleteFirst":
        try:
            q.popleft()
        except:
            pass
        
    elif cmd=="deleteLast":
        try:
            q.pop()
        except:
            pass
        
    else:
        cmd,number=cmd.split()
        if cmd=="delete":
            try:
                q.remove(number)
            except:
                pass
            
        elif cmd=="insert":
            q.appendleft(number)
        
print(*q)

Submission status: Accepted

Solution code:
from sys import stdin

class Node:
    def __init__(self, value: int):
        self.value = value
        self.previous_node = None
        self.next_node = None


class LinkedList():
    def __init__(self):
        self.first = None
        self.last = None
    
    def delete_node(self, node: Node):
        if node.next_node != None:
            node.next_node.previous_node = node.previous_node
        if node.previous_node != None:
            node.previous_node.next_node = node.next_node
        if node == self.first:
            self.first = node.next_node
        if node == self.last:
            self.last = node.previous_node

    def value_generator(self):
        node = self.first
        while node != None:
            yield node.value
            node = node.next_node

    def insert(self, x: int):
        node = Node(x)
        node.next_node = self.first
        if self.first != None:
            self.first.previous_node = node
        self.first = node
        if self.last == None:
            self.last = node

    def delete(self, x: int):
        target_node = self.first
        while target_node != None and target_node.value != x:
            target_node = target_node.next_node
        if target_node != None:
            self.delete_node(target_node)


def main():
    linked_list = LinkedList()
    insert,delete,delete_node = linked_list.insert,linked_list.delete,linked_list.delete_node
    _ = int(stdin.readline())
    for line in (line.split() for line in stdin.readlines()):
        command = line[0]
        if command == "insert":
            insert(int(line[1]))
        elif command == "delete":
            delete(int(line[1]))
        elif command == "deleteFirst":
            delete_node(linked_list.first)
        elif command == "deleteLast":
            delete_node(linked_list.last)
    print(*linked_list.value_generator())


main()

Submission status: Accepted

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

Submission status: Accepted

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

que = deque()
for _ in range(n):
    cmd = input()

    if cmd == 'deleteFirst':
        try:
            que.popleft()
        except:
            pass
    elif cmd == 'deleteLast':
        try:
            que.pop()
        except:
            pass
    else:
        cmd, x = cmd.split()
        if cmd == 'insert':
            que.appendleft(x)
        elif cmd == 'delete':
            try:
                que.remove(x)
            except:
                pass

print(*que)

Submission status: Accepted

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

mylist = deque()


for i in range(n):
    a = input()
    if a == "deleteFirst":
        mylist.popleft()
    elif a == "deleteLast":
        mylist.pop()
    else:
        a,key = a.split()
        if a == "insert":
            mylist.appendleft(key)
        else:
            try:
                mylist.remove(key)
            except:
                pass

print(' '.join(mylist))

Submission status: Accepted

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

print(" ".join(l))

Submission status: Accepted

Solution code:
from collections import deque
import sys

input = sys.stdin.readline

n = int(input())

doubly_linked_list = deque()

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

print(" ".join(doubly_linked_list))

Submission status: Accepted

Solution code:
from collections import deque 

n=int(input())
l=deque()

for loop in range(n):
    command=input()
    
    if command=='deleteFirst':
        l.popleft()
    elif command=='deleteLast':
        l.pop()
    else:
        com,num=command.split()

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

Submission status: Accepted

Solution code:
from collections import deque
import sys
n = int(sys.stdin.readline())
#lines = sys.stdin.readlines()
lines = [input() for i in range(n)]

deq = deque()
for i in range(n):
    o = lines[i].split()
    if o[0] == 'insert':
        deq.appendleft(o[1])
    elif o[0] == 'delete':
        try:
            deq.remove(o[1])
        except:
            continue
    elif o[0] == 'deleteFirst':
        deq.popleft()
    elif o[0] == 'deleteLast':
        deq.pop()

print(' '.join(deq))

Submission status: Accepted

Solution code:
import collections
N=int(input())
A=collections.deque()
for i in range(N):
    c=input()
    if c[0]=='i':
        A.appendleft(int(c.split()[1]))
    elif c[6]==' ':
        if int(c.split()[1]) in A:
            A.remove(int(c.split()[1]))
    elif c[6]=='F':
        A.popleft()
    elif c[6]=='L':
        A.pop()
print(*A)

Submission status: Accepted

Solution code:
from collections import deque
que = deque()
for _ in range(int(input())):
    op, v = (input().split() + ["1"])[:2]
    if op == "insert":
        que.appendleft(v)
    elif op == "delete":
        try:
            que.remove(v)
        except:
            pass
    elif op == "deleteFirst":
        que.popleft()
    elif op == "deleteLast":
        que.pop()
print(' '.join(que))

Submission status: Accepted

Solution code:
import sys
import collections

N = int(input())

L = collections.deque()
for _ in range(N):
    com = (sys.stdin.readline().rstrip()).split()
    if com[0] == 'insert':
        L.appendleft(int(com[1]))
    elif com[0] == 'delete':
        try:
            L.remove(int(com[1]))
        except:
            pass
    elif com[0] == 'deleteFirst':
        L.popleft()
    elif com[0] == 'deleteLast':
        L.pop()

print(*L)

Submission status: Accepted

Solution code:
# -*- coding:utf8 -*-

from collections import deque
n = int(input())
c = [input() for _ in range(n)]
d = deque()

for com in c:
    if com[0] == 'i':
        d.appendleft(com[7:])
    else:
        s = com[6]
        if s == 'F':
            d.popleft()
        elif s == 'L':
            d.pop()
        else:
            try:
                d.remove(com[7:])
            except ValueError:
                pass
print(' '.join(d))

Submission status: Accepted

Solution code:

from collections import deque
import sys

d=deque()

n=int(input())

for i in range(n):
    s=input()
    if s=='deleteFirst':
        d.popleft()
    elif s=='deleteLast':
        d.pop()
    else:
        ss,num=s.split()
        if ss == 'insert':
            d.appendleft(num)
        else:
            try:
                d.remove(num)
            except:
                pass

print(' '.join(d))

"""
def main():
    num2 = 5
    plus(num2)
    print(num2)

def plus(num2):
    num2 += 2
    return num2

if __name__=="__main__":
    main()
    """

Submission status: Accepted

Solution code:
from collections import deque
import sys


deq = deque()

q = int(input())

for _ in range(q):
    s = input()
    if s == 'deleteFirst':
        deq.popleft()
    elif s == 'deleteLast':
        deq.pop()
    else:
        ss, num = s.split()
        if ss == 'insert':
            deq.appendleft(num)
        else:
            try:
                deq.remove(num)
            except:
                pass

print(" ".join(deq))

Submission status: Accepted

Solution code:
# ALDS1_3_C: Doubly Linked List
# collections.deque
# https://docs.python.org/3.8/library/collections.html#collections.deque

#n = 7
#A = [('insert', 5), ('insert', 2), ('insert', 3), ('insert', 1), 
#     ('delete', 3), ('insert', 6), ('delete', 5),]
from collections import deque

n = int(input())
mylist = deque()
for i in range(n):
    myinput = input().split()
    if len(myinput) == 1:
        order = myinput[0]
        x = 0
    else:
        order, x = myinput

    #print(order, x)
    if order == 'insert': 
        mylist.appendleft(x)
    elif order == 'delete':
        try:
            mylist.remove(x)
        except:
            pass
    elif order == 'deleteFirst':
        #mylist.remove(mylist[0])
        mylist.popleft()
    elif order == 'deleteLast':
        mylist.pop()
    #print(mylist)
print(" ".join(map(str, mylist)))