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:
n = int(raw_input())
linked = []
for i in range(n):
    command  = raw_input()
    if command == 'deleteFirst':
        linked.pop(0)
    elif command == 'deleteLast':
        linked.pop()
    elif 'insert' in command:
        _, num = command.split()
        linked.insert(0, num)
    elif 'delete' in command:
        _, num = command.split()
        try:
            linked.remove(num)
        except:
            pass
        
for i in range(len(linked)):
    print linked[i],

Submission status: Time Limit Exceeded

Solution code:
n = int(raw_input())
linked = []
command = []
for i in range(n):
    command.append(raw_input())
    
for i in range(n):
    if command[i] == 'deleteFirst':
        linked.pop(0)
    elif command[i] == 'deleteLast':
        linked.pop()
        
    elif 'insert' in command[i]:
        _, num = command[i].split()
        linked.insert(0, num)
    elif 'delete' in command[i]:
        _, num = command[i].split()
        try:
            linked.remove(num)
        except:
            pass
        
for i in range(len(linked)):
    print linked[i],

Submission status: Time Limit Exceeded

Solution code:
n = int(raw_input())
linked = []
command = []
for i in range(n):
    command.append(raw_input())
    
for i in range(n):
    if command[i] == 'deleteFirst':
        linked.pop(0)
    elif command[i] == 'deleteLast':
        linked.pop()
        
    elif 'insert' in command[i]:
        _, num = command[i].split()
        linked.insert(0, num)
    elif 'delete' in command[i]:
        _, num = command[i].split()
        try:
            linked.remove(num)
        except:
            pass
        
print ' '.join(map(str, linked))

Submission status: Time Limit Exceeded

Solution code:
n = int(raw_input())
linked = []
command = []
for i in range(n):
    command.append(raw_input())
    
for i in range(n):
    if command[i] == 'deleteFirst':
        linked.pop(0)
    elif command[i] == 'deleteLast':
        linked.pop()
        
    elif 'insert' in command[i]:
        #_, num = command[i].split()
        num = command[i][7:]
        linked.insert(0, num)
    elif 'delete' in command[i]: 
        #_, num = command[i].split()
        num = command[i][7:]
        try:
            linked.remove(num)
        except:
            pass
        
print ' '.join(map(str, linked))

Submission status: Time Limit Exceeded

Solution code:
#list
n=int(input())
l=[]
for i in range(n):
    c=[i for i in input().split()]
    d=c[0]
 #   print(c)
    if d=="insert":
 #       print(l)
        l.insert(0,c[1])
    #    print(l)
    elif d=="delete":
  #      print(l)
        j=0
        while j<len(l) and c[1]!=l[j]:
            j+=1
        if j<len(l):
            l.pop(j)
    elif d=="deleteFirst":
        l.pop(0)
    else:
        l.pop(-1)
#print(l)
p=""
for i in range(len(l)-1):
    p+=l[i]+" "
  #  print(p)
p+=l[len(l)-1]
print(p)

Submission status: Time Limit Exceeded

Solution code:
# Aizu Problem ALDS_1_3_C: Doubly Linked List
#
import sys, math, os

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input2.txt", "rt")


def printA(A):
    print(' '.join([str(a) for a in A]))

N = int(input())
A = []
for _ in range(N):
    s = input().split()
    cmd = s[0]
    if cmd == "insert":
        A = [int(s[1])] + A
    elif cmd == "delete":
        x = int(s[1])
        if x in A:
            A.remove(x)
    elif cmd == "deleteFirst":
        A.pop(0)
    elif cmd == "deleteLast":
        A.pop()
printA(A)

Submission status: Time Limit Exceeded

Solution code:
NEXT = 2
DATA = 1
PREV = 0
def dll_processor(operations):

    front = None
    end = None

    for o in operations:
        if o[0] == "insert":
            front, end = insert(front, end, 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):
    node = [None, target, None]
    if front:
        front[PREV] = node
        node[NEXT] = front
        return node, end
    else:
        return node, node

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:
n=int(input())
commands=[input().split() for i in range(n)]
    
List=[]
def command(list_command):
    global List
    if 'insert' in list_command:
        List.insert(0,list_command[1])
    elif 'delete' in list_command:
        try:
            t=List.index(list_command[1])
            del List[t]
        except:
            pass
    elif 'deleteFirst' in list_command:
        del List[0]
    elif 'deleteLast' in list_command:
        del List[-1]
    return True
    
