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:
from collections import deque
from enum import Enum
import sys
import math
 
BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001
 
 
LIST = deque()
 
num_command = int(input())
 
for loop in range(num_command):
 
    input_command = input()
 
    if input_command == 'deleteFirst':
        LIST.popleft()
    elif input_command == 'deleteLast':
        LIST.pop();
    else:
        com,num = input_command.split()
 
        if com == 'insert':
            LIST.appendleft(num)
        elif com == 'delete':
            try:
                LIST.remove(num)
            except:
                pass
 
print(' '.join(LIST))

Submission status: Accepted

Solution code:
from collections import deque

n = int(input())
linked_list = deque()

for _ in range(n):
    row = input().split()
    if len(row) == 1:
        command = row[0]
    else:
        command, key = row
    # print(linked_list, row)
    if command == 'deleteFirst' and linked_list:
        del linked_list[0]
    elif command == 'deleteLast' and linked_list:
        del linked_list[-1]
    elif command == 'delete':
        if key in linked_list:
            linked_list.remove(key)
    else:
        linked_list.appendleft(key)

print(*linked_list)

Submission status: Accepted

Solution code:
from collections import deque

if __name__ == '__main__':
    res = deque([])
    n_order = int(input())
    for i in range(n_order):
        order = input().split()
        if 'insert' == order[0]: 
            res.appendleft(order[1])
        elif 'delete' == order[0]:
            if order[1] in res:
                res.remove(order[1])
        elif 'deleteFirst' == order[0]:
            res.popleft()
        elif 'deleteLast' == order[0]: 
            res.pop()
    print(' '.join(res))

Submission status: Accepted

Solution code:
from collections import deque


if __name__ == "__main__":
    operation_num = int(input())
    double_linked_list: deque = deque()

    for _ in range(operation_num):
        operation = input()

        if ("deleteFirst" == operation):
            double_linked_list.popleft()
        elif ("deleteLast" == operation):
            double_linked_list.pop()
        else:
            command, num = operation.split(" ")
            if ("insert" == command):
                double_linked_list.appendleft(num)
            else:
                try:
                    double_linked_list.remove(num)
                except ValueError:
                    pass

    print(" ".join(double_linked_list))

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N = int(input())
    # linked_list = LinkedList()

    import collections
    d = collections.deque()

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

        if val[0] == 'insert':
            # linked_list.insert(int(val[1]))
            d.appendleft(val[1])
        elif val[0] == 'delete':
            # linked_list.delete_key(int(val[1]))
            try:
                d.remove(val[1])
            except ValueError:
                pass
            
        elif val[0] == 'deleteFirst':
            # linked_list.delete_first()
            d.popleft()
            # result.remove(result[0])
        elif val[0] == 'deleteLast':
            # linked_list.delete_last()
            d.pop()

    # print(linked_list)
    print(' '.join(d))

Submission status: Accepted

Solution code:
from collections import deque


def main():
    lst = deque()
    n = int(input())
    for _ in range(n):
        inputs = input().split(' ')
        command = inputs[0]
        if command == 'insert':
            lst.appendleft(inputs[1])
        elif command == 'delete':
            try:
                lst.remove(inputs[1])
            except Exception:
                pass
        elif command == 'deleteFirst':
            lst.popleft()
        elif command == 'deleteLast':
            lst.pop()
    result = ''
    while len(lst) > 0:
        result += lst.popleft() + ' '
    print(result.strip())


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
"""
# 標準入力取得
## 文字列
 = sys.stdin.readline().rstrip()
 = list(sys.stdin.readline().rstrip())

## 数値
 = int(sys.stdin.readline())
 = map(int, sys.stdin.readline().split())
 = list(map(int, sys.stdin.readline().split()))
 = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  # 二次元配列入力　二次元マップみたいな入力のとき

# 0埋め, 小数点出力桁指定時のときの出力
a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654

# 文字列をリストに格納
char_list = list("abcd") # ["a","b","c","d"]
"""

import math
import sys
import itertools
import queue
from fractions import gcd
from collections import deque


mod = 1000000007

if __name__ == "__main__":
    n = int(sys.stdin.readline())
    commands = {"insert": lambda l,x:l.appendleft(x),
    "delete": lambda l,x:l.remove(x) if x in l else x,
    "deleteFirst": lambda l:l.popleft(),
    "deleteLast": lambda l:l.pop(),
    }
    l=deque()
    for i in range(n):
        c = sys.stdin.readline().split()
        if c[0]=="deleteFirst" or c[0]=="deleteLast":
            commands[c[0]](l)
        elif c[0] in commands:
            commands[c[0]](l,c[1])
            # l.append(commands[c](l.pop(),l.pop()))
    l_list=list(l)
    print(*l_list)

