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:
class LinkedListNode:
    def __init__(self, key, prev_node, next_node):
        self.key = key
        self.prev_node = prev_node
        self.next_node = next_node

class LinkedList:
    def __init__(self):
        self.front_node = None
        self.last_node = None
        self.size = 0

    def insert(self, key):
        new_node = LinkedListNode(key, None, self.front_node)
        if self.front_node != None:
            self.front_node.prev_node = new_node
        self.front_node = new_node
        if self.last_node == None:
            self.last_node = new_node
        self.size += 1

    def delete(self, key):
        node = self.front_node
        while node != None:
            if node.key == key:
                if node == self.front_node:
                    self.front_node = self.front_node.next_node
                if node == self.last_node:
                    self.last_node = self.last_node.prev_node
                if node.prev_node != None:
                    node.prev_node.next_node = node.next_node
                if node.next_node != None:
                    node.next_node.prev_node = node.prev_node
                self.size -= 1
                break
            node = node.next_node

    def delete_first(self):
        if self.size == 0:
            return
        elif self.size == 1:
            self.front_node = None
            self.last_node = None
            self.size -= 1
        else:
            self.front_node = self.front_node.next_node
            self.front_node.prev_node = None
            self.size -= 1

    def delete_last(self):
        if self.size == 0:
            return
        elif self.size == 1:
            self.front_node = None
            self.last_node = None
            self.size -= 1
        else:
            self.last_node = self.last_node.prev_node
            self.last_node.next_node = None
            self.size -= 1

    def to_a(self):
        a = []
        node = self.front_node
        while node != None:
            a.append(node.key)
            node = node.next_node
        return a

l = LinkedList()
n = int(input())
for i in range(n):
    ins = input().split()
    command = ins[0]
    if command == "insert":
        x = int(ins[1])
        l.insert(x)
    elif command == "delete":
        x = int(ins[1])
        l.delete(x)
    elif command == "deleteFirst":
        l.delete_first()
    elif command == "deleteLast":
        l.delete_last()

print(" ".join(map(str, l.to_a())))

Submission status: Time Limit Exceeded

Solution code:
import sys

class LinkedListNode:
    def __init__(self, key, prev_node, next_node):
        self.key = key
        self.prev_node = prev_node
        self.next_node = next_node

class LinkedList:
    def __init__(self):
        self.front_node = None
        self.last_node = None
        self.size = 0

    def insert(self, key):
        new_node = LinkedListNode(key, None, self.front_node)
        if self.front_node != None:
            self.front_node.prev_node = new_node
        self.front_node = new_node
        if self.last_node == None:
            self.last_node = new_node
        self.size += 1

    def delete(self, key):
        node = self.front_node
        while node != None:
            if node.key == key:
                if node == self.front_node:
                    self.front_node = self.front_node.next_node
                if node == self.last_node:
                    self.last_node = self.last_node.prev_node
                if node.prev_node != None:
                    node.prev_node.next_node = node.next_node
                if node.next_node != None:
                    node.next_node.prev_node = node.prev_node
                self.size -= 1
                break
            node = node.next_node

    def delete_first(self):
        if self.size == 0:
            return
        elif self.size == 1:
            self.front_node = None
            self.last_node = None
            self.size -= 1
        else:
            self.front_node = self.front_node.next_node
            self.front_node.prev_node = None
            self.size -= 1

    def delete_last(self):
        if self.size == 0:
            return
        elif self.size == 1:
            self.front_node = None
            self.last_node = None
            self.size -= 1
        else:
            self.last_node = self.last_node.prev_node
            self.last_node.next_node = None
            self.size -= 1

    def to_a(self):
        a = []
        node = self.front_node
        while node != None:
            a.append(node.key)
            node = node.next_node
        return a

l = LinkedList()
n = int(input())
lines = sys.stdin.readlines()
for i in range(n):
    ins = lines[i].split()
    command = ins[0]
    if command == "insert":
        x = int(ins[1])
        l.insert(x)
    elif command == "delete":
        x = int(ins[1])
        l.delete(x)
    elif command == "deleteFirst":
        l.delete_first()
    elif command == "deleteLast":
        l.delete_last()

print(" ".join(map(str, l.to_a())))