for i in commands:
    command(i)
print(*List)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
n=int(input())
commands=[input().split() for i in range(n)]
    
List=deque()
def command(list_command):
    global List
    if 'insert' in list_command:
        List.appendleft(list_command[1])
    elif 'delete' in list_command:
        try:
            List.remove(list_command[1])
        except:
            pass
    elif 'deleteFirst' in list_command:
        List.popleft()
    elif 'deleteLast' in list_command:
        List.pop()
    return True
    
for i in commands:
    command(i)
print(*List)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

Commands = []
for i in range(N):
    l = input().split()
    if len(l) > 1:
        Commands.append([l[0], int(l[1])])
    else:
        Commands.append([l[0]])

A = []
for c in Commands:
    if c[0] == 'insert':
        A.insert(0, c[1])
    elif c[0] == 'delete':
        if c[1] in A:
            A.remove(c[1])
    elif c[0] == 'deleteFirst':
        del A[0]
    elif c[0] == 'deleteLast':
        del A[-1]
    else:
        'error'

print(*A)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = []
for i in range(n):
    tmp = input().split()
    ope = tmp[0]
    if ope == "insert":
        m = tmp[1]
        l.insert(0,m)
    elif ope == "delete":
        m = tmp[1]
        if m in l:
            l.remove(m)
    elif ope == "deleteFirst":
        l.pop(0)
    elif ope == "deleteLast":
        l.pop()
print(*l)

Submission status: Time Limit Exceeded

Solution code:
class DoublyLinkedKist:
    DLlist = []

    def command(self, cmd):
        if cmd[0] == 'insert':
            self.DLlist[:0] = [cmd[1]]
        elif cmd[0] == 'delete':
            if cmd[1] in self.DLlist:
                self.DLlist.remove(cmd[1])
        elif cmd[0] == 'deleteFirst':
            self.DLlist.pop(0)
        elif cmd[0] == 'deleteLast':
            self.DLlist.pop()

if __name__ == '__main__':
    DL = DoublyLinkedKist()
    for i in range(int(input())):
        DL.command(input().split())
    print(*DL.DLlist)

Submission status: Time Limit Exceeded

Solution code:
class MyList(object):
    def __init__(self, value=None, pre=None, _next=None):
        self._value = value
        self._pre = pre
        self._next = _next

    def insert(self, value):
        _next = self._next
        self._next = MyList(value, self, _next)
        if _next is not None:
            _next._pre = self._next

    def delete(self, value):
        current = self._next
        while current is not None:
            if current._value == value:
                current._pre._next = current._next
                if current._next is not None:
                    current._next._pre = current._pre
                break
            current = current._next

    def delete_first(self):
        if self._next is not None:
            if self._next._next is not None:
                self._next._next._pre = self
            self._next = self._next._next

    def delete_last(self):
        current = self
        while current._next is not None:
            current = current._next
        if current._pre is not None:
            current._pre._next = None

    def toList(self):
        current = self._next
        lst = []
        while current is not None:
            lst.append(current._value)
            current = current._next
        return lst


def doubly_inked_list(commands):
    lst = MyList()
    for command in commands:
        if command[0] == "insert":
            lst.insert(command[1])
        elif command[0] == "delete":
            lst.delete(command[1])
        elif command[0] == "deleteFirst":
            lst.delete_first()
        elif command[0] == "deleteLast":
            lst.delete_last()
        else:
            raise ValueError
    return lst.toList()


if __name__ == "__main__":
    n = int(input())
    commands = [input().split() for i in range(0, n)]
    print(" ".join(doubly_inked_list(commands)))

Submission status: Time Limit Exceeded

Solution code:
class MyList(object):
    def __init__(self, value=None, pre=None, _next=None, _tail=None):
        self._value = value
        self._pre = pre
        self._next = _next
        if _tail is None:
            self._tail = self
        else:
            self._tail = _tail

    def insert(self, value):
        _next = self._next
        self._next = MyList(value, self, _next, self._tail)
        if _next is not None:
            _next._pre = self._next
        else:
            self._tail = self._next

    def delete(self, value):
        current = self._next
        while current is not None:
            if current._value == value:
                current._pre._next = current._next
                if current._next is not None:
                    current._next._pre = current._pre
                else:
                    self._tail = current._pre
                break
            current = current._next

    def delete_first(self):
        first = self._next
        if first is not None:
            if first._next is not None:
                first._next._pre = self
            else:
                self._tail = self
            self._next = first._next

    def delete_last(self):
        tail = self._tail
        if tail._pre is not None:
            tail._pre._next = None
            self._tail = tail._pre

    def toList(self):
        current = self._next
        lst = []
        while current is not None:
            lst.append(current._value)
            current = current._next
        return lst


