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


def main():
    queue = deque()
    for _ in range(int(input())):
        command = input().split()
        if len(command) == 2:
            if command[0] == 'insert':
                queue.appendleft(command[1])
            else:   # delete
                try:
                    queue.remove(command[1])
                except ValueError:
                    pass
        elif command[0] == 'deleteFirst':
            queue.popleft()
        else:   # deleteLast
            queue.pop()

    print(' '.join(queue))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_3_C&lang=jp
#   Doubly Linked List : python3
#  2018.11.27 yonezawa


from collections import deque

import sys
input = sys.stdin.readline
import cProfile


def main():
    q = deque()
    n =  int(input())
    skipFlag = False

    while n > 0:
        if skipFlag == False:
            l = list(map(str,input().split()))
            if len(l) == 0:
                break
        skipFlag = False
        cmd = l[0]
        if ( cmd == 'insert'):
            d = int(l[1])
            if n <= 1:
                q.appendleft(d)
                n -= 1
                continue
            l = list(map(str,input().split()))
            if len(l) == 0:
                q.appendleft(d)
                break
            if (l[0] == 'deleteFirst' ):
                n -= 1
                continue
            elif (len(q) == 0 and l[0] == 'deleteLast'):
                n -= 1
                continue
            else:
                skipFlag = True
                q.appendleft(d)
        elif ( cmd == 'delete'):
            try:
                q.remove(int(l[1]))
            except:
                None
        elif ( cmd == 'deleteFirst'):
            q.popleft()
        else:
            q.pop()
        n -= 1

    print (*q)

if __name__ == '__main__':
   main()
#    pr = cProfile.Profile()
#    pr.runcall(main)
#    pr.print_stats()

Submission status: Accepted

Solution code:
from collections import deque

LIST = deque()

n = int(input())

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

    if input_command == 'deleteFirst':
        LIST.popleft()
    elif input_command == 'deleteLast':
        LIST.pop()
    else:
        com, num = input_command.split()  #input_commandでないと動作しない

        if com == 'insert':
            LIST.appendleft(num)
        elif com == 'delete':
            try:
                LIST.remove(num)
            except:
                pass  #なんもなかったらpass

print(' '.join(LIST))

Submission status: Accepted

Solution code:
# https://onlinejudge.u-aizu.ac.jp/courses/lesson/1/ALDS1/3/ALDS1_3_C


import sys
from collections import deque


def solve():
    # linked list
    dll = deque()

    # get all lines at one time
    lines = sys.stdin.readlines()

    for i in range(1, int(lines[0]) + 1):
        # get one line
        ope = lines[i].split()

        # insert X
        if ope[0] == 'insert':
            dll.appendleft(ope[1])

        # delete ...
        elif ope[0] == 'delete':
            if ope[1] in dll:
                dll.remove(ope[1])
        elif ope[0] == 'deleteFirst':
            dll.popleft()
        elif ope[0] == 'deleteLast':
            dll.pop()

    print(*dll, sep=' ')


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
from collections import deque

queue = deque()
n=int(input())

for i in range(n):
    command=input()
    if command == 'deleteFirst':
        queue.popleft()
    elif command == 'deleteLast':
        queue.pop()
    else:
        ope, num = command.split()
        if ope == 'delete':
            try:
                queue.remove(num)
            except:
                pass
        else:
            queue.appendleft(num)
result=list(queue)
print(*result)

Submission status: Accepted

Solution code:
import sys
from collections import deque

def main():
    line = sys.stdin.readlines()
    deq = deque()
    for i in range(1, int(line[0]) + 1):
        input_command = line[i]

        if input_command[0] == 'i':         # 'insert index'
            deq.appendleft(input_command[7:-1])
        elif input_command[0] == 'd':
            if input_command[6] == ' ':     # 'delete index'
                key = input_command[7:-1]
                if key in deq:
                    deq.remove(key)
            elif input_command[6] == 'F':   # 'deleteFirst':
                deq.popleft()
            elif input_command[6] == 'L':   # 'deleteLast':
                deq.pop()

    print(' '.join(deq))
    return


main()

Submission status: Accepted

Solution code:
from collections import deque
x=deque([])
for i in range(int(input())):
	a=input().split()
	if a[0]=='insert':
		x.insert(0,a[1])
	elif a[0]=='delete':
		if a[1] in x:
			x.remove(a[1])		
	elif a[0]=='deleteFirst':
		x.popleft()
	elif a[0]=='deleteLast':
		x.pop()
