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
import sys
n = int(input())
deq = deque()
sub_deq = {}
for cmd in sys.stdin.readlines():
    if cmd == 'deleteFirst\n':
        while deq:
            data = deq.pop()
            if data[1]:
                data[1] = 0
                break
    elif cmd == 'deleteLast\n':
        while deq:
            data = deq.popleft()
            if data[1]:
                data[1] = 0
                break
    else:
        cmd, val = cmd.split()
        if cmd == 'insert':
            data = [val, 1]
            deq.append(data)
            if val not in sub_deq:
                sub_deq[val] = [data]
            else:
                sub_deq[val].append(data)
        elif cmd == 'delete':
            if val in sub_deq:
                deq_v = sub_deq[val]
                while deq_v:
                    data = deq_v.pop()
                    if data[1]:
                        data[1] = 0
                        break
sys.stdout.write(" ".join(data[0] for data in reversed(deq) if data[1]))
sys.stdout.write('\n')

Submission status: Accepted

Solution code:
from collections import deque
from sys import stdin
n = int(input())
ddl = deque()
for i in stdin:
    inp = i.split()
    if len(inp) == 2:
        op, data = inp
        data = int(data)
    else: op, data = inp[0], None
    if op == 'insert':
        ddl.appendleft(data,)
    elif op == 'delete':
        try:
            ddl.remove(data)
        except ValueError:
            pass
    elif op == 'deleteFirst':
        ddl.popleft()
    elif op == 'deleteLast':
        ddl.pop()

print(' '.join([str(item) for item in ddl]))

Submission status: Accepted

Solution code:
from collections import deque
from sys import stdin
n = int(input())
line = stdin.readlines()
ddl = deque()
for i in range(n):
    inp = line[i].split()
    if len(inp) == 2:
        op, data = inp
        data = int(data)
    else: op, data = inp[0], None
    if op == 'insert':
        ddl.appendleft(data,)
    elif op == 'delete':
        try:
            ddl.remove(data)
        except ValueError:
            pass
    elif op == 'deleteFirst':
        ddl.popleft()
    elif op == 'deleteLast':
        ddl.pop()

print(' '.join([str(item) for item in ddl]))

Submission status: Accepted

Solution code:
from collections import deque
import sys
 
dq = deque()
num = int(sys.stdin.readline())
line = sys.stdin.readlines()
 
for i in range(num):
 
    order = line[i].split()
 
    if order[0] == 'insert':
        dq.appendleft(order[1])
    elif order[0] == 'deleteFirst':
        dq.popleft()
    elif order[0] == 'deleteLast':
        dq.pop()
    else:
        if order[1] in dq:
            dq.remove(order[1])
 
print(' '.join(dq))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque
queue = deque([])
n = int(input())
for _ in range(n):
    command = input()
    if command == "deleteFirst":
        _ = queue.popleft()
    elif command == "deleteLast":
        _ = queue.pop()
    elif "delete" in command:
        _, x = command.split()
        try:
            queue.remove(x)
        except ValueError:
            pass
    else:
        _, x = command.split()
        queue.appendleft(x)
print(" ".join(queue))

Submission status: Accepted

Solution code:
n = int(raw_input())
L = []
c = 0
for i in xrange(n):
    cmd = raw_input()
    if cmd[0] == "i":
        L.append(cmd[7:])
    elif cmd[6] == " ":
        try:
            L.pop(~L[::-1].index(cmd[7:]))
        except:
            pass
    elif cmd[6] == "F":
        L.pop()
    else:
        c += 1

print " ".join(map(str, L[c:][::-1]))

Submission status: Accepted

Solution code:
import sys
from collections import deque
input_lines = sys.stdin.readlines()

doublyLinkedList = deque()


