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:
# これもdeque使わないと時間切れになるのか？
from sys import stdin


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


class DoublyLinkedList():
    def __init__(self):
        head = Node()
        head.next = head
        head.prev = head
        self.size = 0
        self.head = head

    def insert(self, x):
        node = Node(key=x, prev=self.head, next=self.head.next)
        self.head.next.prev = node
        self.head.next = node
        #self.size += 1

    def delete(self, x):
        node = self.head.next
        while node is not self.head:
            if node.key == x:
                node.prev.next = node.next
                node.next.prev = node.prev
        #        self.size -= 1
                break
            node = node.next

    def deleteFirst(self):
        self.head.next.next.prev = self.head
        self.head.next = self.head.next.next
        #self.size -= 1

    def deleteLast(self):
        self.head.prev.prev.next = self.head
        self.head.prev = self.head.prev.prev
        #self.size -= 1

    def getKeys(self):
        node = self.head.next
        keys = []
        while node is not self.head:
            keys.append(str(node.key))
            node = node.next
        return " ".join(keys)


L = DoublyLinkedList()
n = int(input())
for i in range(n):
    command = stdin.readline().strip().split()
    if command[0] == "insert":
        L.insert(int(command[1]))
    elif command[0] == "delete":
        L.delete(int(command[1]))
    elif command[0] == "deleteFirst":
        L.deleteFirst()
    else:
        L.deleteLast()

print(L.getKeys())

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin


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


class DoublyLinkedList():
    def __init__(self):
        head = Node()
        tail = Node()
        head.next = tail
        head.prev = tail
        tail.next = head
        tail.prev = head
        #self.size = 0
        self.head = head
        self.tail = tail

    def insert(self, x):
        node = Node(key=x, prev=self.head, next=self.head.next)
        self.head.next.prev = node
        self.head.next = node
        #self.size += 1

    def delete(self, x):
        node = self.head.next
        while node is not self.tail:
            if node.key == x:
                node.prev.next = node.next
                node.next.prev = node.prev
                #self.size -= 1
                break
            node = node.next

    def deleteFirst(self):
        self.head.next.next.prev = self.head
        self.head.next = self.head.next.next
        #self.size -= 1

    def deleteLast(self):
        self.tail.prev.prev.next = self.tail
        self.tail.prev = self.tail.prev.prev
        #self.size -= 1

    def getKeys(self):
        node = self.head.next
        keys = []
        while node is not self.tail:
            keys.append(str(node.key))
            node = node.next
        return " ".join(keys)


L = DoublyLinkedList()
n = int(input())
for i in range(n):
    command = stdin.readline().strip().split()
    if command[0] == "insert":
        L.insert(int(command[1]))
    elif command[0] == "delete":
        L.delete(int(command[1]))
    elif command[0] == "deleteFirst":
        L.deleteFirst()
    else:
        L.deleteLast()

print(L.getKeys())

Submission status: Time Limit Exceeded

Solution code:
# これもdeque使わないと時間切れになるのか？
from sys import stdin


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


class DoublyLinkedList():
    def __init__(self):
        self.head = Node()
        self.head.next = self.head
        self.head.prev = self.head

    def insert(self, x):
        node = Node(key=x, prev=self.head, next=self.head.next)
        self.head.next.prev = node
        self.head.next = node

    def delete(self, x):
        node = self.head.next
        while node is not self.head and node.key != x:
            node = node.next
        if node != self.head:
            node.prev.next = node.next
            node.next.prev = node.prev

    def deleteFirst(self):
        self.head.next.next.prev = self.head
        self.head.next = self.head.next.next

    def deleteLast(self):
        self.head.prev.prev.next = self.head
        self.head.prev = self.head.prev.prev

    def getKeys(self):
        node = self.head.next
        keys = []
        while node is not self.head:
            keys.append(str(node.key))
            node = node.next
        return " ".join(keys)


L = DoublyLinkedList()
n = int(input())
for i in range(n):
    command = stdin.readline().strip().split()
    if command[0] == "insert":
        L.insert(int(command[1]))
    elif command[0] == "delete":
        L.delete(int(command[1]))
    elif command[0] == "deleteFirst":
        L.deleteFirst()
    else:
        L.deleteLast()

print(L.getKeys())

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin


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