print(' '.join(x))

Submission status: Accepted

Solution code:
from collections import deque

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

Submission status: Accepted

Solution code:
import sys
head = -1
tail = -1
dll = []
for i in range(int(sys.stdin.readline())):
    line = sys.stdin.readline()

    if line[6]=='F':
        if head==tail:
            dll=[]
            head=-1
            tail=-1
        else:
            nxt=dll[head][2] #                                                  
            if nxt>=0:  dll[nxt][1]=-1
            if tail==head: tail=-1
            head=nxt
    elif line[6]=='L':
        if head==tail:
            dll=[]
            head=-1
            tail=-1
        else:
            prv=dll[tail][1]
            if prv>=0:  dll[prv][2]=-1
            if tail==head: head=-1
            tail=prv

    elif line[0]=='i': # 'insert':                                              
        x = int(line[7:])
        dll.append([x, -1, head])
        llen = len(dll)-1
        if head==-1:   tail=llen
        else:            dll[head][1]=llen
        head=llen
    elif line[0]=='d': # 'delete':                                              
        x = int(line[7:])
        cur=head
        while cur>=0:
            if dll[cur][0]==x:
                nxt=dll[cur][2]
                prv=dll[cur][1]
                dll[nxt][1]=prv
                dll[prv][2]=nxt
                if nxt==-1: tail=prv
                if prv==-1: head=nxt
                break
            cur=dll[cur][2]

print(dll[head][0], end='')
cur = dll[head][2]
while cur>=0:
    print('',dll[cur][0], end='')
    cur=dll[cur][2]
print()

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:
#95
#ALDS1_3_C
from collections import deque
n=int(input())
a = deque()

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

Submission status: Accepted

Solution code:
from collections import deque
import sys

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

for i in range(n):
    cmd = sys.stdin.readline().strip().split()
    if cmd[0] == "deleteFirst":
        deq.popleft()
    elif cmd[0] == "deleteLast":
        deq.pop()
    else:
        x = int(cmd[1])
        if cmd[0] == "insert":
            deq.appendleft(x)
        elif cmd[0] == "delete":
            if x not in deq: continue
            deq.remove(x)
print(*deq)

Submission status: Accepted

Solution code:
from collections import deque
import sys

class doubly_linked_list():
    def __init__(self):
        #self.linked_list = []
        self.linked_list = deque([])
        #self.length = 0
    
    def insert(self, x):
        #self.linked_list.insert(0,x)
        self.linked_list.appendleft(x)
        #self.length += 1
    
    def delete(self, x):
        # 削除するのは、最初の要素だけでよい
        if x in self.linked_list:
            self.linked_list.remove(x)
            #self.length -= 1
    
    def delete_first(self):
        #if len(self.linked_list) != 0:
        #if self.length != 0:
            #self.linked_list.pop(0)
        self.linked_list.popleft()
            #self.length -= 1
    
    def delete_last(self):
        #if len(self.linked_list) != 0:
        #if self.length != 0:
            #self.linked_list.pop(self.length-1)
        self.linked_list.pop()
            #self.length -= 1

def get_method_and_run(cls, myinput):
    # 入力の受付
    #tmp = input()
    tmp = myinput().rstrip('\n')
    if tmp.find(" ") == -1:
        method = tmp
    else:
        method, x = tmp.split(" ")
    
    # 入力に従い、処理を実行
    if method == "insert":
        cls.insert(x)
    elif method == "delete":
        cls.delete(x)
    elif method == "deleteFirst":
        cls.delete_first()
    elif method == "deleteLast":
        cls.delete_last()
    else:
        pass

if __name__ == "__main__":
    myinput = sys.stdin.readline
    #n = int(input())
    n = int(myinput().rstrip('\n'))
    dll = doubly_linked_list()  
    [ get_method_and_run(dll, myinput) for i in range(n)]
    print(" ".join(dll.linked_list))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque


def doublylink(n):
    l = deque([])
    for _ in range(n):
        f = input().split()

        if f[0] == 'insert':
            l.appendleft(f[1])
        elif f[0] == 'delete':
            if f[1] in l:
                l.remove(f[1])
        elif f[0] == 'deleteFirst':
            l.popleft()
        elif f[0] == 'deleteLast':
            l.pop()

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


if __name__ == '__main__':
    N = int(input())
    doublylink(N)

Submission status: Accepted

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