Submission status: Accepted

Solution code:
from collections import deque
from sys import stdin
N = int(stdin.readline().rstrip())
d = deque()
for i in range(N):
    op_key = stdin.readline().rstrip().split()
    if op_key[0] == 'insert':
        d.appendleft(op_key[1])
    elif op_key[0] == 'delete':
        try:
            d.remove(op_key[1])
        except:
            pass
    elif op_key[0] == 'deleteFirst':
        d.popleft()
    elif op_key[0] == 'deleteLast':
        d.pop()
print(*d)

Submission status: Accepted

Solution code:
from collections import deque

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

for i in range(n):
    cm=input()
    if cm!="deleteFirst" and cm!="deleteLast":
        cm,x=cm.split()
    
    if cm=="insert":
        q.appendleft(x)
    elif cm=="delete":
        try:
            q.remove(x)
        except:
            continue
    elif cm=="deleteFirst":
        q.popleft()
    else:
        q.pop()
print(*q)

Submission status: Accepted

Solution code:
import sys
from collections import deque

input = sys.stdin.readline

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

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# ALDS1_3_C_Doubly_Linked_List

import collections

def doublylinkedlist(d, op):
    if "insert" in op:
        d.appendleft(op.split()[1])
    elif op == "deleteFirst":
        d.popleft()
    elif op == "deleteLast":
        d.pop()
    else:
        try:
            d.remove(op.split()[1])
        except:
            pass
        
    return d

n = int(input())
d = collections.deque()
for _ in range(n):
    doublylinkedlist(d, input())

print(" ".join(d))

Submission status: Accepted

Solution code:
"""
双方向連結リスト
どっちからでもつけられるよ
"""
from collections import deque
d = deque([])
n = int(input())
for _ in range(n):
    
    com = input()
    if com == "deleteFirst" and len(d)>0:
        d.popleft()
    elif com == "deleteLast" and len(d)>0:
        d.pop()
    elif com != "deleteFirst" and com != "deleteLast":
        com, num = com.split(" ")
        if com == "insert":
            d.appendleft(int(num))
        elif com == "delete":
            try:
                d.remove(int(num))
            except:
                continue
    
ans = " ".join([str(s) for s in list(d)])
print(ans)

Submission status: Accepted

Solution code:
from collections import deque

if __name__ == '__main__':
	n = int(input())
	tmp = deque()
	for i in range(n):
		command = input()
		if command == "deleteFirst":
			tmp.popleft()
		elif command == "deleteLast":
			tmp.pop()
		else:
			c,d = command.split()
			if c == "insert":
				tmp.appendleft(int(d))
			elif c == "delete":
				try:
					tmp.remove(int(d))
				except:
					pass
	print(*tmp)

Submission status: Accepted

Solution code:
from enum import Enum
from queue import Queue
import collections

import sys
import math

BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001

A = collections.deque()
n = int(input())
for _ in range(n):
    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)

Submission status: Accepted

Solution code:
from collections import deque

def main():
    n = int(input())
    d = deque()
    for _ in range(n):
        command = input()
        if command == "deleteFirst":
            d.popleft()
        elif command == "deleteLast":
            d.pop()
        else:
            com, num = command.split(" ")
            if com == "insert":
                d.appendleft(num)
            elif com == "delete":
                try:
                    d.remove(num)
                except:
                    pass
    print(" ".join(d))


main()

Submission status: Accepted

Solution code:
from collections import deque