class DoublyLinkedList():
    def __init__(self):
        self.head = Node()
        self.head.next = self.head
        self.head.prev = self.head

    def insert(self, x):
        node = Node(key=x, prev=self.head, next=self.head.next)
        self.head.next.prev = node
        self.head.next = node

    def search(self, x):
        node = self.head.next
        while node is not self.head and node.key != x:
            node = node.next
        return node

    def delete_key(self, x):
        node = self.search(x)
        if node is self.head:
            return None
        self._delete(node)

    def _delete(self, node):
        node.prev.next = node.next
        node.next.prev = node.prev

    def deleteFirst(self):
        self._delete(self.head.next)

    def deleteLast(self):
        self._delete(self.head.prev)

    def getKeys(self):
        node = self.head.next
        keys = []
        while node is not self.head:
            keys.append(node.key)
            node = node.next
        return " ".join(keys)


L = DoublyLinkedList()
n = int(input())
for i in range(n):
    command = stdin.readline().strip().split()
    if command[0] == "insert":
        L.insert(command[1])
    elif command[0] == "delete":
        L.delete_key(command[1])
    elif command[0] == "deleteFirst":
        L.deleteFirst()
    else:
        L.deleteLast()
    # print(L.getKeys())

print(L.getKeys())

Submission status: Time Limit Exceeded

Solution code:
# ALDS1_3_C.

def show(a):
    # 配列の中身を出力する。
    _str = ""
    for i in range(len(a) - 1):
        _str += str(a[i]) + " "
    _str += str(a[len(a) - 1])
    print(_str)

def main():
    n = int(input())
    list = []
    for i in range(n):
        command = input().split()
        if len(command) > 1:
            key = int(command[1])
            if command[0][0] == 'i': list.insert(0, key)
            else:
                if key in list: list.remove(key)
        else:
            if command[0][6] == 'F': del list[0]
            else: list.pop()
    show(list)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

n = int(input())
ops = [tuple(input().split()) for i in range(n)]

ary = deque([])


def insert(ary, x):
    ary.appendleft(x)


def delete(ary, x):
    if x in ary:
        ary.remove(x)


def delete_first(ary):
    ary.popleft()


def delete_last(ary):
    ary.pop()


def operate(ary, op):
    cmd = op[0]

    if cmd == 'insert':
        x = int(op[1])
        insert(ary, x)
    elif cmd == 'delete':
        x = int(op[1])
        delete(ary, x)
    elif cmd == 'deleteFirst':
        delete_first(ary)
    elif cmd == 'deleteLast':
        delete_last(ary)


[operate(ary, op) for op in ops]
print(' '.join([str(_) for _ in ary]))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

n = int(input())
ops = [tuple(input().split()) for i in range(n)]

ary = deque([])


def insert(ary, x):
    ary.appendleft(x)


def delete(ary, x):
    try:
        ary.remove(x)
    except ValueError:
        pass


def delete_first(ary):
    ary.popleft()


def delete_last(ary):
    ary.pop()


def operate(ary, op):
    cmd = op[0]

    if cmd == 'insert':
        x = int(op[1])
        insert(ary, x)
    elif cmd == 'delete':
        x = int(op[1])
        delete(ary, x)
    elif cmd == 'deleteFirst':
        delete_first(ary)
    elif cmd == 'deleteLast':
        delete_last(ary)


[operate(ary, op) for op in ops]
print(' '.join([str(_) for _ in ary]))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

n = int(input())
ops = [tuple(input().split()) for i in range(n)]

ary = deque([])


def insert(ary, x):
    ary.appendleft(x)


def delete(ary, x):
    if x in ary:
        ary.remove(x)

def delete_first(ary):
    ary.popleft()


def delete_last(ary):
    ary.pop()


def operate(ary, op):
    cmd = op[0]

    if cmd == 'insert':
        x = int(op[1])
        insert(ary, x)
    elif cmd == 'delete':
        x = int(op[1])
        delete(ary, x)
    elif cmd == 'deleteFirst':
        delete_first(ary)
    elif cmd == 'deleteLast':
        delete_last(ary)


[operate(ary, op) for op in ops]
print(*ary)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque


def insert(ary, x):
    ary.appendleft(x)


def delete(ary, x):
    if x in ary:
        ary.remove(x)

def delete_first(ary):
    ary.popleft()