from sys import stdin
from collections import deque

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

    for _ in range(n):
        line = stdin.readline().split()
        if line[0] == "insert":
            dll.appendleft(int(line[1]))
        elif line[0] == "delete":
            try:
                dll.remove(int(line[1]))
            except ValueError:
                None
        elif line[0] == "deleteFirst":
            dll.popleft()
        elif line[0] == "deleteLast":
            dll.pop()

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

main()

Submission status: Accepted

Solution code:
from collections import deque
from sys import stdin

def read_and_print_DLList(lst, n):
    for _ in range(n):
        cmd = stdin.readline().strip().split()
        if cmd[0] == 'insert':
            lst.appendleft(cmd[1])
        elif cmd[0] == 'delete':
            if cmd[1] in lst:
                lst.remove(cmd[1])
        elif cmd[0] == 'deleteFirst':
            lst.popleft()
        elif cmd[0] == 'deleteLast':
            lst.pop()
    print(*lst, sep=" ")
    
n = int(input())

lst = deque([])

read_and_print_DLList(lst, n)

Submission status: Accepted

Solution code:
from collections import deque

q = deque()
for _ in range(int(input())):
    command = input().split()
    if command[0] == "insert":
        q.appendleft(command[1])
    elif command[0] == "delete":
        try:
            q.remove(command[1])
        except ValueError:
            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 #リスト風コンテナ

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

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

print(" ".join(a))

Submission status: Accepted

Solution code:
# coding:utf-8
from collections import deque
d = deque()
for i in range(int(input())):
    str = input().split()
    if str[0] == "insert":
        d.appendleft(str[1])
    elif str[0] == "delete":
        if str[1] in d:
            d.remove(str[1])
    elif str[0] == "deleteFirst":
        d.popleft()
    elif str[0] == "deleteLast":
        d.pop()
print(*d)

Submission status: Runtime Error

Solution code:
#coding:utf-8
dic = {}
nil = -1

class Doubly:
    def __init__(self,key,l=nil,r=nil):
        self.l = l
        self.r = r
        self.key = key

    def Insert(self,key):
        obj = Doubly(key)
        obj.l = fake
        fakeR = fake.r
        fake.r = obj
        obj.r = fakeR
        fakeR.l = obj
        dic[key] = obj

    def deleteFirst(self):
        fakeR = fake.r
        fake.r = fakeR.r
        fakeR.r.l = fake
        key = fakeR.key
        del dic[key]

    def deleteLast(self):
        fakeL = fake.l
        fake.l = fakeL.l
        fakeL.l.r = fake
        key = fakeL.key
        del dic[key]
        

    def Delete(self,target):
        obj = dic[target]
        objL = obj.l
        objR = obj.r
        objL.r = objR
        objR.l = objL
        del dic[target]
    


n = int(input())

fake = Doubly(nil)
fake.l = fake
fake.r = fake


for i in range(n):
    data = list(input().split())
    order = data[0]
    if order == "insert":
        Doubly.Insert(Doubly, int(data[1]))
    elif order == "delete":
        Doubly.Delete(Doubly, int(data[1]))
    elif order == "deleteFirst":
        Doubly.deleteFirst(Doubly)
    else:
        Doubly.deleteLast(Doubly)

obj = fake.r
List = []
while obj.key != -1:
    List.append(str(obj.key))
    obj = obj.r

print(" ".join(List))

Submission status: Runtime Error

Solution code:
#coding:utf-8
dic = {}
nil = -1

class Doubly:
    def __init__(self,key,l=nil,r=nil):
        self.l = l
        self.r = r
        self.key = key

    def Insert(self,key):
        obj = Doubly(key)
        obj.l = fake
        fakeR = fake.r
        fake.r = obj
        obj.r = fakeR
        fakeR.l = obj
        if key in dic.keys():
            dic[key].append(obj)
        else:
            dic[key] = [obj]
    def deleteFirst(self):
        fakeR = fake.r
        fake.r = fakeR.r
        fakeR.r.l = fake
        key = fakeR.key
        dic[key].pop()

    def deleteLast(self):
        fakeL = fake.l
        fake.l = fakeL.l
        fakeL.l.r = fake
        key = fakeL.key
        dic[key].pop()
        

    def Delete(self,target):
        if target in dic.keys():
            obj = dic[target].pop()
            objL = obj.l
            objR = obj.r
            objL.r = objR
            objR.l = objL
        


n = int(input())

fake = Doubly(nil)
fake.l = fake
fake.r = fake


