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: Runtime Error

Solution code:
#http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_3_C&lang=jp
#???????????£????????????
#??¬?????????????????¬????????§????????¨???????????????????????????dummy????????????????????¨??§???????????????????????????????????§??????
def dll_processor(operations):
    
    front = None
    end = None
    
    for o in operations:
        if o[0] == "insert":
            front, end = insert(front, end,{ "next":None, "prev":None, "data":o[1] })
        elif o[0] == "delete":
            front, end = delete(front, end, o[1])
        elif o[0] == "deleteFirst":
            front, end = delete_first(front, end)
        elif o[0] == "deleteLast":
            front, end = delete_last(front, end)
        #print(get_list(front))
        
    return get_list(front)

def get_list(front):
    if not front:
        return []
    
    l = []
    target = front
    while True:
        l.append(target["data"])
        if not target["next"]:
            break
        target = target["next"]
    return l

def insert(front, end, target):

    if front:
        front["prev"] = target
        target["next"] = front
        return target, end
    else:
        return target, target
    
def delete(front, end,target):

    delete_node = front
    while not delete_node["data"] == target:
        delete_node = delete_node["next"]
        if delete_node == None:
            return front, end

    if delete_node["prev"] == None:
        delete_node["next"]["prev"] = None
        tmp = delete_node["next"]
        del delte_node
        return tmp, end
    elif delete_node["next"] == None:
        delete_node["prev"]["next"] = None
        tmp = delete_node["prev"]
        del delete_node
        return front, tmp
    else:
        delete_node["next"]["prev"] = delete_node["prev"]
        delete_node["prev"]["next"] = delete_node["next"]
        del delete_node
        return front, end
    
def delete_last(front, end):
    
    if not end["prev"]:
        del front, end
        return None, None
    else:
        end["prev"]["next"] = None
        tmp = end["prev"]
        del end
        return front, tmp
    
def delete_first(front, end):
    
    if not front["next"]:
        del front, end
        return None, None
    else:
        front["next"]["prev"] = None
        tmp = front["next"]
        del front
        return tmp, end

def main():
    n_list = int(input())
    target_list = [input().split() for i in range(n_list)]
    print(*dll_processor(target_list))
    
if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
#http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_3_C&lang=jp
#???????????£????????????

NEXT = 2
DATA = 1
PREV = 0
def dll_processor(operations):
    
    front = None
    end = None
    
    for i,o in enumerate(operations):
        if o[0] == "insert":
            front, end = insert(front, end, tuple(None, int(o[1]), None))
        elif o[0] == "delete":
            front, end = delete(front, end, int(o[1]))
        elif o[0] == "deleteFirst":
            front, end = delete_first(front, end)
        elif o[0] == "deleteLast":
            front, end = delete_last(front, end)
        #print(get_list(front))
        
    return get_list(front)

def get_list(front):
    if not front:
        return []
    
    l = []
    target = front
    while True:
        l.append(target[DATA])
        if not target[NEXT]:
            break
        target = target[NEXT]
    return l

def insert(front, end, target):

    if front:
        front[PREV] = target
        target[NEXT] = front
        return target, end
    else:
        return target, target
    
def delete(front, end,target):

    delete_node = front
    while not delete_node[DATA] == target:
        delete_node = delete_node[NEXT]
        if delete_node == None:
            return front, end

    if delete_node[PREV] == None:
        delete_node[NEXT][PREV] = None
        return delete_node[NEXT], end
    elif delete_node[NEXT] == None:
        delete_node[PREV][NEXT] = None
        return front, delete_node[PREV]
    else:
        delete_node[NEXT][PREV] = delete_node[PREV]
        delete_node[PREV][NEXT] = delete_node[NEXT]
        return front, end
    
def delete_last(front, end):
    
    if not end[PREV]:
        return None, None
    else:
        end[PREV][NEXT] = None
        return front, end[PREV]
    
def delete_first(front, end):
    
    if not front[NEXT]:
        return None, None
    else:
        front[NEXT][PREV] = None
        return front[NEXT], end

