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 LinkedList(object):
    
    def __init__(self):
        self.nodes = []

    def insert(self, node):
        self.nodes.insert(0, node)

    def delete(self, node):
        if node in self.nodes:
            self.nodes.remove(node)

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

    def deleteLast(self):
        self.nodes.pop()

def run():
    n = int(input())
    linked_list = LinkedList()
    orders = [input().split() for _ in range(n)]
    for order in orders:
        if order[0] == 'insert':
            linked_list.insert(int(order[1]))
        elif order[0] == 'delete':
            linked_list.delete(int(order[1]))
        elif order[0] == 'deleteFirst':
            linked_list.deleteFirst()
        elif order[0] == 'deleteLast':
            linked_list.deleteLast()
    print(' '.join([str(n) for n in linked_list.nodes]))
    
if __name__ == '__main__':
    run()

Submission status: Time Limit Exceeded

Solution code:
def run():
    n = int(input())
    nodes = []
    for _ in range(n):
        order = [o.strip() for o in input().split()]
        if order[0] == 'insert':
            nodes.insert(0, (int(order[1])))
        elif order[0] == 'delete':
            if int(order[1]) in nodes:
                nodes.remove(int(order[1]))
        elif order[0] == 'deleteFirst':
            nodes.pop(0)
        elif order[0] == 'deleteLast':
            nodes.pop()
    print(' '.join([str(n) for n in nodes]))
    
if __name__ == '__main__':
    run()

Submission status: Time Limit Exceeded

Solution code:
def run():
    n = int(input())
    nodes = []
    for _ in range(n):
        order = [o.strip() for o in input().split()]
        if order[0] == 'insert':
            nodes.append(int(order[1]))
        elif order[0] == 'delete':
            nodes.reverse()
            if int(order[1]) in nodes:
                nodes.remove(int(order[1]))
            nodes.reverse()
        elif order[0] == 'deleteFirst':
            nodes.pop()
        elif order[0] == 'deleteLast':
            nodes.pop(0)
    nodes.reverse()
    print(' '.join([str(n) for n in nodes]))
    
if __name__ == '__main__':
    run()

Submission status: Time Limit Exceeded

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

    def __init__(self):
        self.head = DList.Cell(None)
        self.last = DList.Cell(None)
        self.head.next = self.last
        self.last.prev = self.head

    def insert(self, x):
        c = DList.Cell(x)
        c.prev = self.head
        c.next = self.head.next
        c.next.prev = c
        self.head.next = c

    def delete(self, x):
        c = self.__find(x)
        if c != None:
            self.__delete(c)

    def __delete(self, c):
        c.prev.next = c.next
        c.next.prev = c.prev

    def __find(self, x):
        c = self.head.next
        while c != None and c.key != x:
            c = c.next
        return c

    def deleteFirst(self):
        self.__delete(self.head.next)
    def deleteLast(self):
        self.__delete(self.last.prev)

    def print(self):
        c = self.head.next
        sep = ''
        while c != self.last:
            print(sep, end='')
            print(c.key, end='')
            c = c.next
            sep = ' '
        print()

dlist = DList()

n = int(input())
for i in range(n):
    cmd = tuple(input().split())
    if   cmd[0] == 'insert':
        dlist.insert(int(cmd[1]))
    elif cmd[0] == 'delete':
        dlist.delete(int(cmd[1]))
    elif cmd[0] == 'deleteFirst':
        dlist.deleteFirst()
    elif cmd[0] == 'deleteLast':
        dlist.deleteLast()

dlist.print()

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

class DList:
    class Cell:
        def __init__(self, k):
            self.key  = k
            self.prev = None
            self.next = None

    def __init__(self):
        self.head = DList.Cell(None)
        self.last = DList.Cell(None)
        self.head.next = self.last
        self.last.prev = self.head

    def insert(self, x):
        c = DList.Cell(x)
        c.prev = self.head
        c.next = self.head.next
        c.next.prev = c
        self.head.next = c

    def delete(self, x):
        c = self.__find(x)
        if c != None:
            self.__delete(c)

    def __delete(self, c):
        c.prev.next = c.next
        c.next.prev = c.prev

    def __find(self, x):
        c = self.head.next
        while c != None and c.key != x:
            c = c.next
        return c

    def deleteFirst(self):
        self.__delete(self.head.next)
    def deleteLast(self):
        self.__delete(self.last.prev)

    def print(self):
        c = self.head.next
        sep = ''
        while c != self.last:
            print(sep, end='')
            print(c.key, end='')
            c = c.next
            sep = ' '
        print()

