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: Wrong Answer

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
        if dic != {}:
            dic[key].pop()
            if dic[key] == []:
                del dic[key]

    def deleteLast(self):
        fakeL = fake.l
        fake.l = fakeL.l
        fakeL.l.r = fake
        key = fakeL.key
        if dic != {}:
            dic[key].pop()
            if dic[key] == []:
                del dic[key]
        

    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
            if dic[target] == []:
                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: Wrong Answer

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
        if dic != {}:
            dic[key].pop(0)
            if dic[key] == []:
                del dic[key]

    def deleteLast(self):
        fakeL = fake.l
        fake.l = fakeL.l
        fakeL.l.r = fake
        key = fakeL.key
        if dic != {}:
            dic[key].pop(0)
            if dic[key] == []:
                del dic[key]
        

    def Delete(self,target):
        if target in dic.keys():
            obj = dic[target].pop(0)
            objL = obj.l
            objR = obj.r
            objL.r = objR
            objR.l = objL
            if dic[target] == []:
                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: Wrong Answer

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])
        if a in list=="True": 
            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: Wrong Answer

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])
        if list.index(a)>0: 
            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: Wrong Answer

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)
    elif c == 'deleteFirst':
        q.popleft()
    else:
        q.pop()
print(*q)

Submission status: Wrong Answer

Solution code:
n=int(raw_input().strip())
lst=[]
for i in xrange(n):
    inp=raw_input().strip().split()
    op=inp[0]
    if len(inp)>1:
        num=int(inp[1])
    if(op=="insert"):
        lst.append(num)
    elif(op=="delete"):
        if num in lst:
            lst.remove(num)
    elif(op=="deleteFirst"):
        lst.remove(lst[0])
    elif(op=="deleteLast"):
        lst.remove(lst[-1])
print " ".join(str(pp) for pp in lst)

Submission status: Wrong Answer

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(n)
        else:
            a.append(n)
for num in a:
    print(num,end=" ")
print()

Submission status: Wrong Answer

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"):
            if num in a:
                a.remove(num)            
        else:
            a.append(num)
print(a[0],end = "")
for num in a[1:]:
    print(" ",num,sep = "",end="")
print()

Submission status: Wrong Answer

Solution code:
class Node(object):

    def __init__(self, _pre, _next, value):
        self._pre = _pre
        self._next = _next
        self._value = value

    @property
    def pre(self):
        return self._pre

    @pre.setter
    def pre(self, _pre):
        self._pre = _pre

    @property
    def next(self):
        return self._next

    @next.setter
    def next(self, _next):
        self._next = _next

    @property
    def value(self):
        return self._value

    @value.setter
    def value(self, _value):
        self._value = _value


class DoublyLinkedList(object):

    def __init__(self):
        self.dummy = Node(None, None, None)
        self.dummy.pre = self.dummy
        self.dummy.next = self.dummy

    def first(self):
        '''return first node of list'''
        return self.dummy.next

    def last(self):
        '''return last node of list'''
        return self.dummy.pre

    def end(self):
        return self.dummy

    def insert_after(self, value, node):
        '''考え方として、Valueをうけとって、クラス内でノードを自己で作成する。'''
        new_node = Node(node.pre, node, value)
        node.pre.next = new_node
        node.pre = new_node

    def insert(self, value):
        self.insert_after(value, self.first())

    def search(self, key):
        n = self.first()
        while n != self.end() and n.value != key:
            n = n.next
        return n

    def print_all(self):
        n = self.first()
        result = []
        while n != self.end():
            result.append(str(n.value))
            n = n.next
        return " ".join(result)


    def delete(self, key, n=None):
        '''ポインターの結合を外すことで削除とする。'''
        if n is None: n = self.search(key)
        n.pre.next = n.next
        n.next.pre = n.pre

    def deleteFirst(self):
        self.delete(None, self.first())

    def deleteLast(self):
        self.delete(None, self.last())


_list = DoublyLinkedList()

n = input()

for _ in range(int(n)):
    com, *num = input().split()
    if num:
        eval("_list.{}({})".format(com, *num))
    else:
        if com == "deleteFirst":
            _list.deleteLast()
        else:
            _list.deleteLast()


print(_list.print_all())

Submission status: Wrong Answer

Solution code:

