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:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
???????????£????????????
??\?????????????????????????????????????????£???????????????????£??????????????????????

insert x: ??£?????????????????????????????? x ?????????????´?????¶?????¶???????
delete x: ?????? x ??????????????????????´??????£????????????????????????????????????????????????????´?????????¨???????????´????????????????????????
deleteFirst?????????????????????????´???????????????????
deleteLast: ????????????????°????????´???????????????????
"""

class Dlist:
    """ ??????????????????????????? """
    MAX = 1000000
    NoValue = -1
    Prev = 0
    Next = 1
    Value = 2

    def __init__(self):
        """ ????????????????????? """
        self.si = -1
        self.ei = -1
        self.pos = -1
        self.uniqid = 0
        self.cnt = 0
        self.items = []

    def __iter__(self):
        """ ????????¬?????? """
        self.pos = self.si
        return self

    def __next__(self):
        """ ?¬???????????????? """
        if self.cnt == 0: # ???????????????
            raise StopIteration()
        elif self.items[self.pos][Dlist.Next] == Dlist.NoValue: # ?¬??????????
            raise StopIteration()
        else:
            self.pos = self.items[self.pos][Dlist.Next]
        return self.items[self.pos]

 
    def insert(self,x):
        """ ??£?????????????????????????????? x ?????????????´?????¶?????¶???? """

        if self.cnt == Dlist.MAX: # ??????????????????
            return -1

        t = [Dlist.NoValue, Dlist.NoValue, x]

        if self.cnt == 0: # ?????????????´?
            self.ei = self.uniqid
        else:
            s = self.items[self.si]
            s[Dlist.Prev] = self.uniqid
            t[Dlist.Next] = self.si
        self.items.append(t)
        self.si = self.uniqid
        self.uniqid += 1
        self.cnt += 1
        return t
    
    def delete(self,x):
        """ ?????? x ??????????????????????´??????£????????????????????????????????????????????????????´?????????¨???????????´????????????????????????"""

        if self.cnt > 0:
            hit = False
            i = self.si
            while hit == False:
                if i == Dlist.NoValue:
                    break
                if self.items[i][Dlist.Value] == x:
                    hit = True
                    if i == self.si:
                        self.deleteFirst() # ?????????????´??????????
                    elif i == self.ei:
                        self.deleteLast() # ????°????????´??????????
                    else:
                        n = self.items[i][Dlist.Next]
                        p = self.items[i][Dlist.Prev]
                        self.items[n][Dlist.Prev] = p
                        self.items[p][Dlist.Next] = n
                        self.items[i][Dlist.Value] = Dlist.NoValue
                        self.cnt -= 1
                else:
                    i = self.items[i][Dlist.Next]

    def deleteFirst(self):
        """ ?????????????????????????´???????????????? """
        if self.cnt > 0:
            n = self.items[self.si][Dlist.Next]
            if n != Dlist.NoValue:
                self.items[n][Dlist.Prev] = Dlist.NoValue
            self.items[self.si][Dlist.Value] = Dlist.NoValue
            self.si = n
            self.cnt -= 1
            if self.cnt < 1:
                self.ei = Dlist.NoValue

    def deleteLast(self):
        """ ????????????????°????????´???????????????????"""
        if self.cnt > 0:
            p = self.items[self.ei][Dlist.Prev]
            if p != Dlist.NoValue:
                self.items[p][Dlist.Next] = Dlist.NoValue
            self.items[self.ei][Dlist.Value] = Dlist.NoValue
            self.ei = p
            self.cnt -= 1
            if self.cnt < 1:
                self.si = Dlist.NoValue

    def getList(self):
        a = []
        if self.cnt > 0:
            i = self.si
            while i != Dlist.NoValue:
                a.append(self.items[i][Dlist.Value])
                i = self.items[i][Dlist.Next]
        return a

    def debugList(self):
        a = []
        if self.cnt > 0:
            i = self.si
            while i != Dlist.NoValue:
                a.append("{} => {}".format(i,self.items[i][Dlist.Value]))
                i = self.items[i][Dlist.Next]
        return a

def main():
    """ ????????? """
    num = int(input().strip())
    dl = Dlist()

    for i in range(num):
        cmd = list(input().strip().split())
        if cmd[0] == "insert":
            dl.insert(cmd[1])
        elif cmd[0] == "delete":
            dl.delete(cmd[1])
        elif cmd[0] == "deleteFirst":
            dl.deleteFirst()
        elif cmd[0] == "deleteLast":
            dl.deleteLast()

#       print()
#       print(cmd)
#       print("si={} ei={} cnt={}".format(dl.si, dl.ei, dl.cnt))
#       print(dl.debugList())

    print(" ".join(map(str, dl.getList())))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
???????????£????????????
??\?????????????????????????????????????????£???????????????????£??????????????????????

insert x: ??£?????????????????????????????? x ?????????????´?????¶?????¶???????
delete x: ?????? x ??????????????????????´??????£????????????????????????????????????????????????????´?????????¨???????????´????????????????????????
deleteFirst?????????????????????????´???????????????????
deleteLast: ????????????????°????????´???????????????????
"""
import sys