dlist = DList()

n = int(stdin.readline())
for i in range(n):
    cmd = tuple(stdin.readline().split())
    if   cmd[0] == 'insert':
        dlist.insert(int(cmd[1]))
    elif cmd[0] == 'delete':
        dlist.delete(int(cmd[1]))
    elif cmd[0] == 'deleteFirst':
        dlist.deleteFirst()
    elif cmd[0] == 'deleteLast':
        dlist.deleteLast()

dlist.print()

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

class DList:
    class Cell:
        def __init__(self, k):
            self.key  = k
            self.prev = None
            self.next = None

    def __init__(self):
        self.head = DList.Cell(None)
        self.last = DList.Cell(None)
        self.head.next = self.last
        self.last.prev = self.head

    def insert(self, x):
        c = DList.Cell(x)
        c.prev = self.head
        c.next = self.head.next
        c.next.prev = c
        self.head.next = c

    def delete(self, x):
        c = self.__find(x)
        if c != None:
            self.__delete(c)

    def __delete(self, c):
        c.prev.next = c.next
        c.next.prev = c.prev

    def __find(self, x):
        c = self.head.next
        while c != None and c.key != x:
            c = c.next
        return c

    def deleteFirst(self):
        self.__delete(self.head.next)
    def deleteLast(self):
        self.__delete(self.last.prev)

    def print(self):
        c = self.head.next
        sep = ''
        while c != self.last:
            print(sep + str(c.key), end='')
            c = c.next
            sep = ' '
        print()

dlist = DList()

n = int(stdin.readline())
for i in range(n):
    cmd = tuple(stdin.readline().split())
    if   cmd[0] == 'insert':
        dlist.insert(int(cmd[1]))
    elif cmd[0] == 'delete':
        dlist.delete(int(cmd[1]))
    elif cmd[0] == 'deleteFirst':
        dlist.deleteFirst()
    elif cmd[0] == 'deleteLast':
        dlist.deleteLast()

dlist.print()

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

class DList:
    class Cell:
        def __init__(self, k):
            self.key  = k
            self.prev = None
            self.next = None

    def __init__(self):
        self.head = DList.Cell(None)
        self.last = DList.Cell(None)
        self.head.next = self.last
        self.last.prev = self.head

    def insert(self, x):
        c = DList.Cell(x)
        c.prev = self.head
        c.next = self.head.next
        c.next.prev = c
        self.head.next = c

    def delete(self, x):
        c = self.__find(x)
        if c != None:
            self.__delete(c)

    def __delete(self, c):
        c.prev.next = c.next
        c.next.prev = c.prev

    def __find(self, x):
        c = self.head.next
        while c != None and c.key != x:
            c = c.next
        return c

    def deleteFirst(self):
        self.__delete(self.head.next)
    def deleteLast(self):
        self.__delete(self.last.prev)

    def print(self):
        c = self.head.next
        ls = []
        while c != self.last:
            ls.append(c.key)
            c = c.next
        print(' '.join(map(str, ls)))

dlist = DList()

n = int(stdin.readline())
for i in range(n):
    cmd = tuple(stdin.readline().split())
    if   cmd[0] == 'insert':
        dlist.insert(int(cmd[1]))
    elif cmd[0] == 'delete':
        dlist.delete(int(cmd[1]))
    elif cmd[0] == 'deleteFirst':
        dlist.deleteFirst()
    elif cmd[0] == 'deleteLast':
        dlist.deleteLast()

dlist.print()

Submission status: Time Limit Exceeded

Solution code:
class LinkedList(object):
    def __init__(self):
        self.nodes = []
        self.first = None
        self.last = None

    def insert(self, key):
        node = {'key':key, 'next':None, 'prev':None}
        if not self.nodes:
            self.first = self.last = node
            self.nodes.append(node)
        else:
            node['next'] = self.first
            self.first['prev'] = node
            self.first = node
            self.nodes.append(node)

    def delete(self, key):
        node = self.first
        if len(self.nodes) == 1 and node['key'] == key:
            self.first = self.last = None
            self.nodes = []
            return
        if self.first['key'] == key:
            self.deleteFirst()
            return
        while node['next']:
            if node['key'] == key:
                node['prev']['next'] = node['next']
                node['next']['prev'] = node['prev']
                self.nodes.remove(node)
                return
            node = node['next']
        if self.last['key'] == key:
            self.deleteLast()

    def deleteFirst(self):
        if len(self.nodes) == 1:
            self.first = self.last = None
            self.nodes = []
        else:
            self.first['next']['prev'] = None
            self.nodes.remove(self.first)
            self.first = self.first['next']

    def deleteLast(self):
        if len(self.nodes) == 1:
            self.first = self.last = None
            self.nodes = []
        else:
            self.last['prev']['next'] = None
            self.nodes.remove(self.last)
            self.last = self.last['prev']

    def show(self):
        if not self.nodes:
            print('')
            return
        node = self.first
        res = [node['key']]
        while node['next']:
            node = node['next']
            res.append(node['key'])
        print(' '.join(res))