class Node(object):

    def __init__(self, _pre, _next, value):
        self._pre = _pre
        self._next = _next
        self._value = value

    @property
    def pre(self):
        return self._pre

    @pre.setter
    def pre(self, _pre):
        self._pre = _pre

    @property
    def next(self):
        return self._next

    @next.setter
    def next(self, _next):
        self._next = _next

    @property
    def value(self):
        return self._value

    @value.setter
    def value(self, _value):
        self._value = _value


class DoublyLinkedList(object):

    def __init__(self):
        self.dummy = Node(None, None, None)
        self.dummy.pre = self.dummy
        self.dummy.next = self.dummy

    def first(self):
        '''return first node of list'''
        return self.dummy.next

    def last(self):
        '''return last node of list'''
        return self.dummy.pre

    def end(self):
        return self.dummy

    def insert_after(self, value, node):
        '''考え方として、Valueをうけとって、クラス内でノードを自己で作成する。'''
        new_node = Node(node.pre, node, value)
        node.pre.next = new_node
        node.pre = new_node

    def insert(self, value):
        self.insert_after(value, self.first())

    def search(self, key):
        n = self.first()
        while n != self.end() and n.value != key:
            n = n.next
        return n

    def print_all(self):
        n = self.first()
        result = []
        while n != self.end():
            result.append(str(n.value))
            n = n.next
        return " ".join(result)


    def delete(self, key, n=None):
        '''ポインターの結合を外すことで削除とする。'''
        if n is None: n = self.search(key)
        n.pre.next = n.next
        n.next.pre = n.pre

    def deleteFirst(self):
        self.delete(None, self.first())

    def deleteLast(self):
        self.delete(None, self.last())


_list = DoublyLinkedList()

n = input()

for _ in range(int(n)):
    com, num = None, None
    com, *num = input().split()

    if num:
        if com == "insert":
            _list.insert(num)
        else:
            _list.delete(num)
    else:
        if com == "deleteFirst":
            _list.deleteFirst()
        else:
            _list.deleteLast()



print(_list.print_all())

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
A = [list(map(str,input().split())) for _ in range(n)]
B = []

for i in range(n):
    if A[i][0] == 'insert':
        B.insert(0,int(A[i][1]))
    elif A[i][0] == 'delete':
        if A[i][1] in B:
            B.remove(int(A[i][1]))
    elif A[i][0] == 'deleteFirst':
        del B[0]
    elif A[i][0] == 'deleteLast':
        del B[-1]
  

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

Submission status: Wrong Answer

Solution code:
from collections import deque

n = int(input())
d = deque()
for _i in range(n):
    line = input().split()
    order = line[0]
    if order in ('insert', 'delete'):
        key = int(line[1])
    if order == 'insert':
        d.appendleft(key)
    elif order == 'delete':
        d.remove(key)
    elif order == 'deleteFirst':
        d.popleft()
    elif order == 'deleteLast':
        d.pop()
    else:
        raise ValueError('Invalid order: {order}')

i = 0
while True:
    try:
        if i == 0:
            print(d.popleft())
        else:
            print(f' {d.popleft()}')
        i += 1
    except IndexError:
        break
# print()

Submission status: Wrong Answer

Solution code:
import time
n = int(input())
cmds = [input().split(' ') for _ in range(n)]

dl_list = []
insert = dl_list.insert
remove = dl_list.remove
for cmd in cmds:
    start_time = time.time()
    try:
        if cmd[0] == 'insert':
            insert(0, int(cmd[1]))
        elif cmd[0] == 'delete':
            remove(int(cmd[1]))
        elif cmd[0] == 'deleteFirst':
            del dl_list[0]
        elif cmd[0] == 'deleteLast':
            del dl_list[-1]
    except ValueError:
        continue
    print(time.time()-start_time)
print(' '.join(map(str, dl_list)))

Submission status: Wrong Answer

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

n = int(input())
cmds = [input().split(' ') for _ in range(n)]

dl_list = []
insert = dl_list.insert
remove = dl_list.remove
for cmd in cmds:
    if cmd[0] == 'insert':
        insert(0, int(cmd[1]))
    elif cmd[0] == 'delete':
        if int(cmd[1]) in dl_list:
            remove(int(cmd[1]))
    elif cmd[0] == 'deleteFirst':
        del dl_list[0]
    elif cmd[0] == 'deleteLast':
        del dl_list[-1]
print(' '.join(map(str, dl_list)))

Submission status: Wrong Answer