def main():
    n_list = int(input())
    target_list = [input().split() for i in range(n_list)]
    print(*dll_processor(target_list))
    
if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
#http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_3_C&lang=jp
#???????????£????????????

NEXT = 2
DATA = 1
PREV = 0
def dll_processor(operations):
    
    front = None
    end = None
    
    for i,o in enumerate(operations):
        if o[0] == "insert":
            front, end = insert(front, end, tuple([None, int(o[1]), None]))
        elif o[0] == "delete":
            front, end = delete(front, end, int(o[1]))
        elif o[0] == "deleteFirst":
            front, end = delete_first(front, end)
        elif o[0] == "deleteLast":
            front, end = delete_last(front, end)
        #print(get_list(front))
        
    return get_list(front)

def get_list(front):
    if not front:
        return []
    
    l = []
    target = front
    while True:
        l.append(target[DATA])
        if not target[NEXT]:
            break
        target = target[NEXT]
    return l

def insert(front, end, target):

    if front:
        front[PREV] = target
        target[NEXT] = front
        return target, end
    else:
        return target, target
    
def delete(front, end,target):

    delete_node = front
    while not delete_node[DATA] == target:
        delete_node = delete_node[NEXT]
        if delete_node == None:
            return front, end

    if delete_node[PREV] == None:
        delete_node[NEXT][PREV] = None
        return delete_node[NEXT], end
    elif delete_node[NEXT] == None:
        delete_node[PREV][NEXT] = None
        return front, delete_node[PREV]
    else:
        delete_node[NEXT][PREV] = delete_node[PREV]
        delete_node[PREV][NEXT] = delete_node[NEXT]
        return front, end
    
def delete_last(front, end):
    
    if not end[PREV]:
        return None, None
    else:
        end[PREV][NEXT] = None
        return front, end[PREV]
    
def delete_first(front, end):
    
    if not front[NEXT]:
        return None, None
    else:
        front[NEXT][PREV] = None
        return front[NEXT], end

def main():
    n_list = int(input())
    target_list = [input().split() for i in range(n_list)]
    print(*dll_processor(target_list))
    
if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
#! /usr/bin/env python
# -*- coding: utf-8 -*-

''' ???????????£???????????? '''

from collections import deque

functions = {"insert": lambda obj, x: obj.insert(x),
             "delete": lambda obj, x: obj.delete(x),
             "deleteFirst": lambda obj: obj.deleteFirst(),
             "deleteLast": lambda obj: obj.deleteLast()}


class DoublyLinkedList(object):
    """ Doubly Linked List """

    def __init__(self):
        # self.list = []
        self.list = deque()

    def insert(self, x):
        """ ???????????????x?????????????´?????????\ """
        # self.list = [x] + self.list
        self.list.appendleft(x)

    def delete(self, x):
        """ ??????x??????????????????????´?????????? """
        # for i in range(len(self.list)):
        #     if self.list[i] == x:
        #         self.list.pop(i)
        #         break
        # if x in self.list:
        self.list.remove(x)

    def deleteFirst(self):
        """ ?????????????????????????´?????????? """
        # self.list.pop(0)
        self.list.popleft()

    def deleteLast(self):
        """ ????????????????°????????´?????????? """
        self.list.pop()


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

    dll = DoublyLinkedList()

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

        if len(command) == 1:
            functions[command[0]](dll)
        elif len(command) == 2:
            functions[command[0]](dll, int(command[1]))

    print(' '.join(map(str, dll.list)))

Submission status: Runtime Error

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

result = list()

def operation(command):
    if command[0] == "insert":
        result.insert(0, int(command[1]))
    elif command[0] == "delete":
        result.remove(int(command[1]))
    elif command[0] == "deleteFirst":
        result.pop(0)
    elif command[0] == "deleteLast":
        result.pop()

n = int(input())
for i in range(n):
    command = input().split()
    operation(command)
print(*result)

Submission status: Runtime Error

Solution code:
arr = []