Submission status: Time Limit Exceeded

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

while(n):
	s=input().split()
	com=s[0]
	if com=="insert":
		l.insert(0,s[1])
	elif com=="delete":
		for i in range(len(l)):
			if l[i]==s[1]:
				l.pop(i)
				break
	elif com=="deleteFirst":
		l.pop(0)
	else:
		l.pop()
	n-=1

for i in range(len(l)-1):
	print(l[i],end=" ")
print(l[len(l)-1],end="\n")

Submission status: Time Limit Exceeded

Solution code:
#http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_3_C&lang=jp
#???????????£????????????
#??¬?????????????????¬????????§????????¨???????????????????????????dummy????????????????????¨??§???????????????????????????????????§??????
def dll_processor(operations):
    dll = Doubly_Linked_List()
    
    for o in operations:
        opcode = o[0]
        if opcode == "insert":
            dll.insert(o[1])
        elif opcode == "delete":
            dll.delete(o[1])
        elif opcode == "deleteFirst":
            dll.deleteFirst()
        elif opcode == "deleteLast":
            dll.deleteLast()
        #print(dll.get_list())
    return dll.get_list()

class Doubly_Linked_List:
    
    def __init__(self):
        self.next = None
        self.prev = None
        self.data = "dummy"

    def get_list(self):
        first = self
        while first.prev:
            first = first.prev
            
        target = first
        l = [target.data]
        while target.next:
            if not target.next.data == "dummy":
                l.append(target.next.data)
            target = target.next
        return l
    
    def insert(self, x):
        
        first = self
        while first.prev:
            first = first.prev

        first.prev = Doubly_Linked_List()#???????????????
        first.prev.next = first
        first.prev.data = x

    def deleteFirst(self):
        first = self

        while first.prev:
            first = first.prev

        if first.data == "dummy" :
            first.next = first.next.next
            first.next.last = first
        else:
            first.next.prev = None
        
    def deleteLast(self):
        last = self
        
        while last.next:
            last = last.next

        if last.data == "dummy":
            last.prev = last.prev.prev
            if last.prev:
                last.prev.next = last
        else:
            last.prev.next = None
        
    def delete(self, x):
        first = self
        while first.prev:
            first = first.prev

        target = first
        while not target.data == x:
            if target.next:
                target = target.next
            else:
                return
    
        if target.prev == None:
            target.next.prev = None
        elif target.next == None:
            target.prev.next = None
        else:
            target.next.prev = target.prev
            target.prev.next = target.next

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

Solution code:
def delete(l, num):
    for i, x in enumerate(l):
        if x == num:
            return l[0:i] + l[i+1:]
    return l


n = int(input())
l = []
for i in range(n):
    com = input().split()
    if com[0] == 'insert':
        l = [int(com[1])] + l
    elif com[0] == 'delete':
        l = delete(l, int(com[1]))
    elif com[0] == 'deleteFirst':
        l = l[1:]
    elif com[0] == 'deleteLast':
        l = l[:-1]
print(*l)

Submission status: Time Limit Exceeded

Solution code:
#python2.7
N= input()
A=[]
B=[]
for i in range(N):
	A.append(raw_input().split())

for i in A:
	if(i[0]=="insert"):
		B.insert(0,i[1])
	elif(i[0]=="delete"):
		for j in range(len(B)):
			if(B[j]==i[1]):
				del B[j]
				break
	elif(i[0]=="deleteFirst"):
		del B[0]
	elif(i[0]=="deleteLast"):
		del B[-1]

for k in range(len(B)-1):
	print B[k],
print B[-1]

Submission status: Time Limit Exceeded

Solution code:
#http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_3_C&lang=jp
#???????????£????????????
#??¬?????????????????¬????????§????????¨???????????????????????????dummy????????????????????¨??§???????????????????????????????????§??????
import gc
def dll_processor(operations):
    
    front = None
    
    for o in operations:
        opcode = o[0]
        if opcode == "insert":
            front = insert(front, { "next":None, "prev":None, "data":o[1] })
        elif opcode == "delete":
            front = delete(front, o[1])
        elif opcode == "deleteFirst":
            front = delete_first(front)
        elif opcode == "deleteLast":
            front = delete_last(front)
        gc.collect()
        #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, target):

    if front:
        front["prev"] = target
        target["next"] = front

    return target
    