for i in range(n):
    data = list(input().split())
    order = data[0]
    if order == "insert":
        Doubly.Insert(Doubly, int(data[1]))
    elif order == "delete":
        Doubly.Delete(Doubly, int(data[1]))
    elif order == "deleteFirst":
        Doubly.deleteFirst(Doubly)
    else:
        Doubly.deleteLast(Doubly)

obj = fake.r
List = []
while obj.key != -1:
    List.append(str(obj.key))
    obj = obj.r

print(" ".join(List))

Submission status: Runtime Error

Solution code:
list = []

count = int(input())

for ver in range(count):
	try:
		conditions,num = input().split()
	except ValueError:
		print(conditions) 
	if(conditions == "insert"):
		list.append(num)
	elif(conditions == "delete"):
		list.remove(num)
for ver in range(len(list)):
	print(list[ver],end=" ")

Submission status: Runtime Error

Solution code:
list = []

count = int(input())

for ver in range(count):
	try:
		conditions,num = input().split()
	except ValueError:
		print(conditions) 
	if(conditions == "insert"):
		list.append(num)
	elif(conditions == "delete"):
		list.remove(num)
for ver in range(len(list)):
	print(list[ver],end=" ")
print('/n')

Submission status: Runtime Error

Solution code:
list = []

count = int(input())

for ver in range(count):
	try:
		conditions,num = input().split()
	except ValueError:
		print(conditions) 
	if(conditions == "insert"):
		list.append(num)
	elif(conditions == "delete"):
		list.remove(num)
for ver in range(len(list)):
	print(list[ver],end=" ")
print("")

Submission status: Runtime Error

Solution code:
list = []

count = int(input())

for ver in range(count):
	try:
		conditions,num = input().split()
	except ValueError:
		print(conditions) 
	if(conditions == "insert"):
		list.append(num)
	elif(conditions == "delete"):
		list.remove(num)
	elif(conditions == "insertFirst"):
		list.insert(0,num)
	elif(conditions == "deleteLast"):
		list.pop
for ver in range(len(list)):
	print(list[ver],end=" ")
print("")

Submission status: Runtime Error

Solution code:
list = []

count = int(input())

for ver in range(count):
	try:
		conditions,num = input().split()
	except ValueError:
		print(conditions) 
	if(conditions == "insert"):
		list.append(num)
	elif(conditions == "delete"):
		list.remove(num)
	elif(conditions == "deleteFirst"):
		del list[0]
	elif(conditions == "deleteLast"):
		list.pop
for ver in range(len(list)):
	print(list[ver],end=" ")
print("")

Submission status: Runtime Error

Solution code:
from collections import deque

if __name__ == '__main__':
    n = int(input())
    dll = deque()
    for _ in range(n):
        command = input().split()
        if command[0] == 'insert':
            dll.appendleft(command[1])
        elif command[0] == 'delete':
            dll.remove(command[1])
        elif command[0] == 'deleteFirst':
            dll.popleft()
        elif command[0] == 'deleteLast':
            dll.pop()
    print(*dll)

Submission status: Runtime Error

Solution code:
n=int(input())
list=[]
for i in range(n):
    A=input().split()
    if A[0]=="insert":
        a=int(A[1])
        list.insert(0,a)
    elif A[0]=="delete":
        a=int(A[1])
        list.remove(a)
    else:
        if A[0]=="deleteFirst":
            list.pop(0)
        elif A[0]=="deleteLast":
            n=len(list)
            list.pop(n)
for i in range(len(list)):
    print(list[i],end="")

Submission status: Runtime Error

Solution code:
n=int(input())
list=[]
for i in range(n):
    A=input().split()
    if A[0]=="insert":
        a=int(A[1])
        list.insert(0,a)
    elif A[0]=="delete":
        a=int(A[1])
        list.remove(a)
    else:
        if A[0]=="deleteFirst":
            list.pop(0)
        elif A[0]=="deleteLast":
            n=len(list)
            list.pop(n-1)
for i in range(len(list)-1):
    print(list[i],end=" ")
print(list[len(list)-1])

Submission status: Runtime Error