for i in range(int(input())):
  command_line = input().split(" ")
  command = command_line[0]
  arg = ""
  if len(command_line) > 1: arg = command_line[1]

  if command == "insert":
    arr.insert(0, arg)
  if command == "delete":
    arr.remove(arg)
  if command == "deleteFirst":
    del arr[0]
  if command == "deleteLast":
    del arr[-1]

print(" ".join(arr))

Submission status: Runtime Error

Solution code:
from collections import deque

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

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

print(*Q)

Submission status: Runtime Error

Solution code:

from collections import deque

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

for i in range(n):
    line = input().split()
    cmd = line[0]

    if len(line) == 2:
        value = line[1]

    if cmd == "insert":
        linked_list.insert(0, value)
    elif cmd == "delete":
        linked_list.remove(value)
    elif cmd == "deleteFirst":
        linked_list.popleft()
    elif cmd == "deleteLast":
        linked_list.pop()

print(*linked_list)

Submission status: Runtime Error

Solution code:

from collections import deque

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

for i in range(n):
    line = input().split()
    cmd = line[0]

    if len(line) == 2:
        value = line[1]

    if cmd == "insert":
        linked_list.appendleft(value)
    elif cmd == "delete":
        linked_list.remove(value)
    elif cmd == "deleteFirst":
        linked_list.popleft()
    elif cmd == "deleteLast":
        linked_list.pop()

print(*linked_list)

Submission status: Runtime Error

Solution code:
# f = open('input.txt')

n = int(input())
# n = int(f.readline())
doubly_linked_list = []

for i in range(n):
    cmd = input()
    # cmd = f.readline().strip()
    if cmd == 'deleteFirst':
        doubly_linked_list.pop(0)
    elif cmd == 'deleteLast':
        doubly_linked_list.pop(-1)
    else:
        cmd, x = cmd.split()
        if cmd == 'insert':
            doubly_linked_list.append(x)
        elif cmd == 'delete':
            doubly_linked_list.remove(x)

print(' '.join(doubly_linked_list))

Submission status: Runtime Error

Solution code:
# f = open('input.txt')

n = int(input())
# n = int(f.readline())
doubly_linked_list = []

for i in range(n):
    cmd = input()
    # cmd = f.readline().strip()
    if cmd == 'deleteFirst':
        doubly_linked_list.pop(0)
    elif cmd == 'deleteLast':
        doubly_linked_list.pop(-1)
    else:
        cmd, x = cmd.split()
        if cmd == 'insert':
            doubly_linked_list.insert(0, x)
        elif cmd == 'delete':
            doubly_linked_list.remove(x)

print(' '.join(doubly_linked_list))

Submission status: Runtime Error

Solution code:
n = int(input())

l = []
for _ in range(n):
    command = input().split()
    if command[0] == "insert":
        l.append(command[1])
    elif command[0] == "delete":
        ind = l.index(command[1])
        l.pop(ind)
    elif command[0] == "deleteFirst":
        l.pop(0)
    elif command[0] == "deleteLast":
        l.pop(-1)
        
print (" ".join(l[::-1]))

Submission status: Runtime Error

Solution code:
def show(A):
    for i in range(len(A) - 1):
        print(A[i], end=" ")
    print(A[len(A) - 1])

list = []

n = int(input())

for i in range(n):
    line = input().split()
    if line[0] == 'insert':
        list.insert(0, int(line[1]))
    if line[0] == 'delete':
        list.remove(int(line[1]))
    if line[0] == 'deleteFirst':
        list.remove(list[0])
    if line[0] == 'deleteLast':
        list.pop()

show(list)

Submission status: Runtime Error

Solution code:
l = []
n = int(input())
for _ in range(n):
    c, *i = input().split()
    if c == 'insert':
        l = [*i, *l]
    elif c == 'delete':
        l.remove(*i)
    elif c == 'deleteFirst':
        del l[0]
    elif c == 'deleteLast':
        del l[len(l)-1]
    else:
        pass

Submission status: Runtime Error