for x in input_lines[1:]:
    oneLine = x.split()
    if 'insert' in oneLine[0]:
        doublyLinkedList.appendleft((int)(oneLine[1]))
    elif 'delete' == oneLine[0]:
        remove_value = (int)(oneLine[1])
        if remove_value in doublyLinkedList:
            doublyLinkedList.remove(remove_value)
    elif 'deleteFirst'in oneLine[0]:
        doublyLinkedList.popleft()
    elif 'deleteLast' in oneLine[0]:
        doublyLinkedList.pop()

print(*doublyLinkedList)

Submission status: Accepted

Solution code:
from collections import deque

n = int(input())
d = deque()
for i in range(n):
    tmp = input().split()
    ope = tmp[0]
    if ope == "insert":
        m = tmp[1]
        d.appendleft(m)
    elif ope == "delete":
        m = tmp[1]
        if m in d:
            d.remove(m)
    elif ope == "deleteFirst":
        d.popleft()
    elif ope == "deleteLast":
        d.pop()
print(*d)

Submission status: Accepted

Solution code:
import sys
from collections import deque
n = int(sys.stdin.readline())
lines = sys.stdin.readlines()

Q = deque()
for i in range(n):
    command = lines[i].split()
    if command[0] == 'insert':
        Q.appendleft(command[1])
    elif command[0] == 'delete':
        try:
            Q.remove(command[1])
        except:
            pass
    elif command[0] == 'deleteFirst':
        Q.popleft()
    elif command[0] == 'deleteLast':
        Q.pop()
print(' '.join(Q))

Submission status: Accepted

Solution code:
from collections import deque
class DoublyLinkedKist:
    DLlist = deque()

    def command(self, cmd):
        if cmd[0] == 'insert':
            self.DLlist.appendleft(cmd[1])
        elif cmd[0] == 'delete':
            if cmd[1] in self.DLlist:
                self.DLlist.remove(cmd[1])
        elif cmd[0] == 'deleteFirst':
            self.DLlist.popleft()
        elif cmd[0] == 'deleteLast':
            self.DLlist.pop()

if __name__ == '__main__':
    DL = DoublyLinkedKist()
    for i in range(int(input())):
        DL.command(input().split())
    print(*DL.DLlist)

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
import sys


class Cell(object):
    __slots__ = ["value", "pre", "next"]

    def __init__(self, value=None, pre=None, _next=None):
        if pre is None and _next is None:
            self.value = None
            self.pre = self
            self.next = self
        else:
            self.value = value
            self.pre = pre
            self.next = _next


class MyList(object):
    def __init__(self):
        self._nil = Cell()

    def insert(self, value):
        self._nil.next = Cell(value, self._nil, self._nil.next)
        self._nil.next.next.pre = self._nil.next

    def delete(self, value):
        self._delete_to(self._search(value))

    def delete_first(self):
        self._delete_to(self._nil.next)

    def delete_last(self):
        self._delete_to(self._nil.pre)

    def toList(self):
        gen = self._val_generator()
        return [val for val in gen()]

    def _val_generator(self):
        def gen():
            current = self._nil.next
            while current is not self._nil:
                yield current.value
                current = current.next
        return gen

    def _delete_to(self, cell):
        if cell is not self._nil:
            cell.next.pre = cell.pre
            cell.pre.next = cell.next

    def _search(self, value):
        current = self._nil.next
        while current is not self._nil and current.value != value:
            current = current.next
        return current


def doubly_inked_list(commands):
    lst = MyList()
    for command in commands:
        if command[0] == "insert":
            lst.insert(command[1])
        elif command[0] == "delete":
            lst.delete(command[1])
        elif command[0] == "deleteFirst":
            lst.delete_first()
        elif command[0] == "deleteLast":
            lst.delete_last()
        else:
            raise ValueError
    return lst.toList()


if __name__ == "__main__":
    commands = [val.split() for val in sys.stdin.read().splitlines()]
    pop = commands.pop(0)
    print(" ".join(doubly_inked_list(commands)))

Submission status: Accepted

Solution code:
from collections import deque

queue = deque([])

n = int(input())

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

last = queue.pop()
for i in queue:
    print(i, end=" ")

print(last)