Solution code:
class Node:
    def __init__(self, d, next):
        self.d = d
        self.next = next

    def __str__(self):
        if self.next is None:
            return "(" + self.d + ")"
        else:
            return "(" + self.d + "," + str(self.next) + ")"


n = int(input())
node = None
for i in range(n):
    s = input()
    # print(s)
    if s == "deleteFirst":
        node = node.next
    elif s == "deleteLast":
        if node.next is None:
            node = None
        n = node
        while n.next.next is not None:
            n = n.next
        n.next = None
    else:
        c, d = s.split()
        if c == 'insert':
            n = Node(d, node)
            n.next = node
            node = n
        elif c == "delete":
            n = node
            while n.next is not None:
                if n.next.d == d:
                    n.next = n.next.next
                    break
                n = n.next

while node.next is not None:
    d = node.d
    print(d, "", end="")
    node = node.next
print(node.d)

Submission status: Wrong Answer

Solution code:
class Node():
    def __init__(self,value,prv,nxt):
        self.value = value
        self.prv = prv
        self.nxt = nxt

class LinkedList():

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

    def insert(self,value):
        if self.head == None:
            self.head = Node(value,None,None)
            self.tail = self.head
        else:
            node = Node(value,None,self.head)
            if self.head.nxt == self.tail: #when only one node exists.
                self.head.prv = node
                self.head = node
            else:
                self.head.prv = node
                self.head = node

    def delete(self,value):
        check_node = self.head
        while True:
            if check_node.value == value:
                if check_node.nxt == None:
                    self.deleteLast()
                else:
                    check_node.prv.nxt = check_node.nxt
                    check_node.nxt.prv = check_node.prv
                break
            else:
                check_node = check_node.nxt

    def deleteFirst(self):
        if self.head.nxt == None:
            self.head == None
        else:
            self.head = self.head.nxt

    def deleteLast(self):
        self.tail.prv.nxt = None
        self.tail = self.tail.prv

n = int(input())
linkedList = LinkedList(None,None)

def getAnswer(linkedList):
    answer = []
    check = linkedList.head
    while True:
        answer.append(check.value)
        if check.nxt == None:
            break
        else:
            check = check.nxt

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

for _ in range(n):
    line = input()
    if not line == "deleteFirst" and not line == "deleteLast":
        command = line.split(" ")[0]
        value = int(line.split(" ")[1])
    else:
        command = line

    if command == "insert":
        linkedList.insert(value)
    elif command == "delete":
        linkedList.delete(value)
    elif command == "deleteFirst":
        linkedList.deleteFirst()
    elif command == "deleteLast":
        linkedList.deleteLast()

getAnswer(linkedList)

Submission status: Wrong Answer

Solution code:
class Node():
    def __init__(self,value,prv,nxt):
        self.value = value
        self.prv = prv
        self.nxt = nxt

class LinkedList():

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

    def insert(self,value):
        if self.head == None:
            self.head = Node(value,None,None)
            self.tail = self.head
        else:
            node = Node(value,None,self.head)
            if self.head.nxt == self.tail: #when only one node exists.
                self.head.prv = node
                self.head = node
            else:
                self.head.prv = node
                self.head = node

    def delete(self,value):
        check_node = self.head
        while not check_node == None:
            if check_node.value == value:
                if check_node == self.tail:
                    self.deleteLast()
                elif check_node == self.head:
                    self.head = check_node.nxt
                    check_node.nxt.prv = None
                else:
                    check_node.prv.nxt = check_node.nxt
                    check_node.nxt.prv = check_node.prv
                break
            check_node = check_node.nxt

    def deleteFirst(self):
        if self.head.nxt == None:
            self.head == None
        else:
            self.head = self.head.nxt

    def deleteLast(self):
        self.tail.prv.nxt = None
        self.tail = self.tail.prv

n = int(input())
linkedList = LinkedList(None,None)

def getAnswer(linkedList):
    answer = []
    check = linkedList.head
    while True:
        answer.append(check.value)
        if check.nxt == None:
            break
        else:
            check = check.nxt

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

for _ in range(n):
    line = input()
    #print(line)
    if not line == "deleteFirst" and not line == "deleteLast":
        command = line.split(" ")[0]
        value = int(line.split(" ")[1])
    else:
        command = line

    if command == "insert":
        linkedList.insert(value)
    elif command == "delete":
        linkedList.delete(value)
    elif command == "deleteFirst":
        linkedList.deleteFirst()
    elif command == "deleteLast":
        linkedList.deleteLast()

    #getAnswer(linkedList)