Solution code:
l = []
n = int(input())
for _ in range(n):
    c, *i = input().split()
    if c == 'insert':
        l = [*i, *l]
    elif c == 'delete':
        l.remove(*i)
    elif c == 'deleteFirst':
        del l[0]
    elif c == 'deleteLast':
        del l[len(l)-1]
    else:
        pass
print(*l)

Submission status: Runtime Error

Solution code:
import sys
from collections import deque
n = int(sys.stdin.readline())
q = deque()
for i in range(n):
    c = sys.stdin.readline()[:-1]
    if c[0] == 'i':
        q.appendleft(c[7:])
    elif c[6] == ' ':
        q.remove(c[7:])
    elif c[6] == 'F':
        q.popleft()
    else:
        q.pop()
print(*q)

Submission status: Runtime Error

Solution code:
from collections import deque
order = int(input())
llist = [input().split() for _ in range(order)]
ans = deque([])
for lst in llist:
    if lst[0] == 'insert':
        ans.appendleft(lst[1])
    
    elif lst[0] == 'delete':
        ans.remove(lst[1])
   
    elif lst[0] == 'deleteFirst':
        ans.popleft()
    
    elif lst[0] == 'deleteLast':
        ans.pop()

print(*ans)

Submission status: Runtime Error

Solution code:
n = int(input())
arr = []
for i in range(n):
    c = input()
    try:
        tmp = c.split()
        com = str(tmp[0])
        num = int(tmp[1])
    except:
        com = str(c)
        num = -1

    if num != -1:
        if com == 'insert':
            arr.insert(0,num)
        if com == 'delete':
            ind = arr.index(num)
            arr.pop(ind)
    else:
        if com == 'deleteFirst':
            arr.pop(0)
        if com == 'deleteLast':
            arr.pop()

if len(arr) != 1:
    for i in range(len(arr)-1):
        print(arr[i], end = ' ')
    print(arr[-1])
else:
    print(arr[0])

Submission status: Runtime Error

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

for i in range(n):
    data_input.append(input().split())

dlList = []

for x in data_input:
    if x[0] == 'insert':
        dlList.insert(0,x[1])
    elif x[0] == 'delete':
        dlList.remove(x[1])
    elif x[0] == 'deleteFirst':
        dlList.pop(0)
    elif x[0] == 'deleteLast':
        dlList.pop()
    else:
        pass

print(' '.join(dlList))

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
def operation(command, num, arrey):
    if command == 'insert':
        arrey.insert(0,num)
        return arrey
    elif command == 'delete':
        if num in arrey:
            arrey.remove(num)
    elif command == 'deleteFirst':
        arrey.pop(0)
    elif command == 'deleteLast':
        arrey.pop(-1)


n = int(input())
list = []

for i in range(n):
    command, num = input().split()
    operation(command,num,list)

for i in range(len(list)):
    print(list[i]+' ',end='')

Submission status: Runtime Error

Solution code:
from collections import deque

def operation(command, arrey):
    if command[0] == 'insert':
        arrey.insert(0,command[1])
        return arrey
    elif command[0] == 'delete':
        if command[1] in arrey:
            arrey.remove(command[1])
    elif command[0] == 'deleteFirst':
        arrey.pop(0)
    elif command[0] == 'deleteLast':
        arrey.pop(-1)


n = int(input())
list = deque()

for i in range(n):
    command = input().split()
    operation(command,list)

for i in range(len(list)):
    print(list[i]) if i == len(list)-1 else print(list[i]+' ',end='')

Submission status: Runtime Error

Solution code:
from collections import deque

def operation(command, arrey):
    if command[0] == 'insert':
        arrey.insert(0,command[1])
        return arrey
    elif command[0] == 'delete':
        if command[1] in arrey:
            arrey.remove(command[1])
    elif command[0] == 'deleteFirst':
        arrey.popleft()
    elif command[0] == 'deleteLast':
        arrey.pop()


n = int(input())
list = deque()

