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: Time Limit Exceeded

Solution code:
import sys


class mydeque:
    class cell:
        def __init__(self,x,y = None,z = None):
            self.data = x
            self.next = y
            self.prev = z

    def __init__(self):
        head = mydeque.cell(None)
        head.next = head
        head.prev = head
        self.size = 0
        self.head = head

    def push_front(self,x):
        h = self.head
        q = h.next
        p = mydeque.cell(x,q,h)
        h.next = p
        q.prev = p
        self.size += 1

    def push_back(self,x):
        h = self.head
        p = h.prev
        q = mydeque.cell(x,h,p)
        h.prev = q
        p.next = q
        self.size += 1

    def delete_top(self):
        h = self.head
        top = h.next
        nexttop = top.next
        h.next = nexttop
        nexttop.prev = h
        del top
        self.size -= 1

    def delete_last(self):
        h = self.head
        last = h.prev
        prevlast = last.prev
        h.prev = prevlast
        prevlast.next = h
        del last
        self.size -= 1

    def delete_find(self,target):
        h = self.head
        now_data = h.next
        while now_data != self.head:
            if now_data.data == target:
                next_now = now_data.next
                prev_now = now_data.prev
                next_now.prev = prev_now
                prev_now.next = next_now
                del now_data
                self.size -= 1
                break
            now_data = now_data.next

    def allprint(self):
        h = self.head.next
        for i in range(self.size):
            if i == self.size - 1:
                print(h.data)
            else:
                print(h.data, end=" ")
                h = h.next

# with open("input.txt","r") as f:
queue = int(input())
length = 0
myd = mydeque()
for q in range(queue):
    command = [n for n in input().strip("\n").split(' ')]
    if command[0] == "insert":
        myd.push_front(command[1])
    elif command[0] == "delete":
        myd.delete_find(command[1])
    elif command[0] == "deleteFirst":
        myd.delete_top()
    else:
        myd.delete_last()
myd.allprint()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

A = []

for i in range(N):
	b= input()
	if (" " in b):
		b, c = b.split()
		c = int(c)
	
	if b == "insert":
		A.insert(0, c)
	elif b == "delete":
		if (c in A):
			A.remove(c)
	elif b == "deleteFirst":
		A.pop(0)
	elif b == "deleteLast":
		A.pop()

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

A = []

for i in range(N):
	b= input()
	if b == "deleteFirst":
		A.pop(0)
	elif b == "deleteLast":
		A.pop()
	else:
		b, c = b.split()
		c = int(c)
	
		if b == "insert":
			A.insert(0, c)
		elif b == "delete":
			if (c in A):
				A.remove(c)

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

Submission status: Time Limit Exceeded

Solution code:
class LinkedList(object):
    def __init__(self):
        self.data = []

    def insert(self, d):
        self.data.insert(0, d)

    def delete(self, d):
        if d in self.data:
            self.data.remove(d)

    def deleteFirst(self):
        self.data.pop(0)

    def deleteLast(self):
        self.data.pop(len(self.data)-1)

data = LinkedList()
for _ in range(int(input())):
    line_data = input().split()
    command = line_data[0]
    if len(line_data) == 2:
        d = int(line_data[1])

    if command == 'insert':
        data.insert(d)
    elif command == 'delete':
        data.delete(d)
    elif command == 'deleteFirst':
        data.deleteFirst()
    elif command == 'deleteLast':
        data.deleteLast()

print(" ".join(list(map(str, data.data))))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
dll =[]
for i in range(n):
    command = input().split()
    if command[0] == 'insert':
        dll.insert(0, command[1])
    if command[0] == 'delete':
        for i in range(len(dll)):
            if dll[i] == command[1]: 
                dll.pop(i)
                break
    if command[0] == 'deleteFirst':
        dll.pop(0)
    if command[0] == 'deleteLast':
        dll.pop()