getAnswer(linkedList)

Submission status: Wrong Answer

Solution code:
import time

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

class MyDLL:
    def __init__(self):
        self.linkedlist = []
        self.length = 0
    
    def isEmpty(self):
        if self.length==0:
            return True
        return False
    
    def insert(self, x):
        # 連結リストの先頭にキー x を持つ要素を継ぎ足す。
        if self.isEmpty():
            # 連結リストが空の時
            new = Node(x, None, None)
            self.linkedlist.insert(0, new)         
        else:
            # 連結リストが空ではない時
            old = self.linkedlist[0] # 今まで先頭にいたノード
            new = Node(x, None, old) # 新しく先頭になるノード
            old.prev = new           # 今まで先頭にいたノードの前に新しいノードを登録
            self.linkedlist.insert(0, new)
        self.length += 1 # 連結リストの長さを更新

    def delete(self, x):
        # キー x を持つ最初の要素を連結リストから削除する。そのような要素が存在しない場合は何もしない。
        for node in self.linkedlist:
            if node.key==x:# キー x を持つ最初の要素    
                if node.prev is None:# 先頭だった時
                    node.next.prev = None # 次の要素の前の要素をNoneにする
                elif node.next is None:# 最後尾だった時
                    node.prev.next = None
                else:# 途中だった時
                    prev_node = node.prev
                    next_node = node.next
                    prev_node.next = next_node
                    next_node.prev = prev_node
                self.linkedlist.remove(node)# listから削除      
                break # キーxを持つ最初だけで良いのでbreak
        
    def deleteFirst(self):
        # リストの先頭の要素を削除する。
        if not self.isEmpty():
            first = self.linkedlist.pop(1)
            if self.length>1:
                first.next.prev = None
        else:
            print("List is empty")
        
    def deleteLast(self):
        # リストの末尾の要素を削除する。        
        if not self.isEmpty():
            last = self.linkedlist.pop()
            if self.length>1:
                last.prev.next = None
        else:
            print("List is empty")
        


def main():
    n = int(input())
    commands = [input().split() for i in range(n)]
    commands = [{'order': c[0], 'value': c[1]} if len(c)==2 else {'order': c} for c in commands]
    dll = MyDLL()
    for c in commands:        
        if c['order']=='insert':
            dll.insert(c['value'])
        elif c['order']=='delete':
            dll.delete(c['value'])
        elif c['order']=='deleteFirst':
            dll.deleteFirst()
        elif c['order']=='deleteLast':
            dll.deleteLast()
    print(' '.join([node.key for node in dll.linkedlist]))

if __name__=='__main__':
    # start = time.time()
    main()
    # elapsed_time = time.time() - start
    # print ("elapsed_time:{0}".format(elapsed_time) + "[sec]")

Submission status: Wrong Answer

Solution code:
import math
import sys

class Node():
    def __init__(self, x):
        self.value = x
        self.left = None
        self.right = None

class Dubbly_linked_list():
    def __init__(self):
        # null を作る
        self.nil = Node(None)
        self.nil.left = self.nil
        self.nil.right = self.nil
    
    def insert(self, x):
        # 先頭にxをkeyとしたノードを挿入
        node = Node(x)
        node.right = self.nil.right
        self.nil.right.left = node
        node.left = self.nil
        self.nil.right = node

    def __search_x(self, x):
        # xを持つ最初のノードを返す
        cursor = self.nil.right
        while(cursor != self.nil):
            if cursor.value == x:
                return cursor
            else:
                cursor = cursor.right
        return None
    
    def delete(self, x):
        # xをkeyとした最初のノードの削除
        delNode = self.__search_x(x)
        if delNode is None:
            print("error: There's no value having {}".format(x))
            sys.exit()
        else:
            delNode.right.left = delNode.left
            delNode.left.right = delNode.right

    def deleteFirst(self):
        # 先頭のノードを削除
        if self.nil.right == self.nil:
            print("error: only nil has existed")
            sys.exit()
        else:
            delNode = self.nil.right
            delNode.right.left = delNode.left
            delNode.left.right = delNode.right
    def deleteLast(self):
        if self.nil.left == self.nil:
            print("error: only nil has existed")
            sys.exit()
        else:
            delNode = self.nil.left
            delNode.right.left = delNode.left
            delNode.left.right = delNode.right
    
    def print(self):
        # 全ての要素をprintする
        cursor = self.nil.right 
        while(cursor != self.nil):
            print(cursor.value, end="")
            cursor = cursor.right
            if cursor != self.nil:
                print(end=" ")
        print()