for i in range(n):
    command = input().split()
    operation(command,list)

for i in range(len(list)):
    print(list[i]) if i == len(list)-1 else print(list[i]+' ',end='')

Submission status: Runtime Error

Solution code:
from collections import deque

def operation(command, arrey):
    if command[0] == 'insert':
        arrey.insert(0,command[1])
        return arrey
    elif command[0] == 'delete':
        if command[1] in arrey:
            arrey.remove(command[1])
    elif command[0] == 'deleteFirst':
        arrey.popleft()
    elif command[0] == 'deleteLast':
        arrey.pop()


n = int(input())
list = deque()

for i in range(n):
    command = input().split()
    operation(command,list)

for i in range(len(list)):
    print(' '.join(list))

Submission status: Runtime Error

Solution code:
from collections import deque

def operation(command, arrey):
    if command[0] == 'insert':
        arrey.insert(0,command[1])
        return arrey
    elif command[0] == 'delete':
        if command[1] in arrey:
            arrey.remove(command[1])
    elif command[0] == 'deleteFirst':
        arrey.popleft()
    elif command[0] == 'deleteLast':
        arrey.pop()


n = int(input())
list = deque()

for i in range(n):
    command = input().split()
    operation(command,list)

print(' '.join(list))

Submission status: Runtime Error

Solution code:
from collections import deque


def operation(command, arrey):
    if command[0] == 'insert':
        arrey.insert(0,command[1])
        return arrey
    elif command[0] == 'delete':
        if command[1] in arrey:
            arrey.remove(command[1])
    elif command[0] == 'deleteFirst':
        arrey.popleft()
    elif command[0] == 'deleteLast':
        arrey.pop()


n = int(input())
list = deque()

for i in range(n):
    command = input().split()
    operation(command,list)

print(' '.join(list))

Submission status: Runtime Error

Solution code:
from collections import deque
import sys

def operation(command, arrey):
    if command[0] == 'insert':
        arrey.insert(0,command[1])
        return arrey
    elif command[0] == 'delete':
        if command[1] in arrey:
            arrey.remove(command[1])
    elif command[0] == 'deleteFirst':
        arrey.popleft()
    elif command[0] == 'deleteLast':
        arrey.pop()


n = int(sys.stdin.readline())
list = deque()

for i in range(n):
    command = sys.stdin.readline().split()
    operation(command,list)

print(' '.join(list))

Submission status: Runtime Error

Solution code:
class Node:
    elm = None
    nxt = None
    prev = None

    def __init__(self, elm=None, prev=None, nxt=None):
        self.elm = elm
        self.prev = prev
        self.nxt = nxt


class MyDLList:
    size = 0
    head = tail = None

    def insert(self, elm):
        if self.is_empty():
            self.head = self.tail = Node(elm)
        else:
            x = Node(elm)
            x.nxt = self.head
            self.head.prev = x
            self.head = x
        self.size += 1

    def list_search(self, x):
        cur = self.head
        while cur != self.tail and cur.elm != x:
            cur = cur.nxt
        if cur.elm != x:
            cur = None
        return cur

    def delete(self, x):
        cur = self.list_search(x)
        if cur:
            if cur == self.head:
                self.delete_first()
            elif cur == self.tail:
                self.delete_last()
            else:
                cur.prev.nxt = cur.nxt
                cur.nxt.prev = cur.prev
            self.size -= 1
            return cur.elm
        

    def delete_first(self):
        cur = self.head
        if not self.is_empty():
            if self.head != self.tail:
                self.head.nxt.prev = None
                self.head = self.head.nxt
            else:
                self.head = self.tail = None
            self.size -= 1
            return cur.elm

    def delete_last(self):
        cur = self.tail
        if not self.is_empty():
            if self.head != self.tail:
                self.tail.prev.nxt = None
                self.tail = self.tail.prev
            else:
                self.tail = self.head = None
            self.size -= 1
            return cur.elm

    def is_empty(self):
        return self.size == 0

    def __str__(self):
        cur = self.head
        output = ""
        while cur.nxt:
            output += int(cur.elm), " "
            cur = cur.nxt
        output += int(cur.elm)
        return output