def delete_last(ary):
    ary.pop()


def operate(ary, op):
    cmd = op[0]

    if cmd == 'insert':
        x = int(op[1])
        insert(ary, x)
    elif cmd == 'delete':
        x = int(op[1])
        delete(ary, x)
    elif cmd == 'deleteFirst':
        delete_first(ary)
    elif cmd == 'deleteLast':
        delete_last(ary)


n = int(input())
ary = deque([])

[operate(ary, input().split()) for i in range(n)]
print(*ary)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque


def insert(ary, x):
    ary.appendleft(x)


def delete(ary, x):
    if x in ary:
        ary.remove(x)


def delete_first(ary):
    ary.popleft()


def delete_last(ary):
    ary.pop()


n = int(input())
ary = deque([])

for _ in range(n):
    op = input().split()
    cmd = op[0]

    if cmd == 'insert':
        x = int(op[1])
        insert(ary, x)
    elif cmd == 'delete':
        x = int(op[1])
        delete(ary, x)
    elif cmd == 'deleteFirst':
        delete_first(ary)
    elif cmd == 'deleteLast':
        delete_last(ary)

print(*ary)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque


def insert(ary, x):
    ary.appendleft(x)


def delete(ary, x):
    if x in ary:
        ary.remove(x)


def delete_first(ary):
    ary.popleft()


def delete_last(ary):
    ary.pop()


n = int(input())
ary = deque([])

for _ in range(n):
    op = input().split()
    cmd = op[0]

    if cmd[0] == 'i':
        x = int(op[1])
        insert(ary, x)
    elif len(cmd) == 6:
        x = int(op[1])
        delete(ary, x)
    elif len(cmd) == 11:
        delete_first(ary)
    elif len(cmd) == 10:
        delete_last(ary)

print(*ary)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

n = int(input())
ary = deque([])

for _ in range(n):
    op = input().split()
    cmd = op[0]

    if cmd == 'insert':
        x = int(op[1])
        ary.appendleft(x)

    elif cmd == 'delete':
        x = int(op[1])
        if x in ary:
            ary.remove(x)

    elif cmd == 'deleteFirst':
        ary.popleft()

    elif cmd == 'deleteLast':
        ary.pop()

print(*ary)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

n = int(input())
ary = deque([])

for _ in range(n):
    op = input().split()
    cmd = op[0]

    if cmd[0] == 'i':
        x = op[1]
        ary.appendleft(x)

    elif len(cmd) == 6:
        x = op[1]
        if x in ary:
            ary.remove(x)

    elif len(cmd) == 10:
        ary.pop()

    else:
        ary.popleft()

print(*ary)

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8

n = int(input())
doublyList = []
for i in range(n):
    order = list(input().split())
    if len(order) == 2:
        if order[0] == "insert":
            doublyList.insert(0, order[1])
        if order[0] == "delete":
            for j,data in enumerate(doublyList):
                if data == order[1]:
                    doublyList.pop(j)
                    break

                    
    else:
        if order[0] == "deleteFirst" :
            doublyList.pop(0)
        if order[0] == "deleteLast":
            doublyList.pop()
            
doublyList = " ".join(doublyList)
print(doublyList)

Submission status: Time Limit Exceeded

Solution code:
def assign(linked_list, commmand, x):
    if command == 'insert':
        linked_list.insert(0, int(x))
    if command == 'delete':
        if int(x) in linked_list:
            linked_list.remove(int(x))
    if command == 'deleteFirst':
        linked_list.pop(0)
    if command == 'deleteLast':
        linked_list.pop()

n = int(input())
linked_list = []
for i in range(n):
    input_line = input().split()
    if len(input_line) == 2:
        command, x = input_line
    else:
        command, x = input_line[0], -1
    assign(linked_list, command, x)
print(' '.join(map(str, linked_list)))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
def insert():
    a.appendleft(command[1])

def delete():
    try:
        a.remove(command[1])
    except ValueError:
        pass

def deleteFirst():
    a.popleft()

def deleteLast():
    a.pop()

n = int(input())
a = deque([])
for i in range(n):
    command = input().split()
    if command[0] == "insert":
        insert()
    elif command[0] == "delete":
        delete()
    elif command[0] == "deleteFirst":
        deleteFirst()
    else:
        deleteLast()