print(*dll)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
dll =[]
for i in range(n):
    command = input().split()
    if command[0] == 'insert':
        dll.insert(0, command[1])
    elif command[0] == 'delete':
        for i in range(len(dll)):
            if dll[i] == command[1]: 
                dll.pop(i)
                break
    elif command[0] == 'deleteFirst':
        dll.pop(0)
    else:
        dll.pop()

print(*dll)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
dll =[]
for i in range(n):
    command = input().split()
    if command[0] == 'insert':
        dll.insert(0, command[1])
    elif command[0] == 'delete':
       # for i in range(len(dll)):
            #if dll[i] == command[1]: 
                #dll.pop(i)
                #break
        indexes = [i for i, x in enumerate(dll) if x == command[1]]
        if len(indexes) != 0:
            dll.pop(indexes[0])
    elif command[0] == 'deleteFirst':
        dll.pop(0)
    else:
        dll.pop()

print(*dll)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
dll =[]
for i in range(n):
    command = input().split()
    if command[0] == 'insert':
        dll.insert(0, command[1])
    elif command[0] == 'delete':
        if command[1] in dll:
            dll.remove(command[1])
    elif command[0] == 'deleteFirst':
        dll.pop(0)
    else:
        dll.pop()

print(*dll)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
dll =[]
for i in range(n):
    command = input().split()
    if command[0] == 'insert':
        dll.insert(0, command[1])
    elif command[0] == 'delete':
        try:
            dll.remove(command[1])
        except:
            pass
    elif command[0] == 'deleteFirst':
        dll.pop(0)
    else:
        dll.pop()

print(*dll)

Submission status: Time Limit Exceeded

Solution code:
box = []
for mak in range(int(input())) :
    com = input().split()
    if   com[0] == 'insert'      : box.insert(0, com[1])
    elif com[0] == 'deleteFirst' : del box[0]
    elif com[0] == 'deleteLast'  : del box[-1]
    else : 
        if com[1] in box :
            box.remove(com[1])
print(' '.join(box))

Submission status: Time Limit Exceeded

Solution code:
#4.4
a = int(input())
val = []

for i in range(0, a):
    tmp = list(input().split())
    if tmp[0] == 'insert':
        val.insert(0, int(tmp[1]))
    elif tmp[0] == 'delete':
        try:
            index = val.index(int(tmp[1]))
            val.pop(index)
        except:
            pass
    elif tmp[0] == 'deleteFirst':
        val.pop(0)
    elif tmp[0] == 'deleteLast':
        val.pop(-1)

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

Submission status: Time Limit Exceeded

Solution code:
class MyList(list):
    def __init__(self):
        self.__init__ = list

    def cmd(self,command):
        if command == 'deleteFirst':
            if len(self) > 0:
                self.pop(0)
        elif command == 'deleteLast':
            if len(self) > 0:
                self.pop(-1)
        else:
            ope, num = command.split()
            num = int(num)
            if ope == 'insert':
                self.insert(0,num)
            else:
                if num in self:
                    self.remove(num)


if __name__ == '__main__' :
    l = MyList()
    n = input()
    for i in range(n):
        l.cmd(raw_input())

    for i in range(len(l)-1):
        print l[i],
    print l[-1]

Submission status: Time Limit Exceeded

Solution code:
def cmd(l,command):
    if command == 'deleteFirst':
        if len(l)>0:
            l.pop(0)
    elif command == 'deleteLast':
        if len(l)>0:
            l.pop(-1)
    else:
        ope, num = command.split()
        num = int(num)
        if ope == 'insert':
            l.insert(0,num)
        else:
            if num in l:
                l.remove(num)


if __name__ == '__main__' :
    l = list()
    n = input()
    for i in range(n):
        cmd(l,raw_input())

    for i in range(len(l)-1):
        print l[i],
    print l[-1]

Submission status: Time Limit Exceeded