def main():
    DLL = Dubbly_linked_list()
    n = int(input())
    for _ in range(n):
        command = input()
        if command[6] == "F":
            DLL.deleteFirst()
        elif command[6] == 'L':
            DLL.deleteLast()
        else:
            command, x = command.split()
            x = int(x)
            if command[0] == "i":
                DLL.insert(x)
            else:
                DLL.delete(x)
 
    DLL.print()




if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
commands = [input().split() for _ in range(n)]

l = []
for com in commands:
    print(l)
    if com[0]=='insert':
        l.insert(0, com[1])
    elif com[0]=='delete':
        try:
            l.remove(com[1])
        except:
            pass
    elif com[0]=='deleteFirst':
        l.pop(0)
    elif com[0]=='deleteLast':
        l.pop()
s = ' '.join(l)
print(s)

Submission status: Wrong Answer

Solution code:
from collections import deque

n = int(input())
commands = [input() for _ in range(n)] # splitとかしてると間に合わない

l = deque()
for com in commands:
    if com[0]=='i':
        l.appendleft(com[6:-1])
    elif com[6]=='F':
        l.popleft()
    elif com[6]=='L':
        l.pop()
    else:
        try:
            l.remove(com[7:-1])
        except:
            pass

s = ' '.join(l)
print(s)

Submission status: Wrong Answer

Solution code:
class Node:
    def __init__(self, key):
        self.key = key
        self.prev = None
        self.next = None
    

class MyDLL:
    def __init__(self):
        # 番兵：仮想的な先頭
        self.sentinel = Node(None)
        self.sentinel.prev = self.sentinel
        self.sentinel.next = self.sentinel
    
    def insert(self, x):
        # 連結リストの先頭にキー x を持つ要素を継ぎ足す。
        new = Node(x)
        new.next = self.sentinel.next # 新しく先頭.next = 番兵.next = 今まで先頭
        self.sentinel.next.prev = new # 番兵.next.prev = 今まで先頭.prev = 新しく先頭
        self.sentinel.next = new      # 番兵.next = 新しく先頭
        new.prev = self.sentinel      # 新しく先頭.prev = 番兵

    def listSearch(self, x):
        cursor = self.sentinel.next # 番兵の次の要素から巡る
        while (cursor != self.sentinel) & (cursor.key != x):
            cursor = cursor.next
        return cursor
    
    def deleteNode(self, node):
        # あるノードを削除するとき
        # 例 A - B - C でBを削除
        if node!=self.sentinel: # Bは番兵ではないことを確認
            node.prev.next = node.next # B.prev.next = A.next = B.next = C
            node.next.prev = node.prev # B.next.prev = C.prev = B.prev = A

    def deleteKey(self, x):
        self.deleteNode(self.listSearch(x))
        
    def deleteFirst(self):
        # リストの先頭の要素を削除する。
        self.deleteNode(self.sentinel.next)
        
    def deleteLast(self):
        # リストの末尾の要素を削除する。        
        self.deleteNode(self.sentinel.prev)
        
    def print(self):
        cursor = self.sentinel.next
        count = 0
        while cursor != self.sentinel:
            if count==0:
                print(cursor.key)
            else:
                print(' '+ cursor.key)
            count +=1
            cursor = cursor.next   


def main():
    n = int(input())
    commands = (input() for i in range(n)) # generator式リスト内包表記
    dll = MyDLL()
    for c in commands:
        if 'insert' in c:
            dll.insert(c.split()[1])
        elif 'First' in c:
            dll.deleteFirst()
        elif 'Last' in c:
            dll.deleteLast()
        elif 'delete' in c:
            dll.deleteKey(c.split()[1])
        else:
            print("the command dont't exist")
            print(c)
    dll.print()

main()

Submission status: Wrong Answer

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

for _ in range(n):
  op = list(map(str,input().split()))
  if(op[0] == "insert"):
    dList.append(op[1])
  elif(op[0] == "delete"):
    for j,k in enumerate(dList):
      if(op[1] == k):
        dList.pop(j)
  elif(op[0] == "deleteFirst"):
    dList.pop(0)
  elif(op[0] == "deleteLast") :
    dList.pop(len(dList)-1)

print(*dList)