for i, e in enumerate(a):
    if i == len(a) - 1:
        print(e)
    else:
        print(e, end=' ')

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
#-*- coding: utf-8 -*-


class Node:
    def __init__(self, val):
        self.val = val
        self.prev = None
        self.next = None


class LinkedList:
    def __init__(self):
        self.head = Node(None)
        self.head.prev = self.head
        self.head.next = self.head

    def insertNode(self, p):
        p.prev = self.head
        p.next = self.head.next
        self.head.next.prev = p
        self.head.next = p

    def searchNode(self, val):
        p = self.head.next
        while p != self.head and p.val != val:
            p = p.next
        return p

    def deleteNode(self, p):
        if p == self.head:
            return
        p.prev.next = p.next
        p.next.prev = p.prev

    def insert(self, val):
        self.insertNode(Node(val))

    def delete(self, val):
        p = self.searchNode(val)
        self.deleteNode(p)

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

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

    def show(self):
        p = self.head.next
        while p != self.head:
            print p.val,
            p = p.next
        print


if __name__ == '__main__':
    n = int(raw_input())
    i = 0
    link = LinkedList()
    while i != n:
        line = raw_input().split()
        if line[0] == 'insert':
            link.insert(int(line[1]))
        elif line[0] == 'delete':
            link.delete(int(line[1]))
        elif line[0] == 'deleteFirst':
            link.deleteFirst()
        elif line[0] == 'deleteLast':
            link.deleteLast()
        i += 1
    link.show()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
#-*- coding: utf-8 -*-

import sys


class Node:
    def __init__(self, val):
        self.val = val
        self.prev = None
        self.next = None


class LinkedList:
    def __init__(self):
        self.head = Node(None)
        self.head.prev = self.head
        self.head.next = self.head

    def insertNode(self, p):
        p.prev = self.head
        p.next = self.head.next
        self.head.next.prev = p
        self.head.next = p

    def searchNode(self, val):
        p = self.head.next
        while p != self.head and p.val != val:
            p = p.next
        return p

    def deleteNode(self, p):
        if p == self.head:
            return
        p.prev.next = p.next
        p.next.prev = p.prev

    def insert(self, val):
        self.insertNode(Node(val))

    def delete(self, val):
        p = self.searchNode(val)
        self.deleteNode(p)

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

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

    def show(self):
        p = self.head.next
        while p != self.head:
            print p.val,
            p = p.next
        print


if __name__ == '__main__':
    n = int(raw_input())
    link = LinkedList()
    for line in sys.stdin.read().splitlines():
        t = line.split()
        if t[0] == 'insert':
            link.insert(t[1])
        elif t[0] == 'delete':
            link.delete(t[1])
        elif t[0] == 'deleteFirst':
            link.deleteFirst()
        elif t[0] == 'deleteLast':
            link.deleteLast()
    link.show()

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8
from collections import deque

n = int(input())
day = deque()
for i in range(n):
    order = list(input().split())
    if order[0] == "insert":
        day.appendleft(order[1])
    if order[0] == "delete":
        try:
            day.remove(order[1])
        except:
            continue
    if order[0] == "deleteFirst":
        day.popleft()
    if order[0] == "deleteLast":
        day.pop()

day = " ".join(day)              
print(day)

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8
from collections import deque

n = int(input())
day = deque()
for i in range(n):
    order = list(input().split())
    if order[0] == "insert":
        day.appendleft(order[1])
    elif order[0] == "delete":
        try:
            day.remove(order[1])
        except:
            continue
    elif order[0] == "deleteFirst":
        day.popleft()
    elif order[0] == "deleteLast":
        day.pop()

day = " ".join(day)              
print(day)

Submission status: Time Limit Exceeded

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

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

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

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

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


def print_list_split_whitespace(a):
    for x in a[:-1]:
        print(x, end=' ')
    print(a[-1])


dll = DoublyLinkedList()

n = int(input())
for _ in range(n):
    command = input().split()
    if command[0] == "insert":
        dll.insert(int(command[1]))
    elif command[0] == "delete":
        dll.delete(int(command[1]))
    elif command[0] == "deleteFirst":
        dll.deleteFirst()
    elif command[0] == "deleteLast":
        dll.deleteLast()
    else:
        raise ValueError("No Command such as", command[0])