Solution code:
class MyList(list):
    def __init__(self):
        self.__init__ = list

    def cmd(self,command):
        if command == 'deleteFirst':
            self.pop(0)
        elif command == 'deleteLast':
            self.pop(-1)
        else:
            ope, num = command.split()
            num = int(num)
            if ope == 'insert':
                self.insert(0,num)
            else:
                if num in self:
                    self.remove(num)


if __name__ == '__main__' :
    l = MyList()
    n = input()
    for i in range(n):
        l.cmd(raw_input())

    for i in range(len(l)-1):
        print l[i],
    print l[-1]

Submission status: Time Limit Exceeded

Solution code:
def cmd(l,command):
    if command == 'deleteFirst':
        l.pop(0)
    elif command == 'deleteLast':
        l.pop(-1)
    else:
        ope, num = command.split()
        num = int(num)
        if ope == 'insert':
            l.insert(0,num)
        else:
            if num in l:
                l.remove(num)


if __name__ == '__main__' :
    l = list()
    n = input()
    for i in range(n):
        cmd(l,raw_input())

    for i in range(len(l)-1):
        print l[i],
    print l[-1]

Submission status: Time Limit Exceeded

Solution code:
class MyList(list):
    def __init__(self):
        self.__init__ = list

    def cmd(self,command):
        if command == 'deleteFirst':
            self.pop(0)
        elif command == 'deleteLast':
            self.pop(-1)
        else:
            ope, num = command.split()
            num = int(num)
            if ope == 'insert':
                self.insert(0,num)
            else:
                if num in self:
                    self.remove(num)


if __name__ == '__main__' :
    l = MyList()
    n = input()

    cmd = [raw_input() for i in range(n)]
    
    for item in cmd:
        l.cmd(item)

    for i in range(len(l)-1):
        print l[i],
    print l[-1]

Submission status: Time Limit Exceeded

Solution code:
class MyList(list):
    def __init__(self):
        self.__init__ = list

    def cmd(self,command):
        if command == 'deleteFirst':
            self.pop(0)
        elif command == 'deleteLast':
            self.pop(-1)
        else:
            ope, num = command.split()
            num = int(num)
            if ope == 'insert':
                self.insert(0,num)
            else:
                if num in self:
                    self.remove(num)

def pr(x):
    print x,


if __name__ == '__main__' :
    l = MyList()
    n = input()
    
    for i in range(n):
        l.cmd(raw_input())

    map(pr,l[:-1])
    print l[-1]

Submission status: Time Limit Exceeded

Solution code:
n = input()
l = []
for i in range(int(n)):
	command = input()
	if command[0] == 'i':
		l.insert(0, command[7:])
	else:
		if command[6] == " ":
			if  command[7:] in l:
				l.remove(command[7:])
		else:
			if command[6] == 'L':
				l.pop()
			else:
				l.pop(0)
for i in l[:-1]:
	print(i, end=' ')
print(l[-1])

Submission status: Time Limit Exceeded

Solution code:
class MyList(list):
    def __init__(self):
        self.__init__ = list

        self.d = {'deleteFirst':self.delF, 'deleteLast':self.delL, 'insert':self.insX, 'delete':self.delX}
    def cmd(self,command):
        if ' ' in command:
            ope, num = command.split()
            num = int(num)
            self.d[ope](num)
        else:
            self.d[command]()
            
    def delF(self):
        self.pop(0)

    def delL(self):
        self.pop(-1)

    def insX(self,num):
        self.insert(0,num)

    def delX(self,num):
        if num in self:
            self.remove(num)

if __name__ == '__main__' :
    l = MyList()
    n = input()
    
    for i in range(n):
        l.cmd(raw_input())

    for i in range(len(l)-1):
        print l[i],
    print l[-1]

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

class MyList(list):
    def __init__(self):
        self.__init__ = list

        self.d = {'deleteFirst':self.delF, 'deleteLast':self.delL, 'insert':self.insX, 'delete':self.delX}
    def cmd(self,command):
        if ' ' in command:
            ope, num = command.split()
            num = int(num)
            self.d[ope](num)
        else:
            self.d[command]()
            
    def delF(self):
        self.pop(0)

    def delL(self):
        self.pop(-1)

    def insX(self,num):
        self.insert(0,num)

    def delX(self,num):
        if num in self:
            self.remove(num)