n = int(input())

V = [list(map(str, input().split())) for _ in range(n)]
dl = MyDLList()

for i in V:
    if i[0] == "insert":
        dl.insert(int(i[1]))
    elif i[0] == "delete":
        dl.delete(int(i[1]))
    elif i[0] == "deleteFirst":
        dl.delete_first()
    elif i[0] == "deleteLast":
        dl.delete_last()

print(dl)

Submission status: Runtime Error

Solution code:
class Node:
    elm = None
    nxt = None
    prev = None

    def __init__(self, elm=None, prev=None, nxt=None):
        self.elm = elm
        self.prev = prev
        self.nxt = nxt


class MyDLList:
    size = 0
    head = tail = None

    def insert(self, elm):
        if self.is_empty():
            self.head = self.tail = Node(elm)
        else:
            x = Node(elm)
            x.nxt = self.head
            self.head.prev = x
            self.head = x
        self.size += 1

    def list_search(self, x):
        cur = self.head
        while cur != self.tail and cur.elm != x:
            cur = cur.nxt
        if cur.elm != x:
            cur = None
        return cur

    def delete(self, x):
        cur = self.list_search(x)
        if cur:
            if cur == self.head:
                self.delete_first()
            elif cur == self.tail:
                self.delete_last()
            else:
                cur.prev.nxt = cur.nxt
                cur.nxt.prev = cur.prev
            self.size -= 1
            return cur.elm


    def delete_first(self):
        cur = self.head
        if not self.is_empty():
            if self.head != self.tail:
                self.head.nxt.prev = None
                self.head = self.head.nxt
            else:
                self.head = self.tail = None
            self.size -= 1
            return cur.elm

    def delete_last(self):
        cur = self.tail
        if not self.is_empty():
            if self.head != self.tail:
                self.tail.prev.nxt = None
                self.tail = self.tail.prev
            else:
                self.tail = self.head = None
            self.size -= 1
            return cur.elm

    def is_empty(self):
        return self.size == 0

    def __str__(self):
        cur = self.head
        output = ""
        while cur.nxt:
            output += str(cur.elm), " "
            cur = cur.nxt
        output += str(cur.elm)
        return output


n = int(input())

V = [list(map(str, input().split())) for _ in range(n)]
dl = MyDLList()

for i in V:
    if i[0] == "insert":
        dl.insert(int(i[1]))
    elif i[0] == "delete":
        dl.delete(int(i[1]))
    elif i[0] == "deleteFirst":
        dl.delete_first()
    elif i[0] == "deleteLast":
        dl.delete_last()

print(dl)

Submission status: Runtime Error

Solution code:
class Node:
    elm = None
    nxt = None
    prev = None

    def __init__(self, elm=None, prev=None, nxt=None):
        self.elm = elm
        self.prev = prev
        self.nxt = nxt


class MyDLList:
    size = 0
    head = tail = None

    def insert(self, elm):
        if self.is_empty():
            self.head = self.tail = Node(elm)
        else:
            x = Node(elm)
            x.nxt = self.head
            self.head.prev = x
            self.head = x
        self.size += 1

    def list_search(self, x):
        cur = self.head
        if cur != self.tail:
            i = 0
            while i < self.size and cur.elm != x:
                cur = cur.nxt
                i += 1
        if cur.elm != x:
            cur = None
        return cur

    def delete(self, x):
        cur = self.list_search(x)
        if cur:
            if cur == self.head:
                self.delete_first()
            elif cur == self.tail:
                self.delete_last()
            else:
                cur.prev.nxt = cur.nxt
                cur.nxt.prev = cur.prev
            self.size -= 1
            return cur.elm

    def delete_first(self):
        cur = self.head
        if self.size != 1:
            self.head.nxt.prev = None
            self.head = self.head.nxt
        else:
            self.head = self.tail = None
        self.size -= 1
        return cur.elm

    def delete_last(self):
        cur = self.tail

        if self.size != 1:
            self.tail.prev.nxt = None
            self.tail = self.tail.prev
        else:
            self.tail = self.head = None
        self.size -= 1
        return cur.elm

    def is_empty(self):
        return self.size == 0

    def __str__(self):
        cur = self.head
        output = ""
        while cur.nxt:
            output += str(cur.elm) + " "
            cur = cur.nxt
        output += str(cur.elm)
        return output