print_list_split_whitespace(dll.data)

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    store = list()
    for i in range(int(input())):
        parts = input().split()
        cmd = parts[0]
        if cmd == 'insert':
            store.insert(0, int(parts[1]))
        elif cmd == 'delete':
            num = int(parts[1])
            if num in store:
                store.remove(num)
        elif cmd == 'deleteFirst':
            store.remove(store[0])
        elif cmd =='deleteLast':
            store = store[:-1]
    print(' '.join(map(str, store)))

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    store = list()
    for i in range(int(input())):
        parts = input().split()
        cmd = parts[0]
        if cmd == 'insert':
            store.insert(0, int(parts[1]))
        elif cmd == 'delete':
            num = int(parts[1])
            if num in store:
                store.remove(num)
        elif cmd == 'deleteFirst':
            store.remove(store[0])
        elif cmd =='deleteLast':
            store = store[:-1]

    if len(store) > 0:
        for n in store[:-1]:
            print(n, end=' ')
        print(store[-1])

Submission status: Time Limit Exceeded

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

    def __init__(self, dtype):
        self.dtype = dtype
        self.dummy = LinkedList.Node(dtype())
        self.dummy.next = self.dummy
        self.dummy.prev = self.dummy

    def insert(self, x):
        node = LinkedList.Node(x, self.dummy.next, self.dummy)

        self.dummy.next.prev = node
        self.dummy.next = node
    
    def delete(self, x):
        node = self.dummy.next

        while node != self.dummy:
            if node.value == x:
                self.__remove_node(node)
                break
            else:
                node = node.next
    
    def deleteLast(self):
        node = self.dummy.prev
        if node != self.dummy:
            self.__remove_node(node)
    
    def deleteFirst(self):
        node = self.dummy.next
        if node != self.dummy:
            self.__remove_node(node)

    def __remove_node(self, node):
        node.prev.next = node.next
        node.next.prev = node.prev

    def items(self):
        node = self.dummy.next
        while node != self.dummy:
            yield node.value
            node = node.next

store = LinkedList(str)
for i in range(int(input())):
    parts = input().split()
    cmd = parts[0]
    if cmd == 'insert':
        store.insert(parts[1])
    elif cmd == 'delete':
        store.delete(parts[1])
    elif cmd == 'deleteFirst':
        store.deleteFirst()
    elif cmd =='deleteLast':
        store.deleteLast()

print(' '.join(store.items()))

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8
n = int(input())

class LinkedList:
    class Cell:
        def __init__(self, x, y=None, z=None):
            # セルを構成（ｘが値、ｙが次のセルの値、ｚが前のセルの値）
            self.data = x
            self.nex = y
            self.pre = z

    def __init__(self, *args):
        head = LinkedList.Cell(None)# オブジェクトを作る準備
        head.nex = head #ヘッダーの右隣のセル
        head.pre = head #ヘッダーの左隣のセル
        self.size = 0
        self.head = head #ヘッダー

    def push_front(self, x):
        h = self.head###ヘッダーのオブジェクトを呼び出し
        q = h.nex   ###qはヘッダーの右隣のセルをｑとおく
        p = LinkedList.Cell(x, q, h) #ｘを値とするセルｐの作成
        h.nex = p #ヘッダーの右隣のセルをｐとする
        q.pre = p #ｑのセルをｐとする
        self.size += 1

    def push_back(self, x):
        h = self.head
        p = h.pre
        q = LinkedList.Cell(x, h, p)
        h.pre = q
        p.next = q
        self.size += 1

    def pop_front(self):
        h = self.head
        q = h.nex
        p = q.nex
        p.pre = h
        h.nex = p
        self.size -= 1

    def pop_back(self):
        h = self.head
        p = h.pre
        q = p.pre
        q.nex = h
        h.pre = q
        self.size -= 1

    def delete(self, x):
        cp = self.head
        i = 0
        while i < self.size:
            i += 1
            cp = cp.nex
            if cp.data == x:
                cp_pre = cp.pre
                cp_nex = cp.nex
                cp_pre.nex = cp_nex
                cp_nex.pre = cp_pre
                self.size -= 1
                return cp.data
        return None
            