if __name__ == '__main__' :
    l = MyList()
    n = input()
    
    for i in range(n):
        l.cmd(raw_input())

    q = deque(l)
    for i in range(len(l)-1):
        print q.popleft(),
    print l[-1]

Submission status: Time Limit Exceeded

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

nil = Node(None)
nil.prev = nil
nil.next = nil

def insert(key):
	node = Node(key)
	node.next = nil.next
	nil.next.prev = node
	nil.next = node
	node.prev = nil;

def search(key):
	cur = nil.next
	while ((cur != nil) and (cur.key != key)):
		cur = cur.next
	return cur

def delete(t):
	if t!=nil:
		t.prev.next = t.next
		t.next.prev = t.prev

def deleteFirst():
	delete(nil.next)

def deleteLast():
	delete(nil.prev)

n = input()
for _ in range(int(n)):
	command = input()
	if command[0] == 'i':
		insert(command[7:])
	else:
		if command[6] == " ":
			delete(search(command[7:]))
		else:
			if command[6] == 'L':
				deleteLast()
			else:
				deleteFirst()

cur = nil.next
while(1):
	if cur.next == nil:
		break;
	print(cur.key, end=" ")
	cur = cur.next
print(cur.key)

Submission status: Time Limit Exceeded

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

nil = Node(None)
nil.prev = nil
nil.next = nil

def insert(key):
	node = Node(key)
	node.next = nil.next
	nil.next.prev = node
	nil.next = node
	node.prev = nil;

def search(key):
	cur = nil.next
	while ((cur != nil) and (cur.key != key)):
		cur = cur.next
	return cur

def delete(t):
	if t!=nil:
		t.prev.next = t.next
		t.next.prev = t.prev
		del t

def deleteFirst():
	delete(nil.next)

def deleteLast():
	delete(nil.prev)

n = input()
for _ in range(int(n)):
	command = input()
	if command[0] == 'i':
		insert(command[7:])
	else:
		if command[6] == " ":
			delete(search(command[7:]))
		else:
			if command[6] == 'L':
				deleteLast()
			else:
				deleteFirst()

cur = nil.next
while(1):
	if cur.next == nil:
		break;
	print(cur.key, end=" ")
	cur = cur.next
print(cur.key)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

def doubly_lined_list(n, A):
	Q = deque()

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

	print(' '.join(Q))

if __name__ == '__main__':
	n = int(input())
	A = []

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

	doubly_lined_list(n, A)

Submission status: Time Limit Exceeded

Solution code:
def insert_ele(x):
    lin.insert(0,x)
def delete_ele(x):
    if x in lin: lin.remove(x)
def deleteFirst():
    lin.pop(0)
def deleteLast():
    lin.pop()

cnt = input()
lin = []

for i in range(int(cnt)):
    com = input().split()
    if com[0] == "insert":
        insert_ele(com[1])
    if com[0] == "delete":
        delete_ele(com[1])
    if com[0] == "deleteFirst":
        deleteFirst()
    if com[0] == "deleteLast":
        deleteLast()

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

Submission status: Time Limit Exceeded

Solution code:
def insert_ele(x):
    lin.insert(0,x)
def delete_ele(x):
    if x in lin:
        lin.remove(x)
def deleteFirst():
    lin.pop(0)
def deleteLast():
    lin.pop()

cnt = input()
lin = []

for i in range(int(cnt)):
    com = input().split()
    if com[0] == "insert":
        insert_ele(int(com[1]))
    if com[0] == "delete":
        delete_ele(int(com[1]))
    if com[0] == "deleteFirst":
        deleteFirst()
    if com[0] == "deleteLast":
        deleteLast()

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