def delete(front, target):

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

    if delete_node["prev"] == None:
        delete_node["next"]["prev"] = None
        return delete_node["next"]
    elif delete_node["next"] == None:
        delete_node["prev"]["next"] = None
        return front
    else:
        delete_node["next"]["prev"] = delete_node["prev"]
        delete_node["prev"]["next"] = delete_node["next"]
        return front
    
def delete_last(front):
    last = front
    while last["next"]:
        last = last["next"]

    if last == front:
        return None
    else:
        last["prev"]["next"] = None
        return front
    
def delete_first(front):
    if not front["next"]:
        return None
    
    front["next"]["prev"] = None
    return front["next"]

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

Solution code:
#http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_3_C&lang=jp
#???????????£????????????
#??¬?????????????????¬????????§????????¨???????????????????????????dummy????????????????????¨??§???????????????????????????????????§??????
import gc
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)
        gc.collect()
        #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: Time Limit Exceeded

Solution code:
#http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_3_C&lang=jp
#???????????£????????????
#??¬?????????????????¬????????§????????¨???????????????????????????dummy????????????????????¨??§???????????????????????????????????§??????
import gc
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])
            gc.collect()
        elif o[0] == "deleteFirst":
            front, end = delete_first(front, end)
            gc.collect()
        elif o[0] == "deleteLast":
            front, end = delete_last(front, end)
            gc.collect()
        #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: Time Limit Exceeded

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

NEXT = 2
DATA = 1
PREV = 0
import gc
def dll_processor(operations):
    
    front = None
    end = None
    
    for i,o in enumerate(operations):
        if o[0] == "insert":
            front, end = insert(front, end, [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)
        if i % 10 == 0:
            gc.collect()
        #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: Time Limit Exceeded

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

NEXT = 2
DATA = 1
PREV = 0
import gc
def dll_processor(operations):
    
    front = None
    end = None
    
    for i,o in enumerate(operations):
        if o[0] == "insert":
            front, end = insert(front, end, [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)
        if i % 100 == 0:
            gc.collect()
        #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: Time Limit Exceeded

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

NEXT = 2
DATA = 1
PREV = 0
import gc
def dll_processor(operations):
    
    front = None
    end = None
    
    for i,o in enumerate(operations):
        if o[0] == "insert":
            front, end = insert(front, end, [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)
        gc.collect(1)
        #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: Time Limit Exceeded

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

NEXT = 2
DATA = 1
PREV = 0
class DLL:
    __slots__ = ["next", "prev", "data"]

    def __init__(self, data):
        self.next = None
        self.prev = None
        self.data = data

def dll_processor(operations):
    
    front = None
    end = None
    
    for i,o in enumerate(operations):
        if o[0] == "insert":
            front, end = insert(front, end, DLL(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
        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: Time Limit Exceeded

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

NEXT = 2
DATA = 1
PREV = 0
class DLL:
    __slots__ = ["next", "prev", "data"]

    def __init__(self, data):
        self.next = None
        self.prev = None
        self.data = data

def dll_processor(operations):
    
    front = None
    end = None
    
    for i,o in enumerate(operations):
        if o[0] == "insert":
            front, end = insert(front, end, DLL(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
        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
        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
        return front, end.prev
    
def delete_first(front, end):
    
    if not front.next:
        del front, end
        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: Time Limit Exceeded

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

NEXT = 2
DATA = 1
PREV = 0
class DLL:
    __slots__ = ["next", "prev", "data"]

    def __init__(self, data):
        self.next = None
        self.prev = None
        self.data = data

def dll_processor(operations):
    
    front = None
    end = None
    
    for i,o in enumerate(operations):
        if o[0] == "insert":
            front, end = insert(front, end, DLL(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
        delete_node = delete_node.next
        return delete_node, end
    elif delete_node.next == None:
        delete_node.prev.next = None
        delete_node = delete_node.prev
        return front, delete_node
    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
        end = end.prev
        return front, end
    
def delete_first(front, end):
    
    if not front.next:
        del front, end
        return None, None
    else:
        front.next.prev = None
        front = front.next
        return front, 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()