class Dlist:
    """ ??????????????????????????? """
    MAX = 1000000
    NoValue = -1
    Prev = 0
    Next = 1
    Value = 2

    def __init__(self):
        """ ????????????????????? """
        self.si = -1
        self.ei = -1
        self.pos = -1
        self.uniqid = 0
        self.cnt = 0
        self.items = []

    def __iter__(self):
        """ ????????¬?????? """
        self.pos = self.si
        return self

    def __next__(self):
        """ ?¬???????????????? """
        if self.cnt == 0: # ???????????????
            raise StopIteration()
        elif self.items[self.pos][Dlist.Next] == Dlist.NoValue: # ?¬??????????
            raise StopIteration()
        else:
            self.pos = self.items[self.pos][Dlist.Next]
        return self.items[self.pos]

 
    def insert(self,x):
        """ ??£?????????????????????????????? x ?????????????´?????¶?????¶???? """

        if self.cnt == Dlist.MAX: # ??????????????????
            return -1

        t = [Dlist.NoValue, Dlist.NoValue, x]

        if self.cnt == 0: # ?????????????´?
            self.ei = self.uniqid
        else:
            s = self.items[self.si]
            s[Dlist.Prev] = self.uniqid
            t[Dlist.Next] = self.si
        self.items.append(t)
        self.si = self.uniqid
        self.uniqid += 1
        self.cnt += 1
        return t
    
    def delete(self,x):
        """ ?????? x ??????????????????????´??????£????????????????????????????????????????????????????´?????????¨???????????´????????????????????????"""

        if self.cnt > 0:
            hit = False
            i = self.si
            while hit == False:
                if i == Dlist.NoValue:
                    break
                if self.items[i][Dlist.Value] == x:
                    hit = True
                    if i == self.si:
                        self.deleteFirst() # ?????????????´??????????
                    elif i == self.ei:
                        self.deleteLast() # ????°????????´??????????
                    else:
                        n = self.items[i][Dlist.Next]
                        p = self.items[i][Dlist.Prev]
                        self.items[n][Dlist.Prev] = p
                        self.items[p][Dlist.Next] = n
                        self.items[i][Dlist.Value] = Dlist.NoValue
                        self.cnt -= 1
                else:
                    i = self.items[i][Dlist.Next]

    def deleteFirst(self):
        """ ?????????????????????????´???????????????? """
        if self.cnt > 0:
            n = self.items[self.si][Dlist.Next]
            if n != Dlist.NoValue:
                self.items[n][Dlist.Prev] = Dlist.NoValue
            self.items[self.si][Dlist.Value] = Dlist.NoValue
            self.si = n
            self.cnt -= 1
            if self.cnt < 1:
                self.ei = Dlist.NoValue

    def deleteLast(self):
        """ ????????????????°????????´???????????????????"""
        if self.cnt > 0:
            p = self.items[self.ei][Dlist.Prev]
            if p != Dlist.NoValue:
                self.items[p][Dlist.Next] = Dlist.NoValue
            self.items[self.ei][Dlist.Value] = Dlist.NoValue
            self.ei = p
            self.cnt -= 1
            if self.cnt < 1:
                self.si = Dlist.NoValue

    def getList(self):
        a = []
        if self.cnt > 0:
            i = self.si
            while i != Dlist.NoValue:
                a.append(self.items[i][Dlist.Value])
                i = self.items[i][Dlist.Next]
        return a

    def debugList(self):
        a = []
        if self.cnt > 0:
            i = self.si
            while i != Dlist.NoValue:
                a.append("{} => {}".format(i,self.items[i][Dlist.Value]))
                i = self.items[i][Dlist.Next]
        return a