Submission status: Accepted

Solution code:
from collections import deque

queue = deque([])

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

print(" ".join(queue))

Submission status: Accepted

Solution code:
from collections import deque

queue = deque()

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

print(" ".join(queue))

Submission status: Accepted

Solution code:
from collections import deque

def main():

    queue = deque()

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

    print(" ".join(queue))

main()

Submission status: Accepted

Solution code:
import sys
from collections import deque

queue = deque()

for _ in range(int(sys.stdin.readline())):
    commands = sys.stdin.readline()[:-1].split(" ")
    if commands[0] == "insert":
        queue.appendleft(commands[1])
    elif commands[0] == "delete":
        try:
            queue.remove(commands[1])
        except ValueError:
            pass
    elif commands[0] == "deleteFirst":
        queue.popleft()
    elif commands[0] == "deleteLast":
        queue.pop()

print(" ".join(queue))

Submission status: Accepted

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

d = deque()

for _ in range(n):
    com = input()
    if (com[0] == 'i'):
        com ,i = com.split()
        d.appendleft(int(i))
    elif (com[0] == 'd'):
        if (com[6] == ' '):
            com, i = com.split()
            try:
                d.remove(int(i))
            except ValueError:
                pass
        elif (com[6] == 'F'):
            d.popleft()
        elif (com[6] == 'L'):
            d.pop()
ans = map(str, d)
ans = " ".join(ans)
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
query = int(input())
l = deque()
for _ in range(query):
    command = input().strip()
    if command == "deleteFirst":
        try:
            l.popleft()
        except:
            pass
    elif command == "deleteLast":
        try:
            l.pop()
        except:
            pass
    else:
        command, value = command.split(" ")
        if command == "insert":
            l.appendleft(int(value))
        elif command == "delete":
            try:
                l.remove(int(value))
            except:
                pass
print(*l)

Submission status: Accepted

Solution code:
from collections import deque

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

for i in range(n):
	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:
from collections import deque

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

for i in range(n):
	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:
from collections import deque
n = int(input())
dlist = deque()
for i in range(n):
    code = input().split()
    if code[0] == "insert":
        dlist.insert(0,code[1])
    if code[0] == "delete":
        try:
            dlist.remove(code[1])
        except:
            continue
    if code[0] == "deleteFirst":
        dlist.popleft()
    if code[0] == "deleteLast":
        dlist.pop()
print(*dlist,sep=" ")

Submission status: Accepted

Solution code:
import sys
from collections import deque
 
n = int(sys.stdin.readline())
que = deque()
for i in range(n):
    line = sys.stdin.readline()
    op = line.rstrip().split()
    if "insert" == op[0]:
        que.appendleft(op[1])
    elif "delete" == op[0]:
        if op[1] in que:
            que.remove(op[1])
    elif "deleteFirst" == op[0]:
        que.popleft()
    else:
        que.pop()
 
print(*que)

Submission status: Accepted

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

from collections import deque

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

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

    print(" ".join(L))

Submission status: Accepted

Solution code:
from collections import deque
dq = deque()
for _ in range(int(input())):
    ss = input()
    if ss == "deleteFirst":
        del dq[0]
    elif ss == "deleteLast":
        del dq[-1]
    else:
        c, s = ss.split()
        s = int(s)
        if c == "insert":
            dq.appendleft(s)
        elif c == "delete":
            if s in dq:
                dq.remove(s)
print(*dq)

Submission status: Accepted

Solution code:
import sys
from collections import deque
readline = sys.stdin.readline
dq = deque()
for _ in range(int(input())):
    ss = readline().strip()
    if ss == "deleteFirst":
        del dq[0]
    elif ss == "deleteLast":
        del dq[-1]
    else:
        c, s = ss.split()
        s = int(s)
        if c == "insert":
            dq.appendleft(s)
        elif c == "delete":
            if s in dq:
                dq.remove(s)
print(*dq)

Submission status: Accepted