def main():
    ans = deque()
    n = int(input())
    for i in range(n):
        c = input()
        if c == "deleteFirst":
            ans.popleft()
        elif c == "deleteLast":
            ans.pop()
        else:
            c, x = c.split()
            if c == "insert":
                ans.insert(0, x)
            else:
                try:
                    ans.remove(x)
                except ValueError:
                    pass
    print(*ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque


# INPUT
n = int(input())


# PROCESS
que = deque()

for _ in range(n):

    command = input()

    if   command == "deleteFirst":
        que.popleft()
    elif command == "deleteLast":
        que.pop()
    else:
        command, x = command.split()

        if   command == "insert":
            que.appendleft(int(x))

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


# OUTPUT
print(*que)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections
num=collections.deque([])

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

Submission status: Accepted

Solution code:
# reffer to http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2968766#1
class Node(object):
  def __init__(self, num, prv = None, nxt = None):
    self.num = num
    self.prv = None
    self.nxt = None

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

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

    if self.start is None:
      # 最初と最後にnew_elemをセット
      self.start = self.last = new_elem
    else:
      # 前に追加していくのでnew_elemの次にself.startをセット
      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:
        # nodeが1つしか無いかのチェック
        if it.prv is None and it.nxt is None:
          self.start = self.last = new_elem
        else:
          # 先頭かどうかのチェック
          if it.prv is not None:
            it.prv.nxt = it.nxt
          else:
            self.start = self.start.nxt

          # 最後かどうかのチェック
          if it.nxt is not None:
            it.nxt.prv = it.prv
          else:
            self.last = self.last.prv

        break

      # 対象nodeを次に回す
      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()

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
from collections import deque

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

Submission status: Accepted

Solution code:
from collections import deque


n = int(input())

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

print(' '.join(l))

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 # prevもnextも番兵に接続

    def insert(self, key):
        new = Node(key)
        new.next = self.nil.next   # 新しいNodeの後ろをもともと番兵の後ろにあったNodeに変更
        self.nil.next.prev = new   # もともと番兵の次にあったNodeの前を新しいNodeに変更
        self.nil.next = new        # 番兵の後ろを新しいNodeに変更
        new.prev = self.nil             # 新しいNodeの前をnilに指定

    def listSearch(self, key):
        cur = self.nil.next
        while cur != self.nil and cur.key != key:
            cur = cur.next
        return cur

    def deleteNode(self, t):
        if t == self.nil:
            return
        t.prev.next = t.next
        t.next.prev = t.prev

    def deleteFirst(self):
        self.deleteNode(self.nil.next)

    def deleteLast(self):
        self.deleteNode(self.nil.prev)

    def deleteKey(self, key):
        self.deleteNode(self.listSearch(key))


if __name__ == '__main__':
    import sys
    input = sys.stdin.readline

    n = int(input())

    dll = DoublyLinkedList()

    for _ in range(n):
        com = input().rstrip()
        # keyはinsert,deleteなら[x]になり、
        # deleteFirst,deleteLastなら[]になる。
        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))

Submission status: Accepted

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

print(*c)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
#MAX_ARRAY_SIZE = 10**6

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

nil = Node(None)

#class DoublyLinkedList:
#    global nil
def init():
    nil.next = nil
    nil.prev = nil

def insert(x):
    node = Node(x)
    # 番兵の直後に要素を追加
    node.next = nil.next
    nil.next.prev = node
    nil.next = node
    node.prev = nil

def delete(x):
    # 検索したノードを削除
    deleteNode(listSearch(x))

def deleteFirst():
    deleteNode(nil.next)

def deleteLast():
    deleteNode(nil.prev)

def deleteNode(node):
    # nodeが番兵の場合は処理をしない
    if node is nil:
        return
    node.prev.next = node.next
    node.next.prev = node.prev
    del node

def listSearch(x):
    # 番兵の次の要素から辿る
    cur = nil.next
    while cur is not nil and cur.data != x:
        cur = cur.next
    return cur

#double_list = DoublyLinkedList()
init()

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

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

Submission status: Accepted

Solution code:
from collections import deque

LIST = deque()

num_command = int(input())

for loop in range(num_command):
    input_command = input()

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

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

Submission status: Accepted

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

print(" ".join(ans))

Submission status: Accepted

Solution code:
from collections import deque


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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
from collections import deque

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

print(' '.join(num))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque

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

    print(" ".join(que))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque

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

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

print(" ".join(list))

Submission status: Accepted

Solution code:
from collections import deque

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

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

Submission status: Accepted

Solution code:
#! /usr/local/bin/python3
# coding: utf-8

class DoublyLinkedNode:

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

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

class DoublyLinkedList:

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

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

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

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

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

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

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

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

    import sys

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

    dll.print()

if __name__ == "__main__":
    _main()

Submission status: Accepted

Solution code:
from collections import deque

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

for i in range(n):
  input_str=input().split()
  if input_str[0]=="insert":
      my_Linkedlist.appendleft(input_str[1])
  elif input_str[0]=="delete":
      if my_Linkedlist.count(input_str[1])>0:
          my_Linkedlist.remove(input_str[1]) #删除第一个值为value的元素
  elif input_str[0]=="deleteFirst":
      my_Linkedlist.popleft()
  elif input_str[0]=="deleteLast":
    my_Linkedlist.pop()
    
print(*my_Linkedlist)

Submission status: Accepted

Solution code:
from collections import deque


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

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

print(*b)

Submission status: Accepted

Solution code:
from collections import deque
import sys


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


if '__main__' == __name__:
    main()

Submission status: Accepted

Solution code:
from sys import stdin
from collections import deque

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

print(*lst)

Submission status: Accepted

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


for i in range(N):
    c = input()
    if c == 'deleteFirst':
        a.popleft()
    elif c == 'deleteLast':
        a.pop()
    else:
        c, n = c.split()
        if c == 'insert':
            a.appendleft(n)
        elif c == 'delete':
            try:
                a.remove(n)
            except:
                pass

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
連結リスト
"""
from collections import deque

N = int(input())

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