def doubly_inked_list(commands):
    lst = MyList()
    for command in commands:
        if command[0] == "insert":
            lst.insert(command[1])
        elif command[0] == "delete":
            lst.delete(command[1])
        elif command[0] == "deleteFirst":
            lst.delete_first()
        elif command[0] == "deleteLast":
            lst.delete_last()
        else:
            raise ValueError
    return lst.toList()


if __name__ == "__main__":
    n = int(input())
    commands = [input().split() for i in range(0, n)]
    print(" ".join(doubly_inked_list(commands)))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

n = int(input())
cmd = [input().split() for i in range(n)]
stack = deque()

for i in range(n):
    if cmd[i][0] == "insert":
        stack.appendleft(cmd[i][1])
    elif cmd[i][0] == "delete":
        if cmd[i][1] in stack:
            stack.remove(cmd[i][1])
    elif cmd[i][0] == "deleteFirst":
        stack.popleft()
    elif cmd[i][0] == "deleteLast":
        stack.pop()
print(*stack)

Submission status: Time Limit Exceeded

Solution code:
n = int(raw_input())

A = []
for i in range(n):
	com = raw_input().split()
	if com[0] == "insert":
		A.insert(0, int(com[1]))
	elif com[0] == "delete":
		if int(com[1]) in A:
			index = A.index(int(com[1]))
			del A[index]
	elif com[0] == "deleteFirst":
		del A[0]
	else:
		del A[-1]

for i in range(len(A)):
	if i != len(A)-1:
		print(A[i]),
	else:
		print(A[i])

Submission status: Time Limit Exceeded

Solution code:
n = int(raw_input())

A = []
for i in range(n):
	com = raw_input().split()
	if com[0] == "insert":
		A.insert(0, int(com[1]))
	elif com[0] == "delete":
		try:
			index = A.index(int(com[1]))
			del A[index]
		except:
			pass
		#if int(com[1]) in A:
		#	index = A.index(int(com[1]))
		#	del A[index]
	elif com[0] == "deleteFirst":
		del A[0]
	else:
		del A[-1]

for i in range(len(A)):
	if i != len(A)-1:
		print(A[i]),
	else:
		print(A[i])

Submission status: Time Limit Exceeded

Solution code:
k = int(input())
l=[]
x=[]
for i in range(k):
    buff = input()
    if "deleteFirst" in buff:
        x.pop(0)
    elif "deleteLast" in buff:
        x.pop(len(x)-1)
    elif "insert" in buff:
        x.insert(0, ((buff.lstrip('insert '))))
    elif "delete" in buff:
        try:
            x.remove((buff.lstrip('delete ')))
        except:
            pass
print(" ".join(x))

Submission status: Time Limit Exceeded

Solution code:
k = int(input())
l=[]
x=[]
for i in range(k):
    buff = input()
    if "deleteFirst" in buff:
        x.pop(0)
    elif "deleteLast" in buff:
        x.pop(len(x)-1)
    elif "insert" in buff:
        x.insert(0, (buff.lstrip('insert ')))
    elif "delete" in buff:
        try:
            x.remove(buff.lstrip('delete '))
        except:
            pass
print(" ".join(x))

Submission status: Time Limit Exceeded

Solution code:
k = int(input())
l=[]
x=[]
for i in range(k):
    buff = input()
    if "deleteFirst" in buff:
        del x[0]
    elif "deleteLast" in buff:
        del x[-1]
    elif "insert" in buff:
        x.insert(0, (buff.lstrip('insert ')))
    elif "delete" in buff:
        try:
            x.remove(buff.lstrip('delete '))
        except:
            pass
print(" ".join(x))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
dlist = []
for i in range(n):
    code = input().split()
    if code[0] == "insert":
        dlist.insert(0,code[1])
    if code[0] == "delete":
        try:
            dlist.remove(code[1])
        except:
            continue
    if code[0] == "deleteFirst":
        dlist.pop(0)
    if code[0] == "deleteLast":
        dlist.pop()