def run():
    n = int(input())
    linked_list = LinkedList()
    for _ in range(n):
        order = [o.strip() for o in input().split()]
        if order[0] == 'insert':
            linked_list.insert(order[1])
        elif order[0] == 'delete':
            linked_list.delete(order[1])
        elif order[0] == 'deleteFirst':
            linked_list.deleteFirst()
        elif order[0] == 'deleteLast':
            linked_list.deleteLast()
    linked_list.show()

if __name__ == '__main__':
    run()

Submission status: Time Limit Exceeded

Solution code:
class LinkedList(object):
    def __init__(self):
        self.first = None
        self.last = None

    def insert(self, key):
        node = {'key':key, 'next':None, 'prev':None}
        if not self.first:
            self.first = self.last = node
        else:
            node['next'] = self.first
            self.first['prev'] = node
            self.first = node

    def delete(self, key):
        node = self.first
        if self.first['key'] == key:
            self.deleteFirst()
            return
        while node['next']:
            if node['key'] == key:
                node['prev']['next'] = node['next']
                node['next']['prev'] = node['prev']
                return
            node = node['next']
        if self.last['key'] == key:
            self.deleteLast()

    def deleteFirst(self):
        if self.first == self.last:
            self.first = self.last = None
        else:
            self.first['next']['prev'] = None
            self.first = self.first['next']

    def deleteLast(self):
        if self.first == self.last:
            self.first = self.last = None
        else:
            self.last['prev']['next'] = None
            self.last = self.last['prev']

    def show(self):
        if not self.first:
            print('')
            return
        node = self.first
        res = [node['key']]
        while node['next']:
            node = node['next']
            res.append(node['key'])
        print(' '.join(res))

def run():
    n = int(input())
    linked_list = LinkedList()
    for _ in range(n):
        order = [o.strip() for o in input().split()]
        if order[0] == 'insert':
            linked_list.insert(order[1])
        elif order[0] == 'delete':
            linked_list.delete(order[1])
        elif order[0] == 'deleteFirst':
            linked_list.deleteFirst()
        elif order[0] == 'deleteLast':
            linked_list.deleteLast()
    linked_list.show()

if __name__ == '__main__':
    run()

Submission status: Time Limit Exceeded

Solution code:
class LinkedList(object):
    def __init__(self):
        self.first = None
        self.last = None

    def insert(self, key):
        node = {'key':key, 'next':None, 'prev':None}
        if not self.first:
            self.first = self.last = node
        else:
            node['next'] = self.first
            self.first['prev'] = node
            self.first = node

    def delete(self, key):
        node = self.first
        if self.first['key'] == key:
            self.deleteFirst()
            return
        while node['next']:
            if node['key'] == key:
                node['prev']['next'] = node['next']
                node['next']['prev'] = node['prev']
                return
            node = node['next']
        if self.last['key'] == key:
            self.deleteLast()

    def deleteFirst(self):
        if self.first == self.last:
            self.first = self.last = None
        else:
            self.first['next']['prev'] = None
            self.first = self.first['next']

    def deleteLast(self):
        if self.first == self.last:
            self.first = self.last = None
        else:
            self.last['prev']['next'] = None
            self.last = self.last['prev']

    def show(self):
        if not self.first:
            print('')
            return
        node = self.first
        res = [node['key']]
        while node['next']:
            node = node['next']
            res.append(node['key'])
        print(' '.join(res))

def run():
    n = int(input())
    linked_list = LinkedList()
    for _ in range(n):
        order = input().split()
        if order[0] == 'insert':
            linked_list.insert(order[1])
        elif order[0] == 'delete':
            linked_list.delete(order[1])
        elif order[0] == 'deleteFirst':
            linked_list.deleteFirst()
        elif order[0] == 'deleteLast':
            linked_list.deleteLast()
    linked_list.show()

if __name__ == '__main__':
    run()

Submission status: Time Limit Exceeded