Submission status: Time Limit Exceeded

Solution code:
cnt = input()
lin = []

for i in range(int(cnt)):
    com = input().split()
    if com[0] == "insert":
        lin.insert(0,com[1])
    if com[0] == "delete":
        if com[1] in lin:
            lin.remove(com[1])
    if com[0] == "deleteFirst":
        lin.pop(0)
    if com[0] == "deleteLast":
        lin.pop()

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

Submission status: Time Limit Exceeded

Solution code:
class Deque:
    #??????????????????
    class Node:
        def __init__(self, x, y = None, z = None):
            self.key = x
            self.next = y
            self.prev = z
    def __init__(self):
        head = Deque.Node(None)
        head.next = head
        head.prev = head
        self.size = 0
        self.head = head

    #insert x:??£??????????????????????????????x?????????????´???????????¶????
    def insert(self,x):
        h = self.head
        q = h.next
        p = Deque.Node(x,q,h)
        h.next = p
        q.prev = p
        self.size += 1

    #deleteFirst:??£????????????????????????????´???????????????????
    def deleteFirst(self):
        if self.size == 0: raise IndexError
        h = self.head
        q = h.next
        p = q.next
        p.prev = h
        h.next = p
        self.size -= 1

    #deleteLast:??£???????????????????°????????´???????????????????
    def deleteLast(self):
        if self.size == 0: raise IndexError
        h = self.head
        q = h.prev
        p = q.prev
        p.next = h
        h.prev = p
        self.size -= 1

    #???????????????????????¢?´¢
    def listSearch(self, x):
        h = self.head
        cur = h.next
        while cur != None and cur.key != x:
            cur = cur.next
        return cur

    #delete:x???????????????????´??????????????????°?????¢?´¢???????§?
    def delete(self, x):
        if self.size == 0: raise IndexError
        if x == None: return
        h = self.listSearch(x)
        q = h.next
        p = h.prev
        p.next = q
        q. prev = p
        self.size -=1

    #??????
    def printList(self):
        h = self.head
        cur = h.next
        isf = 0
        while 1:
            if cur == h: break
            if isf > 0: print(" ", end="")
            print(cur.key, end="")
            isf += 1
            cur = cur.next
        print("")

N = int(input())
CL=[]
d = Deque()
for i in range(N):
    CL.append(input().split())
for i in CL:
    if i[0] == "insert":
        x = i[1]
        d.insert(x)
    if i[0] == "delete":
        x = i[1]
        d.delete(x)
    if i[0] == "deleteFirst":
        d.deleteFirst()
    if i[0] =="deleteLast":
        d.deleteLast()
d.printList()

Submission status: Time Limit Exceeded

Solution code:
class Deque:
    #??????????????????
    class Node:
        def __init__(self, x, y = None, z = None):
            self.key = x
            self.next = y
            self.prev = z
    def __init__(self):
        head = Deque.Node(None)
        head.next = head
        head.prev = head
        self.size = 0
        self.head = head

    #insert x:??£??????????????????????????????x?????????????´???????????¶????
    def insert(self,x):
        h = self.head
        q = h.next
        p = Deque.Node(x,q,h)
        h.next = p
        q.prev = p
        self.size += 1

    #deleteFirst:??£????????????????????????????´???????????????????
    def deleteFirst(self):
        if self.size == 0: raise IndexError
        h = self.head
        q = h.next
        p = q.next
        p.prev = h
        h.next = p
        self.size -= 1

    #deleteLast:??£???????????????????°????????´???????????????????
    def deleteLast(self):
        if self.size == 0: raise IndexError
        h = self.head
        q = h.prev
        p = q.prev
        p.next = h
        h.prev = p
        self.size -= 1

    #???????????????????????¢?´¢
    def listSearch(self, x):
        h = self.head
        cur = h.next
        while cur != None and cur.key != x:
            cur = cur.next
        return cur

    #delete:x???????????????????´??????????????????°?????¢?´¢???????§?
    def delete(self, x):
        if self.size == 0: raise IndexError
        if x == None: return
        h = self.listSearch(x)
        q = h.next
        p = h.prev
        p.next = q
        q. prev = p
        self.size -=1

    #??????
    def printList(self):
        h = self.head
        cur = h.next
        isf = 0
        while 1:
            if cur == h: break
            if isf > 0: print(" ", end="")
            print(cur.key, end="")
            isf = 1
            cur = cur.next
        print("")