print(*dlist,sep=" ")

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = []
for i in range(n):
    com = input().split()
    if com[0] == "insert":
        key = int(com[1]) 
        l.insert(0, int(key))
    elif com[0] == "delete":
        key = int(com[1]) 
        if int(key) in l:
            l.remove(int(key))
    elif com[0] == "deleteFirst":
        del l[0]
    else:
        del l[-1]
print(*l)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = []
for i in range(n):
    com = input().split()
    if com[0] == "insert":
        key = int(com[1]) 
        l.insert(0, key)
    elif com[0] == "delete":
        key = int(com[1]) 
        if int(key) in l:
            l.remove(key)
    elif com[0] == "deleteFirst":
        del l[0]
    else:
        del l[-1]
print(*l)

Submission status: Time Limit Exceeded

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

class Node():
    def __init__(self, key, prev=None, nex=None):
        self.key = key
        self.prev = prev
        self.next = nex
    
class DoublyLinkedLiset():
    def __init__(self):
        self.nil = Node(None)
        self.nil.next = self.nil
        self.nil.prev = self.nil
    
    def insert(self, x):
        X = Node(x)
        X.next = self.nil.next
        self.nil.next.prev = X
        self.nil.next = X
        X.prev = self.nil

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

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

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

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

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

    def printList(self):
        cur = self.nil.next
        isf = 0
        while cur != self.nil:
            if isf > 0:
                print(" ", end="")
            print(cur.key, end="")
            cur = cur.next
            isf += 1
        print()

if __name__ == '__main__':

    n = int(input())
    dll = DoublyLinkedLiset()

    for i in range(n):
        line = input().split(" ")
        if len(line) == 2:
            command, key = line
            if command == "insert":
                dll.insert(key)
            elif command == "delete":
                dll.deleteKey(key)

        else:
            command = line[0]
            if command == "deleteFirst":
                dll.deleteFirst()
            elif command == "deleteLast":
                dll.deleteLast()

    dll.printList()

Submission status: Time Limit Exceeded

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

class Node():
    def __init__(self, key, prev=None, nex=None):
        self.key = key
        self.prev = prev
        self.next = nex
    
class DoublyLinkedLiset():
    def __init__(self):
        self.nil = Node(None)
        self.nil.next = self.nil
        self.nil.prev = self.nil
    
    def insert(self, x):
        X = Node(x)
        X.next = self.nil.next
        self.nil.next.prev = X
        self.nil.next = X
        X.prev = self.nil

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

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

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

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

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

    def printList(self):
        cur = self.nil.next
        ret = []
        
        while cur.key is not None:
            ret.append(cur.key)
            cur = cur.next
  
        return ' '.join(ret)

if __name__ == '__main__':

    n = int(input())
    dll = DoublyLinkedLiset()

    for i in range(n):
        line = input().split(" ")
        if len(line) == 2:
            command, key = line
            if command == "insert":
                dll.insert(key)
            elif command == "delete":
                dll.deleteKey(key)

        else:
            command = line[0]
            if command == "deleteFirst":
                dll.deleteFirst()
            elif command == "deleteLast":
                dll.deleteLast()

    print(dll.printList())

Submission status: Time Limit Exceeded

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

class Node():
    def __init__(self, key, prev=None, nex=None):
        self.key = key
        self.prev = prev
        self.next = nex
    
class DoublyLinkedLiset():
    def __init__(self):
        self.nil = Node(None)
        self.nil.next = self.nil
        self.nil.prev = self.nil
    
    def insert(self, x):
        X = Node(x)
        X.next = self.nil.next
        self.nil.next.prev = X
        self.nil.next = X
        X.prev = self.nil

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

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

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

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

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

    def printList(self):
        cur = self.nil.next
        ret = []
        
        while cur.key is not None:
            ret.append(cur.key)
            cur = cur.next
  
        return ' '.join(ret)

if __name__ == '__main__':

    n = int(input())
    dll = DoublyLinkedLiset()

    for i in range(n):
        line = input().split(" ")
        if len(line) == 2:
            command, key = line
            if command == "insert":
                dll.insert(key)
            elif command == "delete":
                dll.deleteKey(key)

        else:
            command = line[0]
            if command == "deleteFirst":
                dll.deleteFirst()
            elif command == "deleteLast":
                dll.deleteLast()

    print(dll.printList())