def main():
    """ ????????? """
    num = int(input().strip())
    dl = Dlist()

    istr = sys.stdin.read()
    cmds = list(istr.split("\n"))

    for i in range(num):
        cmd = list(cmds[i].split())
        if cmd[0] == "insert":
            dl.insert(cmd[1])
        elif cmd[0] == "delete":
            dl.delete(cmd[1])
        elif cmd[0] == "deleteFirst":
            dl.deleteFirst()
        elif cmd[0] == "deleteLast":
            dl.deleteLast()

#       print()
#       print(cmd)
#       print("si={} ei={} cnt={}".format(dl.si, dl.ei, dl.cnt))
#       print(dl.debugList())

    print(" ".join(map(str, dl.getList())))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
???????????£????????????
??\?????????????????????????????????????????£???????????????????£??????????????????????

insert x: ??£?????????????????????????????? x ?????????????´?????¶?????¶???????
delete x: ?????? x ??????????????????????´??????£????????????????????????????????????????????????????´?????????¨???????????´????????????????????????
deleteFirst?????????????????????????´???????????????????
deleteLast: ????????????????°????????´???????????????????
"""
import sys

class Dlist:
    """ ??????????????????????????? """
    MAX = 1000000
    NoValue = -1
    Prev = 0
    Next = 1
    Value = 2

    def __init__(self):
        """ ????????????????????? """
        self.clear()

    def __iter__(self):
        """ ????????¬?????? """
        self.pos = self.si
        return self

    def __next__(self):
        """ ?¬???????????????? """
        if self.cnt == 0: # ???????????????
            raise StopIteration()
        elif self.items[self.pos][Dlist.Next] == Dlist.NoValue: # ?¬??????????
            raise StopIteration()
        else:
            self.pos = self.items[self.pos][Dlist.Next]
        return self.items[self.pos]


    def clear(self):
        """ ????´?????????????????????¢ """
        self.si = -1
        self.ei = -1
        self.pos = -1
        self.uniqid = 0
        self.cnt = 0
        self.items = []
        
    def insert(self,x):
        """ ??£?????????????????????????????? x ?????????????´?????¶?????¶???? """

        if self.cnt == Dlist.MAX: # ??????????????????
            return -1

        t = [Dlist.NoValue, Dlist.NoValue, x]

        if self.cnt == 0: # ?????????????´?
            self.ei = self.uniqid
        else:
            s = self.items[self.si]
            s[Dlist.Prev] = self.uniqid
            t[Dlist.Next] = self.si
        self.items.append(t)
        self.si = self.uniqid
        self.uniqid += 1
        self.cnt += 1
        return t
    
    def delete(self,x):
        """ ?????? x ??????????????????????´??????£????????????????????????????????????????????????????´?????????¨???????????´????????????????????????"""

        if self.cnt > 0:
            hit = False
            i = self.si
            while hit == False:
                if i == Dlist.NoValue:
                    break
                if self.items[i][Dlist.Value] == x:
                    hit = True
                    if i == self.si:
                        self.deleteFirst() # ?????????????´??????????
                    elif i == self.ei:
                        self.deleteLast() # ????°????????´??????????
                    else:
                        n = self.items[i][Dlist.Next]
                        p = self.items[i][Dlist.Prev]
                        self.items[n][Dlist.Prev] = p
                        self.items[p][Dlist.Next] = n
                        self.items[i][Dlist.Value] = Dlist.NoValue
                        self.cnt -= 1
                else:
                    i = self.items[i][Dlist.Next]

    def deleteFirst(self):
        """ ?????????????????????????´???????????????? """
        if self.cnt < 2:
            self.clear()
        else:
            n = self.items[self.si][Dlist.Next]
            if n != Dlist.NoValue:
                self.items[n][Dlist.Prev] = Dlist.NoValue
            self.items[self.si][Dlist.Value] = Dlist.NoValue
            self.si = n
            self.cnt -= 1

    def deleteLast(self):
        """ ????????????????°????????´???????????????????"""
        if self.cnt < 2:
            self.clear()
        else:
            p = self.items[self.ei][Dlist.Prev]
            if p != Dlist.NoValue:
                self.items[p][Dlist.Next] = Dlist.NoValue
            self.items[self.ei][Dlist.Value] = Dlist.NoValue
            self.ei = p
            self.cnt -= 1

    def getList(self):
        a = []
        if self.cnt > 0:
            i = self.si
            while i != Dlist.NoValue:
                a.append(self.items[i][Dlist.Value])
                i = self.items[i][Dlist.Next]
        return a

def main():
    """ ????????? """
    num = int(input().strip())
    dl = Dlist()

    istr = sys.stdin.read()
    cmds = list(istr.split("\n"))

    for i in range(num):
        cmd = list(cmds[i].split())
        if cmd[0] == "insert":
            dl.insert(cmd[1])
        elif cmd[0] == "delete":
            dl.delete(cmd[1])
        elif cmd[0] == "deleteFirst":
            dl.deleteFirst()
        elif cmd[0] == "deleteLast":
            dl.deleteLast()

#       print()
#       print(cmd)
#       print("si={} ei={} cnt={}".format(dl.si, dl.ei, dl.cnt))
#       print(dl.debugList())

    print(" ".join(dl.getList()))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

for i in range(n):
	command = input().split()
	if len(command) == 2:
		command,x = command
	else:
		command = command[0]
	if command == 'insert':
		data.insert(0,x)
	elif command == 'delete':
		if x in data:
			data.remove(x)
	elif command == 'deleteFirst':
		data.pop(0)
	elif command == 'deleteLast':
		data.pop()

print(*data)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
???????????£????????????
??\?????????????????????????????????????????£???????????????????£??????????????????????

insert x: ??£?????????????????????????????? x ?????????????´?????¶?????¶???????
delete x: ?????? x ??????????????????????´??????£????????????????????????????????????????????????????´?????????¨???????????´????????????????????????
deleteFirst?????????????????????????´???????????????????
deleteLast: ????????????????°????????´???????????????????
"""
import sys