n = int(input())

dl = MyDLList()

for i in range(n):
    x = list(map(str, input().split()))
    if x[0] == "insert":
        dl.insert(int(x[1]))
    elif x[0] == "delete":
        dl.delete(int(x[1]))
    elif x[0] == "deleteFirst":
        dl.delete_first()
    elif x[0] == "deleteLast":
        dl.delete_last()

print(dl)

Submission status: Runtime Error

Solution code:
class Node:
    elm = None
    nxt = None
    prev = None

    def __init__(self, elm=None, prev=None, nxt=None):
        self.elm = elm
        self.prev = prev
        self.nxt = nxt


class MyDLList:
    size = 0
    head = tail = None
    dic = dict()

    def insert(self, elm):
        if self.is_empty():
            self.dic[elm] = Node(elm)
            self.head = self.tail = self.dic[elm]
        else:
            self.dic[elm] = Node(elm)
            self.dic[elm].nxt = self.head
            self.head.prev = self.dic[elm]
            self.head = self.dic[elm]
        self.size += 1

    def list_search(self, x):
        # cur = self.head
        # if cur != self.tail:
        #     i = 1
        #     while i < self.size and cur.elm != x:
        #         cur = cur.nxt
        #         i += 1
        # if cur.elm != x:
        #     cur = None
        return self.dic.get(x)

    def delete(self, x):
        cur = self.list_search(x)
        if cur:
            if cur == self.head:
                self.delete_first()
            elif cur == self.tail:
                self.delete_last()
            else:
                self.dic.pop(x)
                cur.prev.nxt = cur.nxt
                cur.nxt.prev = cur.prev
                self.size -= 1
            return cur.elm

    def delete_first(self):
        # ????§???§?????°????????¨??????????????????????´???????????????¨???????????????
        cur = self.head
        if self.size != 1:
            self.dic.pop(self.head.elm)
            self.head.nxt.prev = None
            self.head = self.head.nxt
        else:
            self.dic.clear()
            self.head = self.tail = None

        self.size -= 1
        return cur.elm

    def delete_last(self):
        # ????§???§?????°????????¨??????????????????????´???????????????¨???????????????
        cur = self.tail
        if self.size != 1:
            self.dic.pop(self.tail.elm)
            self.tail.prev.nxt = None
            self.tail = self.tail.prev
        else:
            self.dic.clear()
            self.tail = self.head = None
        self.size -= 1
        return cur.elm

    def is_empty(self):
        return self.size == 0

    def __str__(self):
        cur = self.head
        output = ""
        i = 1
        while i < self.size:
            output += str(cur.elm) + " "
            cur = cur.nxt
            i += 1
        output += str(cur.elm)
        return output


n = int(input())

dl = MyDLList()

for i in range(n):
    x = list(map(str, input().split()))
    if x[0] == "insert":
        dl.insert(int(x[1]))
    elif x[0] == "delete":
        dl.delete(int(x[1]))
    elif x[0] == "deleteFirst":
        dl.delete_first()
    elif x[0] == "deleteLast":
        dl.delete_last()

print(dl)

Submission status: Runtime Error

Solution code:
from collections import deque

n = int(input())
q = deque()
for i in range(n):
    x = list(map(str, input().split()))
    if x[0] == "insert":
        q.appendleft(int(x[1]))
    elif x[0] == "delete":
        try:
            q.remove(int(x[1]))
        except ValueError:
            pass
    elif x[0] == "deleteFirst":
        q.popleft()
    elif x[0] == "deleteLast":
        q.popright()