Submission status: Time Limit Exceeded

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

class Node(object):
    def __init__(self, key, prev=None, nex=None):
        self.key = key
        self.prev = prev
        self.next = nex
    
class DoublyLinkedLiset(object):
    def __init__(self):
        self.nil = Node(None)
        self.nil.next = self.nil
        self.nil.prev = self.nil
    
    def insert(self, x):
        X = Node(x)
        X.next = self.nil.next
        self.nil.next.prev = X
        self.nil.next = X
        X.prev = self.nil

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

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

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

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

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

    def printList(self):
        cur = self.nil.next
        ret = []
        
        while cur.key is not None:
            ret.append(cur.key)
            cur = cur.next
  
        print(' '.join(ret))

if __name__ == '__main__':

    n = int(input())
    dll = DoublyLinkedLiset()

    for i in range(n):
        command = input().split(" ")
        if command[0] == "insert":
            dll.insert(command[1])
        elif command[0] == "delete":
            dll.deleteKey(command[1])
        elif command[0] == "deleteFirst":
            dll.deleteFirst()
        elif command[0] == "deleteLast":
            dll.deleteLast()

    dll.printList()

Submission status: Time Limit Exceeded

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


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


class DoublyLinkedLiset(object):
    def __init__(self):
        self.nil = Node(None)
        self.nil.next = self.nil
        self.nil.prev = self.nil

    def insert(self, x):
        X = Node(x)
        X.next = self.nil.next
        self.nil.next.prev = X
        self.nil.next = X
        X.prev = self.nil

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

        return cur

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

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

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

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

    def printList(self):
        cur = self.nil.next
        ret = []

        while cur.key is not None:
            ret.append(cur.key)
            cur = cur.next

        print(' '.join(ret))


if __name__ == '__main__':

    n = int(input())
    dll = DoublyLinkedLiset()

    for i in range(n):
        command = input().split(" ")
        if command[0] == "insert":
            dll.insert(command[1])
        elif command[0] == "delete":
            dll.deleteKey(command[1])
        elif command[0] == "deleteFirst":
            dll.deleteFirst()
        elif command[0] == "deleteLast":
            dll.deleteLast()

    dll.printList()

Submission status: Time Limit Exceeded

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


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


class DoublyLinkedLiset(object):
    def __init__(self):
        self.nil = Node(None)
        self.nil.next = self.nil
        self.nil.prev = self.nil

    def insert(self, x):
        X = Node(x)
        X.next = self.nil.next
        self.nil.next.prev = X
        self.nil.next = X
        X.prev = self.nil

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

        return cur

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

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

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

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

    def printList(self):
        cur = self.nil.next
        ret = []

        while cur.key is not None:
            ret.append(cur.key)
            cur = cur.next

        print(' '.join(ret))


if __name__ == '__main__':

    n = int(input())
    dll = DoublyLinkedLiset()

    for _ in range(n):
        command = input().split(" ")
        if command[0] == "insert":
            dll.insert(command[1])
        elif command[0] == "delete":
            dll.deleteKey(command[1])
        elif command[0] == "deleteFirst":
            dll.deleteFirst()
        elif command[0] == "deleteLast":
            dll.deleteLast()

    dll.printList()

Submission status: Time Limit Exceeded

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


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


class DoublyLinkedLiset(object):
    def __init__(self):
        self.nil = Node(None)
        self.nil.next = self.nil
        self.nil.prev = self.nil

    def insert(self, x):
        X = Node(x)
        X.next = self.nil.next
        self.nil.next.prev = X
        self.nil.next = X
        X.prev = self.nil

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

        return cur

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

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

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

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

    def printList(self):
        cur = self.nil.next
        ret = []

        while cur.key is not None:
            ret.append(cur.key)
            cur = cur.next

        print(' '.join(ret))


if __name__ == '__main__':

    n = int(input())
    dll = DoublyLinkedLiset()

    for _ in range(n):
        command = input().split(" ")
        if command[0] == "insert":
            dll.insert(command[1])
        elif command[0] == "delete":
            dll.deleteKey(command[1])
        elif command[0] == "deleteFirst":
            dll.deleteFirst()
        elif command[0] == "deleteLast":
            dll.deleteLast()

    dll.printList()