Solution code:
class LinkedList(object):
    def __init__(self):
        self.first = None
        self.last = None

    def insert(self, key):
        node = {'key':key, 'next':None, 'prev':None}
        if not self.first:
            self.first = self.last = node
        else:
            node['next'] = self.first
            self.first['prev'] = node
            self.first = node

    def delete(self, key):
        node = self.first
        if self.first['key'] == key:
            self.deleteFirst()
            return
        while node['next']:
            if node['key'] == key:
                node['prev']['next'] = node['next']
                node['next']['prev'] = node['prev']
                return
            node = node['next']
        if self.last['key'] == key:
            self.deleteLast()

    def deleteFirst(self):
        if self.first == self.last:
            self.first = self.last = None
        else:
            self.first['next']['prev'] = None
            self.first = self.first['next']

    def deleteLast(self):
        if self.first == self.last:
            self.first = self.last = None
        else:
            self.last['prev']['next'] = None
            self.last = self.last['prev']

    def show(self):
        if not self.first:
            print('')
            return
        node = self.first
        res = [node['key']]
        while node['next']:
            node = node['next']
            res.append(node['key'])
        print(' '.join(res))

def run():
    n = int(input())
    linked_list = LinkedList()
    for _ in range(n):
        order = input()
        if order.startswith('insert'):
            linked_list.insert(order.split()[1])
        elif order.startswith('deleteFirst'):
            linked_list.deleteFirst()
        elif order.startswith('deleteLast'):
            linked_list.deleteLast()
        elif order.startswith('delete'):
            linked_list.delete(order.split()[1])
    linked_list.show()

if __name__ == '__main__':
    run()

Submission status: Time Limit Exceeded

Solution code:
class LinkedList(object):
    def __init__(self):
        self.first = None
        self.last = None

    def insert(self, key):
        if not self.first:
            self.first = self.last = {'key':key, 'next':None, 'prev':None}
        else:
            self.first = {'key':key, 'next':self.first, 'prev':None}
            self.first['next']['prev'] = self.first

    def delete(self, key):
        node = self.first
        if self.first['key'] == key:
            self.deleteFirst()
            return
        while node['next']:
            if node['key'] == key:
                node['prev']['next'] = node['next']
                node['next']['prev'] = node['prev']
                return
            node = node['next']
        if self.last['key'] == key:
            self.deleteLast()

    def deleteFirst(self):
        if self.first == self.last:
            self.first = self.last = None
        else:
            self.first['next']['prev'] = None
            self.first = self.first['next']

    def deleteLast(self):
        if self.first == self.last:
            self.first = self.last = None
        else:
            self.last['prev']['next'] = None
            self.last = self.last['prev']

    def show(self):
        if not self.first:
            print('')
            return
        node = self.first
        res = [node['key']]
        while node['next']:
            node = node['next']
            res.append(node['key'])
        print(' '.join(res))

def run():
    n = int(input())
    linked_list = LinkedList()
    for _ in range(n):
        order = input()
        if order.startswith('insert'):
            linked_list.insert(order.split()[1])
        elif order.startswith('deleteFirst'):
            linked_list.deleteFirst()
        elif order.startswith('deleteLast'):
            linked_list.deleteLast()
        elif order.startswith('delete'):
            linked_list.delete(order.split()[1])
    linked_list.show()

if __name__ == '__main__':
    run()

Submission status: Time Limit Exceeded

Solution code:
class LinkedList(object):
    def __init__(self):
        self.first = None
        self.last = None

    def insert(self, key):
        if not self.first:
            self.first = self.last = {'key':key, 'next':None, 'prev':None}
        else:
            self.first = {'key':key, 'next':self.first, 'prev':None}
            self.first['next']['prev'] = self.first

    def delete(self, key):
        node = self.first
        if self.first['key'] == key:
            self.deleteFirst()
            return
        while node['next']:
            if node['key'] == key:
                node['prev']['next'] = node['next']
                node['next']['prev'] = node['prev']
                return
            node = node['next']
        if self.last['key'] == key:
            self.deleteLast()

    def deleteFirst(self):
        if self.first == self.last:
            self.first = self.last = None
        else:
            self.first = self.first['next']
            self.first['prev'] = None

    def deleteLast(self):
        if self.first == self.last:
            self.first = self.last = None
        else:
            self.last = self.last['prev']
            self.last['next'] = None

    def show(self):
        if not self.first:
            print('')
            return
        node = self.first
        res = [node['key']]
        while node['next']:
            node = node['next']
            res.append(node['key'])
        print(' '.join(res))