n = int(input())
d = Deque()
for i in range(n):
    i = input().split()
    if i[0] == "insert":
        x = i[1]
        d.insert(x)
    if i[0] == "delete":
        x = i[1]
        d.delete(x)
    if i[0] == "deleteFirst":
        d.deleteFirst()
    if i[0] =="deleteLast":
        d.deleteLast()
d.printList()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
doubly linked list
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_3_C&lang=jp
"""
# 9/10 ??§???????¶????????????????

from collections import namedtuple


class DualLinkList(object):
    def __init__(self):
        self.nil = namedtuple('Node', ['key', 'prev', 'next'])
        self.nil.next = self.nil
        self.nil.prev = self.nil

    def insert(self, key):
        x = namedtuple('Node', ['key', 'prev', 'next'])
        x.key = key
        x.next = self.nil.next
        self.nil.next.prev = x
        self.nil.next = x
        x.prev = self.nil

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

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

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

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

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

    def get_keys(self):
        results = []
        cur = self.nil.next
        while cur != self.nil:
            results.append(cur.key)
            cur = cur.next
        return results

    def print(self):
        cur = self.nil.next
        while cur != self.nil:
            print(cur.key)
            cur = cur.next


def process_instructions(dll, insts):
    # line_count = 1
    for ins in insts:
        # print('processing line: {0}'.format(line_count))
        # line_count += 1
        try:
            code, key = ins.split()
        except ValueError:
            code = ins
        if code == 'insert':
            dll.insert(int(key))
        elif code == 'delete':
            dll.deleteKey(int(key))
        elif code == 'deleteFirst':
            dll.deleteFirst()
        elif code == 'deleteLast':
            dll.deleteLast()
        else:
            raise ValueError
    return dll.get_keys()



if __name__ == '__main__':
    # ??????????????\???
    num = int(input())
    data = [input() for x in range(num)]
    #data = []
    #with open('ALDS1_3_C_in9.txt') as f:  # ???????????????????????????
    #    for line in f:
    #       data.append(line.strip())
    #data = data[1:]

    # ??????????????????
    dll = DualLinkList()
    results = process_instructions(dll, data)

    # ???????????¨???
    print('{0}'.format(' '.join(map(str, results))))

Submission status: Time Limit Exceeded

Solution code:
class Deque:
    #??????????????????
    class Node:
        def __init__(self, x, y = None, z = None):
            self.key = x
            self.next = y
            self.prev = z
    def __init__(self):
        head = Deque.Node(None)
        head.next = head
        head.prev = head
        self.size = 0
        self.head = head

    #insert x:??£??????????????????????????????x?????????????´???????????¶????
    def insert(self,x):
        h = self.head
        q = h.next
        p = Deque.Node(x,q,h)
        h.next = p
        q.prev = p
        self.size += 1

    #deleteFirst:??£????????????????????????????´???????????????????
    def deleteFirst(self):
        if self.size == 0: raise IndexError
        h = self.head
        q = h.next
        p = q.next
        p.prev = h
        h.next = p
        self.size -= 1

    #deleteLast:??£???????????????????°????????´???????????????????
    def deleteLast(self):
        if self.size == 0: raise IndexError
        h = self.head
        q = h.prev
        p = q.prev
        p.next = h
        h.prev = p
        self.size -= 1

    #???????????????????????¢?´¢
    def listSearch(self, x):
        h = self.head
        cur = h.next
        while cur.key != None and cur.key != x:
            cur = cur.next
        return cur

    #delete:x???????????????????´??????????????????°?????¢?´¢???????§?
    def delete(self, x):
        if self.size == 0: raise IndexError
        if x == None: return
        h = self.listSearch(x)
        if h.key == None: return
        q = h.next
        p = h.prev
        p.next = q
        q. prev = p
        self.size -=1

    #??????
    def printList(self):
        h = self.head
        cur = h.next
        L = []
        while 1:
            if cur.key == None: break
            L.append(cur.key)
            cur = cur.next
        print(" ".join(L))

n = int(input())
d = Deque()
for i in range(n):
    i = input().split()
    if i[0] == "insert":
        x = i[1]
        d.insert(x)
    if i[0] == "delete":
        x = i[1]
        d.delete(x)
    if i[0] == "deleteFirst":
        d.deleteFirst()
    if i[0] =="deleteLast":
        d.deleteLast()
d.printList()

Submission status: Time Limit Exceeded

Solution code:
#ALDS1_3-C Elementary data structures - Doubly Linked List
n=int(input())
L=[]
for i in range(n):
    cmd=input().split()
    if(cmd[0]=="insert"):
        L=[cmd[1]]+L
    if(cmd[0]=="delete" and cmd[1] in L):
        L.remove(cmd[1])
    if(cmd[0]=="deleteFirst"):
        L.pop(0)
    if(cmd[0]=="deleteLast"):
        L.pop()
s=""
for i in L:
    s+=i+" "
print(s[:-1])

Submission status: Time Limit Exceeded

Solution code:
#ALDS1_3-C Elementary data structures - Doubly Linked List
n=int(input())
L=[]
for i in range(n):
    cmd=input().split()
    if(cmd[0]=="insert"):
        L=[cmd[1]]+L
    elif(cmd[0]=="delete" and cmd[1] in L):
        L.remove(cmd[1])
    elif(cmd[0]=="deleteFirst"):
        L.pop(0)
    if(cmd[0]=="deleteLast"):
        L.pop()
s=""
for i in L:
    s+=i+" "
print(s[:-1])

Submission status: Time Limit Exceeded

Solution code:
class Deque:
    class Node:
        def __init__(self, x, y = None, z = None):
            self.key = x
            self.next = y
            self.prev = z
    def __init__(self):
        head = Deque.Node(None)
        head.next = head
        head.prev = head
        self.size = 0
        self.head = head

    def insert(self,x):
        h = self.head
        q = h.next
        p = Deque.Node(x,q,h)
        h.next = p
        q.prev = p
        self.size += 1

    def deleteFirst(self):
        if self.size == 0: raise IndexError
        h = self.head
        q = h.next
        p = q.next
        p.prev = h
        h.next = p
        self.size -= 1

    def deleteLast(self):
        if self.size == 0: raise IndexError
        h = self.head
        q = h.prev
        p = q.prev
        p.next = h
        h.prev = p
        self.size -= 1

    def listSearch(self, x):
        h = self.head
        cur = h.next
        while cur.key != None and cur.key != x:
            cur = cur.next
        return cur

    def delete(self, x):
        if self.size == 0: raise IndexError
        if x == None: return
        h = self.listSearch(x)
        if h.key == None: return
        q = h.next
        p = h.prev
        p.next = q
        q. prev = p
        self.size -=1

    def printList(self):
        h = self.head
        cur = h.next
        L = []
        while 1:
            if cur.key == None: break
            L.append(str(cur.key))
            cur = cur.next
        print(" ".join(L))

n = int(input())
d = Deque()
for i in range(n):
    i = input().split()
    if i[0] == "insert":
        x = int(i[1])
        d.insert(x)
    if i[0] == "delete":
        x = int(i[1])
        d.delete(x)
    if i[0] == "deleteFirst":
        d.deleteFirst()
    if i[0] =="deleteLast":
        d.deleteLast()
d.printList()