class Dlist:
    """ ??????????????????????????? """
    MAX = 2000000
    NoValue = -1
    Prev = 0
    Next = 1
    Value = 2

    def __init__(self):
        """ ????????????????????? """
        self.items = [0] * Dlist.MAX
        self.clear()

    def clear(self):
        """ ????´?????????????????????¢ """
        self.si = -1
        self.ei = -1
        self.pos = -1
        self.uniqid = 0
        self.cnt = 0
        
    def insert(self,x):
        """ ??£?????????????????????????????? x ?????????????´?????¶?????¶???? """

        t = [Dlist.NoValue, Dlist.NoValue, x]

        if self.cnt == 0: # ?????????????´?
            self.ei = self.uniqid
        else:
            s = self.items[self.si]
            s[Dlist.Prev] = self.uniqid
            t[Dlist.Next] = self.si
        self.items[self.uniqid] = t
        self.si = self.uniqid
        self.uniqid += 1
        self.cnt += 1
    
    def delete(self,x):
        """ ?????? x ??????????????????????´??????£????????????????????????????????????????????????????´?????????¨???????????´????????????????????????"""

        if self.cnt > 0:
            hit = False
            i = self.si
            while hit == False:
                if i == Dlist.NoValue:
                    break
                if self.items[i][Dlist.Value] == x:
                    hit = True
                    if i == self.si:
                        self.deleteFirst() # ?????????????´??????????
                    elif i == self.ei:
                        self.deleteLast() # ????°????????´??????????
                    else:
                        n = self.items[i][Dlist.Next]
                        p = self.items[i][Dlist.Prev]
                        self.items[n][Dlist.Prev] = p
                        self.items[p][Dlist.Next] = n
                        self.items[i][Dlist.Value] = Dlist.NoValue
                        self.cnt -= 1
                else:
                    i = self.items[i][Dlist.Next]

    def deleteFirst(self):
        """ ?????????????????????????´???????????????? """
        if self.cnt < 2:
            self.clear()
        else:
            n = self.items[self.si][Dlist.Next]
            if n != Dlist.NoValue:
                self.items[n][Dlist.Prev] = Dlist.NoValue
            self.items[self.si][Dlist.Value] = Dlist.NoValue
            self.si = n
            self.cnt -= 1

    def deleteLast(self):
        """ ????????????????°????????´???????????????????"""
        if self.cnt < 2:
            self.clear()
        else:
            p = self.items[self.ei][Dlist.Prev]
            if p != Dlist.NoValue:
                self.items[p][Dlist.Next] = Dlist.NoValue
            self.items[self.ei][Dlist.Value] = Dlist.NoValue
            self.ei = p
            self.cnt -= 1

    def getList(self):
        a = []
        if self.cnt > 0:
            i = self.si
            while i != Dlist.NoValue:
                a.append(self.items[i][Dlist.Value])
                i = self.items[i][Dlist.Next]
        return a