def run():
    n = int(input())
    linked_list = LinkedList()
    for _ in range(n):
        order = input()
        if order.startswith('insert'):
            linked_list.insert(order.split()[1])
        elif order.startswith('deleteFirst'):
            linked_list.deleteFirst()
        elif order.startswith('deleteLast'):
            linked_list.deleteLast()
        elif order.startswith('delete'):
            linked_list.delete(order.split()[1])
    linked_list.show()

if __name__ == '__main__':
    run()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

class LinkedList(object):
    def __init__(self):
        self.dq = deque()

    def insert(self, key):
        self.dq.appendleft(key)

    def delete(self, key):
        for i in self.dq:
            if i == key:
                self.dq.remove(key)
                return

    def deleteFirst(self):
        self.dq.popleft()

    def deleteLast(self):
        self.dq.pop()

    def show(self):
        print(' '.join(self.dq))

def run():
    n = int(input())
    linked_list = LinkedList()
    for _ in range(n):
        order = input()
        if order.startswith('insert'):
            linked_list.insert(order.split()[1])
        elif order.startswith('deleteFirst'):
            linked_list.deleteFirst()
        elif order.startswith('deleteLast'):
            linked_list.deleteLast()
        elif order.startswith('delete'):
            linked_list.delete(order.split()[1])
    linked_list.show()

if __name__ == '__main__':
    run()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

class LinkedList(object):
    def __init__(self):
        self.dq = deque()

    def insert(self, key):
        self.dq.appendleft(key)

    def delete(self, key):
        if not key in self.dq:
            return
        for x in self.dq:
            if x == key:
                self.dq.remove(key)
                return

    def deleteFirst(self):
        self.dq.popleft()

    def deleteLast(self):
        self.dq.pop()

    def show(self):
        print(' '.join(self.dq))

def run():
    n = int(input())
    linked_list = LinkedList()
    for _ in range(n):
        order = input()
        if order.startswith('insert'):
            linked_list.insert(order.split()[1])
        elif order.startswith('deleteFirst'):
            linked_list.deleteFirst()
        elif order.startswith('deleteLast'):
            linked_list.deleteLast()
        elif order.startswith('delete'):
            linked_list.delete(order.split()[1])
    linked_list.show()

if __name__ == '__main__':
    run()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

def run():
    n = int(input())
    dq = deque()
    for _ in range(n):
        order = input()
        if order[0] == 'i':
            dq.appendleft(order.split()[1])
        elif order[6] == ' ':
            key = order.split()[1]
            if not key in dq:
                continue
            for i in range(len(dq)):
                if dq[i] == key:
                    dq.remove(key)
                    break
        elif order[6] == 'F':
            dq.popleft()
        elif order[6] == 'L':
            dq.pop()

    print(' '.join(dq))

if __name__ == '__main__':
    run()

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

class DList:
    class Cell:
        def __init__(self, k):
            self.key  = k
            self.prev = None
            self.next = None

    def __init__(self):
        self.head = DList.Cell(None)
        self.last = DList.Cell(None)
        self.head.next = self.last
        self.last.prev = self.head

    def insert(self, x):
        c = DList.Cell(x)
        c.prev = self.head
        c.next = self.head.next
        c.next.prev = c
        self.head.next = c

    def delete(self, x):
        c = self.__find(x)
        if c != None:
            self.__delete(c)

    def __delete(self, c):
        c.prev.next = c.next
        c.next.prev = c.prev

    def __find(self, x):
        c = self.head.next
        while c != None and c.key != x:
            c = c.next
        return c

    def deleteFirst(self):
        self.__delete(self.head.next)
    def deleteLast(self):
        self.__delete(self.last.prev)

    def __iter__(self):
        self.it = self.head.next
        return self
    def __next__(self):
        if self.it == self.last:
            raise StopIteration
        k = self.it.key
        self.it = self.it.next
        return k

dlist = DList()

n = int(stdin.readline())
for i in range(n):
    cmd = tuple(stdin.readline().split())
    if   cmd[0] == 'insert':
        dlist.insert(int(cmd[1]))
    elif cmd[0] == 'delete':
        dlist.delete(int(cmd[1]))
    elif cmd[0] == 'deleteFirst':
        dlist.deleteFirst()
    elif cmd[0] == 'deleteLast':
        dlist.deleteLast()

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

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