l = LinkedList()
for i in range(n):
    order = list(input().split())
    if order[0] == "insert":
        l.push_front(order[1])
    elif order[0] == "delete":
        l.delete(order[1])
    elif order[0] == "deleteFirst":
        l.pop_front()
    else:
        l.pop_back()

head = l.head
numList = []
for i in range(l.size):
    head = head.nex
    numList.append(head.data)

numList = " ".join([str(num) for num in numList])
print(numList)

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8
from collections import deque
#from numba import jit
n = int(input())

def ord(order): 
    if order[0] == "insert":
        day.appendleft(order[1])
    elif order[0] == "delete":
        try:
            day.remove(order[1])
        except:
            pass
    elif order[0] == "deleteFirst":
        day.popleft()
    elif order[0] == "deleteLast":
        day.pop()


day = deque()
for i in range(n):
    order = list(input().split())
    ord(order)
day = " ".join(day)              
print(day)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict, deque

class LinkedList(object):
    class Node(object):
        def __init__(self, value, next=None, prev=None):
            self.value = value
            self.next = next
            self.prev = prev

    def __init__(self, dtype):
        self.dtype = dtype
        self.dummy = LinkedList.Node(dtype())
        self.dummy.next = self.dummy
        self.dummy.prev = self.dummy

        self.dict = defaultdict(deque)

    def insert(self, x):
        node = LinkedList.Node(x, self.dummy.next, self.dummy)

        self.dummy.next.prev = node
        self.dummy.next = node

        self.dict[x].append(node)
    
    def delete(self, x):
        if x in self.dict:
            queue = self.dict[x]
            if len(queue) > 0:
                node = queue.pop()
                self.__remove_node(node)
    
    def deleteLast(self):
        node = self.dummy.prev
        if node != self.dummy:
            self.__remove_node(node)
            self.dict[node.value].popleft()
    
    def deleteFirst(self):
        node = self.dummy.next
        if node != self.dummy:
            self.__remove_node(node)
            self.dict[node.value].pop()

    def __remove_node(self, node):
        node.prev.next = node.next
        node.next.prev = node.prev

    def items(self):
        node = self.dummy.next
        while node != self.dummy:
            yield node.value
            node = node.next

store = LinkedList(str)
for i in range(int(input())):
    parts = input().split()
    cmd = parts[0]
    if cmd == 'insert':
        store.insert(parts[1])
    elif cmd == 'delete':
        store.delete(parts[1])
    elif cmd == 'deleteFirst':
        store.deleteFirst()
    elif cmd =='deleteLast':
        store.deleteLast()

print(' '.join(store.items()))

Submission status: Time Limit Exceeded

Solution code:

from collections import defaultdict, deque

class LinkedList(object):
    class Node(object):
        def __init__(self, value, next=None, prev=None):
            self.value = value
            self.next = next
            self.prev = prev

    def __init__(self, dtype):
        self.dtype = dtype
        self.dummy = LinkedList.Node(dtype())
        self.dummy.next = self.dummy
        self.dummy.prev = self.dummy

        self.dict = defaultdict(deque)

    def insert(self, x):
        node = LinkedList.Node(x, self.dummy.next, self.dummy)

        self.dummy.next.prev = node
        self.dummy.next = node

        self.dict[x].append(node)
    
    def delete(self, x):
        if x in self.dict:
            queue = self.dict[x]
            if len(queue) > 0:
                node = queue.pop()
                self.__remove_node(node)
    
    def deleteLast(self):
        node = self.dummy.prev
        if node != self.dummy:
            self.__remove_node(node)
            self.dict[node.value].popleft()
    
    def deleteFirst(self):
        node = self.dummy.next
        if node != self.dummy:
            self.__remove_node(node)
            self.dict[node.value].pop()

    def __remove_node(self, node):
        node.prev.next = node.next
        node.next.prev = node.prev

    def items(self):
        ret = []
        node = self.dummy.next
        while node != self.dummy:
            ret.append(node.value)
            node = node.next
        return ret

store = LinkedList(str)
for i in range(int(input())):
    parts = input().split()
    cmd = parts[0]
    if cmd == 'insert':
        store.insert(parts[1])
    elif cmd == 'delete':
        store.delete(parts[1])
    elif cmd == 'deleteFirst':
        store.deleteFirst()
    elif cmd =='deleteLast':
        store.deleteLast()

print(' '.join(store.items()))