def main():
    """ ????????? """
    num = int(input().strip())
    istr = sys.stdin.read()
    cmds = list(istr.split("\n"))
    dl = Dlist()

    for i in range(num):
        cmd = list(cmds[i].split())
        if cmd[0] == "insert":
            dl.insert(cmd[1])
        elif cmd[0] == "delete":
            dl.delete(cmd[1])
        elif cmd[0] == "deleteFirst":
            dl.deleteFirst()
        elif cmd[0] == "deleteLast":
            dl.deleteLast()

#       print()
#       print(cmd)
#       print("si={} ei={} cnt={}".format(dl.si, dl.ei, dl.cnt))
#       print(dl.debugList())

    print(" ".join(dl.getList()))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
???????????£????????????
??\?????????????????????????????????????????£???????????????????£??????????????????????

insert x: ??£?????????????????????????????? x ?????????????´?????¶?????¶???????
delete x: ?????? x ??????????????????????´??????£????????????????????????????????????????????????????´?????????¨???????????´????????????????????????
deleteFirst?????????????????????????´???????????????????
deleteLast: ????????????????°????????´???????????????????
"""
import sys

class Dlist:
    """ ??????????????????????????? """
    MAX = 2000000
    NoValue = -1
    Prev = 0
    Next = 1
    Value = 2

    def __init__(self):
        """ ????????????????????? """
        self.items = [0] * Dlist.MAX
        self.clear()

    def clear(self):
        """ ????´?????????????????????¢ """
        self.si = -1
        self.ei = -1
        self.pos = -1
        self.uniqid = 0
        self.cnt = 0
        
    def insert(self,x):
        """ ??£?????????????????????????????? x ?????????????´?????¶?????¶???? """

        t = [Dlist.NoValue, Dlist.NoValue, x]

        if self.cnt == 0: # ?????????????´?
            self.ei = self.uniqid
        else:
            s = self.items[self.si]
            s[Dlist.Prev] = self.uniqid
            t[Dlist.Next] = self.si
        self.items[self.uniqid] = t
        self.si = self.uniqid
        self.uniqid += 1
        self.cnt += 1
    
    def delete(self,x):
        """ ?????? x ??????????????????????´??????£????????????????????????????????????????????????????´?????????¨???????????´????????????????????????"""

        if self.cnt > 0:
            hit = False
            i = self.si
            while hit == False:
                if i == Dlist.NoValue:
                    break
                if self.items[i][Dlist.Value] == x:
                    hit = True
                    if i == self.si:
                        self.deleteFirst() # ?????????????´??????????
                    elif i == self.ei:
                        self.deleteLast() # ????°????????´??????????
                    else:
                        n = self.items[i][Dlist.Next]
                        p = self.items[i][Dlist.Prev]
                        self.items[n][Dlist.Prev] = p
                        self.items[p][Dlist.Next] = n
#                        self.items[i][Dlist.Value] = Dlist.NoValue
                        self.cnt -= 1
                else:
                    i = self.items[i][Dlist.Next]

    def deleteFirst(self):
        """ ?????????????????????????´???????????????? """
        if self.cnt < 2:
            self.clear()
        else:
            n = self.items[self.si][Dlist.Next]
            if n != Dlist.NoValue:
                self.items[n][Dlist.Prev] = Dlist.NoValue
#            self.items[self.si][Dlist.Value] = Dlist.NoValue
            self.si = n
            self.cnt -= 1

    def deleteLast(self):
        """ ????????????????°????????´???????????????????"""
        if self.cnt < 2:
            self.clear()
        else:
            p = self.items[self.ei][Dlist.Prev]
            if p != Dlist.NoValue:
                self.items[p][Dlist.Next] = Dlist.NoValue
#            self.items[self.ei][Dlist.Value] = Dlist.NoValue
            self.ei = p
            self.cnt -= 1

    def getList(self):
        a = []
        if self.cnt > 0:
            i = self.si
            while i != Dlist.NoValue:
                a.append(self.items[i][Dlist.Value])
                i = self.items[i][Dlist.Next]
        return a

def main():
    """ ????????? """
    num = int(input().strip())
    istr = sys.stdin.read()
    cmds = list(istr.split("\n"))
    dl = Dlist()

    for i in range(num):
        cmd = list(cmds[i].split())
        if cmd[0] == "insert":
            dl.insert(cmd[1])
        elif cmd[0] == "delete":
            dl.delete(cmd[1])
        elif cmd[0] == "deleteFirst":
            dl.deleteFirst()
        elif cmd[0] == "deleteLast":
            dl.deleteLast()

#       print()
#       print(cmd)
#       print("si={} ei={} cnt={}".format(dl.si, dl.ei, dl.cnt))
#       print(dl.debugList())

    print(" ".join(dl.getList()))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())

dl_list=[]

for i in range(n):
    cmd=input().split()
    if cmd[0]=="insert":
        dl_list.insert(0,cmd[1])
    elif cmd[0]=="delete":
        try:
            dl_list.remove(cmd[1])
        except:
            pass
    elif cmd[0]=="deleteFirst":
        del dl_list[0]
    elif cmd[0]=="deleteLast":
        del dl_list[-1]

print(*dl_list)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())

dl_list=[]

for i in range(n):
    cmd=input().split()
    if cmd[0]=="insert":
        dl_list.insert(0,cmd[1])
    elif cmd[0]=="delete":
        try:
            dl_list.remove(cmd[1])
        except:
            pass
    elif cmd[0]=="deleteFirst":
        del dl_list[0]
    elif cmd[0]=="deleteLast":
        del dl_list[-1]

print(" ".join(dl_list))

Submission status: Time Limit Exceeded

Solution code:

class DoublyLinkedList:
    def insert(self, L, key):
        L.insert(0, key)
    
    def delete(self, L, key):
        if key in L:
            idx = L.index(key)
            L.pop(idx)
    
    def deleteFirst(self, L):
        L.pop(0)
    
    def deleteLast(self, L):
        L.pop()

N = int(input())
C = []
for i in range(N):
    C.append(input().split())

DLL = DoublyLinkedList()
L = []
for i in C:
    if i[0] == "insert":
        DLL.insert(L, int(i[1]))
    elif i[0] == "delete":
        DLL.delete(L, int(i[1]))
    elif i[0] == "deleteFirst":
        DLL.deleteFirst(L)
    elif i[0] == "deleteLast":
        DLL.deleteLast(L)
    else:
        raise ValueError("Wrong Command")

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

Submission status: Time Limit Exceeded

Solution code:

class DoublyLinkedList:
    def insert(self, L, key):
        L.insert(0, key)
    
    def delete(self, L, key):
        if key in L:
            idx = L.index(key)
            L.pop(idx)
        else:
            pass
    
    def deleteFirst(self, L):
        L.pop(0)
    
    def deleteLast(self, L):
        L.pop()

N = int(input())
C = []
for i in range(N):
    C.append(input().split())

DLL = DoublyLinkedList()
L = []
for i in C:
    if i[0] == "insert":
        DLL.insert(L, int(i[1]))
    elif i[0] == "delete":
        DLL.delete(L, int(i[1]))
    elif i[0] == "deleteFirst":
        DLL.deleteFirst(L)
    elif i[0] == "deleteLast":
        DLL.deleteLast(L)
    else:
        raise ValueError("Wrong Command")

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

Submission status: Time Limit Exceeded

Solution code:
class DoublyLinkedList:
    def insert(self, L, key):
        L.insert(0, key)
    
    def delete(self, L, key):
        if key in L:
            idx = L.index(key)
            L.pop(idx)
        else:
            pass
    
    def deleteFirst(self, L):
        L.pop(0)
    
    def deleteLast(self, L):
        L.pop()

N = int(input())
C = []
for i in range(N):
    C.append(input().split())

DLL = DoublyLinkedList()
L = []
for i in C:
    if i[0] == "insert":
        DLL.insert(L, int(i[1]))
    elif i[0] == "delete":
        DLL.delete(L, int(i[1]))
    elif i[0] == "deleteFirst":
        DLL.deleteFirst(L)
    elif i[0] == "deleteLast":
        DLL.deleteLast(L)
    else:
        raise ValueError("Wrong Command")

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

class cell:
    def __init__(self, data, before, _next):
        self.data = data
        self.before = before
        self._next = _next

def search(ddl, data):
    for ind,item in enumerate(ddl):
        if item.data == data:
            return ind
    return None

def getItem(ddl, index):
    if len(ddl) > index and index >= 0:
        return ddl[index]
    return None

ddl = []
for i in range(n):
    inp = input().split()
    if len(inp) == 2:
        op, data = inp
        data = int(data)
    else: op, data = inp[0], None
    if op == 'insert':
        if len(ddl) == 0:
            _next = None
        else:
            _next = ddl[0]
        c = cell(data, None, _next)
        ddl = [c] + ddl
    elif op == 'delete':
        ind = search(ddl, data)
        if ind != None:
            ddl[ind - 1].next = getItem(ddl, ind + 1)
            if getItem(ddl, ind+1) is not None:
                ddl[ind + 1].prev = getItem(ddl, ind - 1)
            ddl = ddl[:ind] + ddl[ind+1:]
    elif op == 'deleteFirst':
        ddl = ddl[1:]
        if len(ddl) != 0:
            ddl[0].prev = None
    elif op == 'deleteLast':
        ddl = ddl[:-1]
        if len(ddl) != 0:
         ddl[-1]._next = None

print(' '.join([str(item.data) for item in ddl]))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
n = int(input())
deq = deque()
sub_deq = {}
for i in range(n):
    cmd = input()
    if cmd == 'deleteFirst':
        while deq:
            data = deq.popleft()
            if data[1]:
                data[1] = 0
                break
    elif cmd == 'deleteLast':
        while deq:
            data = deq.pop()
            if data[1]:
                data[1] = 0
                break
    else:
        cmd, val = cmd.split()
        if cmd == 'insert':
            data = [val, 1]
            deq.appendleft(data)
            if val not in sub_deq:
                sub_deq[val] = deque()
            sub_deq[val].appendleft(data)
        elif cmd == 'delete':
            if val in sub_deq:
                deq_v = sub_deq[val]
                while deq_v:
                    data = deq_v.popleft()
                    if data[1]:
                        data[1] = 0
                        break
print(*(data[0] for data in deq if data[1]))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
n = int(input())
deq = deque()
sub_deq = {}
for i in range(n):
    cmd = input()
    if cmd == 'deleteFirst':
        while deq:
            data = deq.pop()
            if data[1]:
                data[1] = 0
                break
    elif cmd == 'deleteLast':
        while deq:
            data = deq.popleft()
            if data[1]:
                data[1] = 0
                break
    else:
        cmd, val = cmd.split()
        if cmd == 'insert':
            data = [val, 1]
            deq.append(data)
            if val not in sub_deq:
                sub_deq[val] = [data]
            else:
                sub_deq[val].append(data)
        elif cmd == 'delete':
            if val in sub_deq:
                deq_v = sub_deq[val]
                while deq_v:
                    data = deq_v.pop()
                    if data[1]:
                        data[1] = 0
                        break
print(*(data[0] for data in reversed(deq) if data[1]))