Solution code:
import sys
from collections import deque
readline = sys.stdin.readline
dq = deque()
for _ in range(int(input())):
    c = readline().split()
    if c[0] == "deleteFirst":
        del dq[0]
    elif c[0] == "deleteLast":
        del dq[-1]
    else:
        if c[0] == "insert":
            dq.appendleft(c[1])
        elif c[0] == "delete":
            if c[1] in dq:
                dq.remove(c[1])
print(*dq)

Submission status: Accepted

Solution code:
import sys
from collections import deque
readline = sys.stdin.readline
dq = deque()
for _ in range(int(input())):
    c = readline().split()
    if c[0] == "deleteFirst":
        dq.popleft()
    elif c[0] == "deleteLast":
        dq.pop()
    else:
        if c[0] == "insert":
            dq.appendleft(c[1])
        elif c[0] == "delete":
            if c[1] in dq:
                dq.remove(c[1])
print(*dq)

Submission status: Accepted

Solution code:
import collections
d=collections.deque()
for _ in range(int(input())):
 e=input()
 if'i'==e[0]:d.appendleft(e.split()[1])
 else:
  if' '==e[6]:
   m=e.split()[1]
   if m in d:d.remove(m)
  elif len(e)%2:d.popleft()
  else:d.pop()
print(*d)

Submission status: Accepted

Solution code:
import collections,sys
d=collections.deque()
input()
for e in sys.stdin.readlines():
 if'i'==e[0]:d.appendleft(e.split()[1])
 else:
  if' '==e[6]:
   m=e.split()[1]
   if m in d:d.remove(m)
  elif len(e)%2:d.pop()
  else:d.popleft()
print(*d)

Submission status: Accepted

Solution code:
import collections

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

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

    if line[0] == 'i':
        que.appendleft(int(line[7:]))
    elif line[6] == ' ':
        try:
            que.remove(int(line[7:]))
        except ValueError:
            pass
    elif line[6] == 'F':
        que.popleft()
    else:
        que.pop()

print(*que)

Submission status: Accepted

Solution code:
import collections,sys
d=collections.deque()
input()
for e in sys.stdin:
 if'i'==e[0]:d.appendleft(e.split()[1])
 else:
  if' '==e[6]:
   m=e.split()[1]
   if m in d:d.remove(m)
  elif len(e)%2:d.pop()
  else:d.popleft()
print(*d)

Submission status: Accepted

Solution code:
class Node(object):
    def __init__(self, num, prv = None, nxt = None):
        self.num = num
        self.prv = prv
        self.nxt = nxt
 
 
class DoublyLinkedList(object):
    def __init__(self):
        self.start = self.last = None
 
    def insert(self, num):
        new_elem = Node(num)
 
        if self.start is None:
            self.start = self.last = new_elem
        else:
            new_elem.nxt = self.start
            self.start.prv = new_elem
            self.start = new_elem
 
    def delete_num(self, target):
        it = self.start
        while it is not None:
            if it.num == target:
                if it.prv is None and it.nxt is None:
                    self.start = self.last = None
                else:
                    if it.prv is not None:
                        it.prv.nxt = it.nxt
                    else:
                        self.start = self.start.nxt
 
                    if it.nxt is not None:
                        it.nxt.prv = it.prv
                    else:
                        self.last = self.last.prv
                break
            it = it.nxt
 
    def delete_start(self):
        if self.start is self.last:
            self.start = self.last = None
        else:
            self.start.nxt.prv = None
            self.start = self.start.nxt
 
    def delete_last(self):
        if self.start is self.last:
            self.start = self.last = None
        else:
            self.last.prv.nxt = None
            self.last = self.last.prv
 
    def get_content(self):
        ret = []
        it = self.start
 
        while it is not None:
            ret.append(it.num)
            it = it.nxt
 
        return ' '.join(ret)
 
 