Solution code:
class DoublyLinkedListNode:
    def __init__(self, num):
        self.num  = num
        self.next = None
        self.prev = None

    def setNext(self, node):
        self.next = node

    def setPrev(self, node):
        self.prev = node

    def getNum(self):
        return self.num

    def getNext(self):
        return self.next

    def getPrev(self):
        return self.prev

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

    def insert(self, x):
        node = DoublyLinkedListNode(x)

        if self.first == None:
            self.first = node
            self.last  = node
            return

        self.first.setPrev(node)
        node.setNext(self.first)

        self.first = node

    def delete(self, x):
        node = self.first

        while node != None:
            if node.getNum() == x:
                if id(node) == id(self.first):
                    self.deleteFirst()
                    break

                if id(node) == id(self.last):
                    self.deleteLast()
                    break

                pnode = node.getPrev()
                nnode = node.getNext()

                if pnode != None:
                    pnode.setNext(nnode)
                if nnode != None:
                    nnode.setPrev(pnode)

                break

            node = node.getNext()

    def deleteFirst(self):
        node = self.first.getNext()
        node.setPrev(None)
        self.first = node

    def deleteLast(self):
        node = self.last.getPrev()
        node.setNext(None)
        self.last = node

    def __iter__(self):
        self._it = self.first
        return self

    def __next__(self):
        node = self._it
        if node == None:
            raise StopIteration

        self._it = self._it.getNext()
        return node.getNum()

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

    lst = DoublyLinkedList()

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

        if 'insert' == cmds[0]:
            lst.insert(int(cmds[1]))
        if 'delete' == cmds[0]:
            lst.delete(int(cmds[1]))
        if 'deleteFirst' == cmds[0]:
            lst.deleteFirst()
        if 'deleteLast' == cmds[0]:
            lst.deleteLast()

    print(" ".join([str(x) for x in lst]))

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from collections import deque

# 命令数入力
n = int(input())
# 命令入力
order_list = [input().split() for i in range(n)]


# 双方向連結リスト
doubly_linked_list = deque()

for _ in range(n):
    
    #order = input().split()
    
    if order[0] == "insert":
        doubly_linked_list.appendleft(order[1])
    
    elif order[0] == "delete":
        if order[1] in doubly_linked_list:
            doubly_linked_list.remove(order[1])
    
    elif order[0] =="deleteFirst":
        doubly_linked_list.popleft()
    
    elif order[0] == "deleteLast":
        doubly_linked_list.pop()
        
# 最終結果出力
print(*doubly_linked_list)

Submission status: Runtime Error

Solution code:
from collections import deque
d = deque()

for _ in range(int(input())):
    s = input()
    try:
        com, val = s.split()
        val = int(val)
    except ValueError:
        com = s
    
    if com == 'insert':
        d.appendleft(val)
    elif com == 'delete':
        d.remove(val)
    elif com == 'deleteFirst':
        d.popleft()
    elif com == 'deleteLast':
        d.pop()

print(*d)

Submission status: Runtime Error

Solution code:
from collections import deque
q = deque()
for _ in range(int(input())):
    c, *k = input().split()
    k = k[0] if k else None
    if c == 'insert':
        q.appendleft(k)
    elif c == 'delete':
        q.remove(k)
    elif c == 'deleteFirst':
        q.popleft()
    else:
        q.pop()
print(*q)

Submission status: Runtime Error

Solution code:
from collections import deque
q = deque()
for _ in range(int(input())):
    c, *k = input().split()
    k = k[0] if k else None
    if c[0] == 'i':
        q.appendleft(k)
    elif c[0] == 'd':
        if c[6] == 'F':
            q.popleft()
        elif c[6] == 'L':
            q.pop()
        else:
            if k in q:
                q.remove(k)
print(*q)

Submission status: Runtime Error

Solution code:
from collections import deque
from sys import stdin
q = deque()
input()
for s in stdin:
    c, *k = s.split()
    k = k[0] if k else None
    if c[0] == 'i':
        q.appendleft(k)
    elif c[0] == 'delete':
        if k in q:
            q.remove(k)
    if c == 'deleteFirst':
        q.popleft()
    else:
        q.pop()
print(*q)

Submission status: Runtime Error

Solution code:
n = map(int,input())
a = [input(sep=" ")[1]]
for i in range(n):
    command = input()
    if(command == "deleteFirst"):
        a.pop(0)
    elif(command == "deleteLast"):
        a.pop()
    else:
        command,num = command.split(sep = " ")
        num = int(num)
        if(command == "delete"):
            a.remove(n)
        else:
            a.append(n)

Submission status: Runtime Error

Solution code:
n = map(int,input())
a = [int(input().split(sep=" ")[1])]
for i in range(n):
    command = input()
    if(command == "deleteFirst"):
        a.pop(0)
    elif(command == "deleteLast"):
        a.pop()
    else:
        command,num = command.split(sep = " ")
        num = int(num)
        if(command == "delete"):
            a.remove(n)
        else:
            a.append(n)