class DList:
    class Cell:
        def __init__(self, k):
            self.key  = k
            self.prev = None
            self.next = None

    def __init__(self):
        self.head = DList.Cell(None)
        self.last = DList.Cell(None)
        self.head.next = self.last
        self.last.prev = self.head

    def insert(self, x):
        c = DList.Cell(x)
        c.prev = self.head
        c.next = self.head.next
        c.next.prev = c
        self.head.next = c

    def delete(self, x):
        c = self.__find(x)
        if c != None:
            self.__delete(c)

    def __delete(self, c):
        c.prev.next = c.next
        c.next.prev = c.prev

    def __find(self, x):
        c = self.head.next
        while c != None and c.key != x:
            c = c.next
        return c

    def deleteFirst(self):
        self.__delete(self.head.next)
    def deleteLast(self):
        self.__delete(self.last.prev)

    def __iter__(self):
        self.it = self.head.next
        return self
    def __next__(self):
        if self.it == self.last:
            raise StopIteration
        k = self.it.key
        self.it = self.it.next
        return k

dlist = DList()

n = int(stdin.readline())
for i in range(n):
    cmd = tuple(stdin.readline().split())
    if   cmd[0] == 'insert':
        dlist.insert((cmd[1]))
    elif cmd[0] == 'delete':
        dlist.delete((cmd[1]))
    elif cmd[0] == 'deleteFirst':
        dlist.deleteFirst()
    elif cmd[0] == 'deleteLast':
        dlist.deleteLast()

print(' '.join((dlist)))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
n = int(input())
l = deque()

for _ in range (n):
    cmd = input().split(' ')
    if cmd[0]=='insert':
        l.appendleft(int(cmd[1]))
    if cmd[0]=='delete':
       if len(l) !=0 and int(cmd[1]) in l:
            l.remove(int(cmd[1]))
    if cmd[0]=='deleteFirst':
        l.popleft()
    if cmd[0]=='deleteLast':
        l.pop()
print(*l)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
n = int(input())
l = deque()
count ={}
for _ in range (n):
    cmd = input().split(' ')
    
    if cmd[0]=='insert':
        val = int(cmd[1])
        l.appendleft(val)
        if val not in count :
            count[val] =1
        else :
            count[val] +=1
                
    if cmd[0]=='delete':
        val = int(cmd[1])
        if len(l) !=0 and val in count:
           if count[val]:
            l.remove(val)
            count[val] -=1 
    
    if cmd[0]=='deleteFirst':
        val = l.popleft()
        count[val] -=1
    if cmd[0]=='deleteLast':
        val =l.pop()
        count[val] -=1
print(*l)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
n = int(input())
l = deque()

for _ in range (n):
    cmd = input().split(' ')
    
    if cmd[0]=='insert':
        val = int(cmd[1])
        l.appendleft(val)
                
    if cmd[0]=='delete':
        val = int(cmd[1])
        try:l.remove(val)
        except :pass
    
    if cmd[0]=='deleteFirst':
        l.popleft()
       
    if cmd[0]=='deleteLast':
        l.pop()
print(*l)

Submission status: Time Limit Exceeded

Solution code:
n = int(raw_input())

li = []
for i in range(n):
    cmd = raw_input().split()
    if cmd[0] == 'insert':
        li.insert(0,cmd[1])
    elif cmd[0] == 'delete':
        try:
            li.remove(cmd[1])
        except:
            pass
    elif cmd[0] == 'deleteFirst':
        li.pop(0)
    elif cmd[0] == 'deleteLast':
        li.pop()
for e in li:
    print e,

Submission status: Time Limit Exceeded

Solution code:
n = int(raw_input())

li = []
for i in range(n):
    cmd = raw_input().split()
    if cmd[0] == 'insert':
        li.append(cmd[1])
    elif cmd[0] == 'delete':
        try:
            p = li[::-1].index(cmd[1])
            li.pop(-p-1)
        except:
            pass
    elif cmd[0] == 'deleteFirst':
        li.pop()
    elif cmd[0] == 'deleteLast':
        li.pop(0)
for e in li[::-1]:
    print e,

Submission status: Time Limit Exceeded

Solution code:
n = int(raw_input())

li = []
for i in range(n):
    cmd = raw_input().split()
    if cmd[0] == 'insert':
        li.append(int(cmd[1]))
    elif cmd[0] == 'delete':
        try:
            p = li[::-1].index(int(cmd[1]))
            li.pop(-p-1)
        except:
            pass
    elif cmd[0] == 'deleteFirst':
        li.pop()
    elif cmd[0] == 'deleteLast':
        li.pop(0)
for e in li[::-1]:
    print e,