def _main():
    from sys import stdin
 
    n = int(input())
 
    lst = DoublyLinkedList()
     
    for _ in range(n):
        cmd = stdin.readline().strip().split()
        if cmd[0] == 'insert':
            lst.insert(cmd[1])
        elif cmd[0] == 'delete':
            lst.delete_num(cmd[1])
        elif cmd[0] == 'deleteFirst':
            lst.delete_start()
        elif cmd[0] == 'deleteLast':
            lst.delete_last()
 
    print(lst.get_content())
 
 
if __name__ == '__main__':
    _main()

Submission status: Accepted

Solution code:
class Node(object):
    def __init__(self, num, prev = None, nxt = None):
        self.num = num;
        self.prev = prev;
        self.nxt = nxt;

class Double_Linked_List(object):
    def __init__(self):
        self.first = self.last = None;

    def insert(self, num):
        node = Node(num);
        if self.first is not None:
            self.first.prev = node;
            node.nxt = self.first;
            self.first = node;
        else:
            self.first = node;
            self.last = node;

    def delete(self, num):
        current = self.first;
        while current is not None:
            if current.num == num:
                prev = current.prev;
                nxt = current.nxt;
                if prev is not None:
                    current.prev = None;
                    prev.nxt = nxt;
                if nxt is not None:
                    current.nxt = None;
                    nxt.prev = prev;
                if nxt is None:
                    self.last = prev;
                if prev is None:
                    self.first = nxt;
                break;
            else:
                current = current.nxt;

    def delete_first(self):
        if self.first is not None:
            nxt = self.first.nxt;
            if nxt is not None:
                self.first.nxt = None;
                nxt.prev = None;
                self.first = nxt;
            else:
                self.first = None;
                self.last = None;

    def delete_last(self):
        if self.last is not None:
            prev = self.last.prev;
            if prev is not None:
                self.last.prev = None;
                prev.nxt = None;
                self.last = prev;
            else:
                self.first = None;
                self.last = None;

    def get_content(self):
        ret = [];
        append = ret.append;
        current = self.first;
        while current is not None:
            append(current.num);
            current = current.nxt;
        return " ".join(ret);

def main():
    from sys import stdin
    double_linked_list = Double_Linked_List();
    count = int(input());
    delete_first = double_linked_list.delete_first;
    delete_last = double_linked_list.delete_last;
    delete = double_linked_list.delete;
    insert = double_linked_list.insert;
    for i in range(count):
        command = stdin.readline().strip().split();
        if command[0] == "insert":
            insert(command[1]);
        if command[0] == "delete":
            delete(command[1]);
        elif command[0] == "deleteFirst":
            delete_first();
        elif command[0] == "deleteLast":
            delete_last();
    print(double_linked_list.get_content());

main();

Submission status: Accepted

Solution code:
from collections import deque
 
n=int(input())
command=[0 for i in range(n)]
k=[0 for i in range(n)]
k_queue= deque([])
 
for i in range(n):
    s=input()
    if s=="deleteFirst" or s=="deleteLast":
        command[i],k[i]=s,0
    else:
        command[i],k[i]=s.split()
     
for i in range(n):
    if command[i]=="insert":
        k_queue.appendleft(k[i])
    elif command[i]=="deleteFirst":
        k_queue.popleft()
    elif command[i]=="deleteLast":
        k_queue.pop()
    else:
        try: k_queue.remove(k[i])
        except ValueError: y=0
print( " ".join(map(str, k_queue)))

Submission status: Accepted

Solution code:
from collections import deque

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

print(' '.join(dq))

Submission status: Accepted

Solution code:
import collections
hoge = collections.deque()
n = int(input())
for _ in range(n):
    commands = input().split()
    if len(commands) == 1:
        if commands[0] == 'deleteFirst':
            hoge.popleft()
        else:
            hoge.pop()
    else:
        com, x = commands
        if com == 'insert':
            hoge.insert(0, x)
        else:
            if x in hoge:
                hoge.remove(x)
print (' '.join(hoge))

Submission status: Accepted

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

dll = deque()