for num in a:
    print(num,end=" ")
print()

Submission status: Runtime Error

Solution code:
n = map(int,input())
a = [int(input().split(sep=" ")[1])]
for i in range(n - 1):
    command = input()
    if(command == "deleteFirst"):
        a.pop(0)
    elif(command == "deleteLast"):
        a.pop()
    else:
        command,num = command.split(sep = " ")
        num = int(num)
        if(command == "delete"):
            a.remove(n)
        else:
            a.append(n)
for num in a:
    print(num,end=" ")
print()

Submission status: Runtime Error

Solution code:
n = int(input())
a = [int(input().split(sep=" ")[1])]
for i in range(n - 1):
    command = input()
    if(command == "deleteFirst"):
        a.pop(0)
    elif(command == "deleteLast"):
        a.pop()
    else:
        command,num = command.split(sep = " ")
        num = int(num)
        if(command == "delete"):
            a.remove(num)
        else:
            a.append(num)
print(a[0],end = "")
for num in a[1:]:
    print(" ",num,sep = "",end="")
print()

Submission status: Runtime Error

Solution code:
n = int(input())
num_list = []

for _ in range(n):
    cmd = (input().split())
    if   cmd[0] == 'insert':
        num_list.insert(0,int(cmd[1]))
    elif cmd[0] == 'delete':
        idx = num_list.index(int(cmd[1]))
        del(num_list[idx])
    elif cmd[0] == 'deleteFirst':
        del(num_list[0])
    elif cmd[0] == 'deleteLast':
        num_list.pop()

ans_list = [str(i) for i in num_list]
print(' '.join(ans_list))

Submission status: Runtime Error

Solution code:
n = int(input())
num_list = []

for _ in range(n):
    cmd = (input().split())
    if   cmd[0] == 'insert':
        num_list.insert(0,cmd[1])
    elif cmd[0] == 'delete':
        if cmd[1] in num_list:
            num_list.remove(cmd[0])
    elif cmd[0] == 'deleteFirst':
        del(num_list[0])
    else:
        num_list.pop()

print(' '.join(num_list))

Submission status: Runtime Error

Solution code:

from sys import stdin
import sys
class node:
    def __init__(self,num,pre = None,nextnode = None):
        self.num = num
        self.prenode = pre
        self.nextnode = nextnode
    
class doubly_linked_list:
    import sys
    begin_node = None
    end_node = None
    
    def insert(self,num):
        if(self.end_node):
            self.end_node.nextnode = node(num,pre = self.end_node)
            self.end_node = self.end_node.nextnode
        else:
            self.begin_node = self.end_node = node(num)
    def deleteFirst(self):
        if self.begin_node is self.end_node:
            self.begin_node = self.end_node = None
        else:
            self.begin_node = self.begin_node.nextnode
            self.begin_node.prenode = None
            
    def deleteLast(self):
        if self.begin_node is self.end_node:
            self.begin_node = self.end_node = None
        else:        
            self.end_node = self.end_node.prenode
            self.end_node.nextnode = None

    def delete_num(self, target):
        it = self.begin_node
        while it is not None:
            if it.num == target:
                if it.prenode is None and it.nextnode is None:
                    self.begin_node = self.end_node = None
                else:
                    if it.prenode is not None:
                        it.prenode.nextnode = it.nextnode
                    else:
                        self.begin_node = self.begin_node.nextnode
  
                    if it.nextnode is not None:
                        it.nextnode.prenode = it.prenode
                    else:
                        self.end_node = self.end_node.prenode
                break
            it = it.nextnode
            
    def print_all(self):
        current_node = self.begin_node
        while(current_node is not self.end_node):
            print(current_node.num,end = " ",flush = True)
            current_node = current_node.nextnode
        print(self.end_node.num,flush = True)
        sys.stdout.flush()
        
n = int(input())
dll = doubly_linked_list()
dll.insert(stdin.readline().strip().split()[1])

for i in range(n - 1):
    command = stdin.readline().strip()
    if(command == "deleteFirst"):
        dll.deleteFirst()
    elif(command == "deleteLast"):
        dll.deleteLast()
    else:
        command,num = command.split(sep = " ")
        #num = int(num)
        if(command == "delete"):
            dll.delete(num)            
        else:
            dll.insert(num)
dll.print_all()