n = int(input())
for _ in range(n):
  raw = input().split()
  command = raw[0]
  if command == 'delete': 
    value = raw[1]
    try:
      dll.remove(value)
    except ValueError as err:
      continue
  elif command == 'insert':
    value = raw[1]
    dll.appendleft(value)
  elif command == 'deleteFirst':
    try:
      dll.popleft()
    except IndexError as err:
      continue
  elif command == 'deleteLast':
    try:
      dll.pop()
    except IndexError as err:
      continue

print(" ".join(dll))

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.split()[1])
  else:
   if' '==e[6]:
    m=e.split()[1]
    if m in d:d.remove(m)
   elif len(e)%2:d.pop()
   else:d.popleft()
 print(*d)
if'__main__'==__name__:s()

Submission status: Accepted

Solution code:
from collections import deque

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

Submission status: Accepted

Solution code:
import collections

a = collections.deque()

for i in range(int(input())):
    b = input()
    # 命令がinsertだった場合、
    if b[0] == 'i':
        # キーxを先頭に追加(数字は文字列bの7文字目以降にある)
        a.appendleft(b[7:])
    # 命令がdeleteだった場合
    elif b[6] == ' ':
        # 命令のキーが含まれる場合そのキーのデータを消すが、なければ何もしない
        try:
            a.remove(b[7:])
        except:
            pass
    # 命令がdeleteFirstだった場合
    elif len(b) > 10:
        a.popleft()
    # 命令がdeleteLastだった場合
    elif len(b) > 6:
        a.pop()

print(*a)

Submission status: Accepted

Solution code:
import sys


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

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

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

    def delete(self, node):
        if node in (self.head, self.tail):
            return
        else:
            node.prev.next, node.next.prev = node.next, node.prev

    def delete_first(self):
        self.delete(self.head.next)

    def delete_last(self):
        self.delete(self.tail.prev)

    def delete_first_value(self, v):
        node = self.find(v, self.head)
        if node is not None:
            self.delete(node)

    def find(self, v, cur=None):
        if cur is None:
            cur = self.head

        while cur != self.tail:
            cur = cur.next
            if cur.value == v:
                return cur

        return None

    def __str__(self):
        cur = self.head.next
        vs = ''
        while cur != self.tail:
            vs += cur.value
            cur = cur.next
            if cur != self.tail:
                vs += ' '

        return vs


def run():
    steps = int(input())  # flake8: noqa
    dl = DoubleLinkedList()

    for command in sys.stdin:
        if command.startswith('insert'):
            dl.insert(command[7:-1])
        elif command.startswith('deleteFirst'):
            dl.delete_first()
        elif command.startswith('deleteLast'):
            dl.delete_last()
        elif command.startswith('delete'):
            dl.delete_first_value(command[7:-1])
        else:
            raise ValueError()

    print(dl)


if __name__ == '__main__':
    run()

Submission status: Accepted

Solution code:
from collections import deque
d = deque()
for _ in range(int(input())):
  a = input()
  if "i" == a[0]: d.appendleft(int(a[7:]))
  elif "F" == a[6]: d.popleft()
  elif "L" == a[6]: d.pop()
  else:
    try: d.remove(int(a[7:]))
    except: pass
print(*d)

Submission status: Accepted

Solution code:
from collections import deque
import sys

n = int(input())
result = deque()
func = {'insert':result.appendleft,\
        'delete':result.remove,\
        'deleteFirst':result.popleft,\
        'deleteLast':result.pop}

for line in sys.stdin.readlines():
    c = line.split()
    if len(c) > 1:
        try:
            func[c[0]](int(c[1]))
        except:
            continue
    else:
        func[c[0]]()

print(*result)

Submission status: Accepted

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

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 len(e)%2:d.pop()
   else:d.popleft()
 print(*d)
s()

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 len(e)%2:d.pop()
   else:d.popleft()
 print(*d)
if'__main__'==__name__:s()

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 len(e)&1:d.pop()
   else:d.popleft()
 print(*d)
if'__main__'==__name__:s()

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