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:
#http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_3_C&lang=jp
#???????????£????????????

NEXT = 2
DATA = 1
PREV = 0
import gc
def dll_processor(operations):
    
    front = None
    end = None
    
    for i,o in enumerate(operations):
        if o[0] == "insert":
            front, end = insert(front, end, [None, int(o[1]), None])
        elif o[0] == "delete":
            front, end = delete(front, end, int(o[1]))
        elif o[0] == "deleteFirst":
            front, end = delete_first(front, end)
        elif o[0] == "deleteLast":
            front, end = delete_last(front, end)
        #print(get_list(front))
        
    return get_list(front)

def get_list(front):
    if not front:
        return []
    
    l = []
    target = front
    while True:
        l.append(target[DATA])
        if not target[NEXT]:
            break
        target = target[NEXT]
    return l

def insert(front, end, target):

    if front:
        front[PREV] = target
        target[NEXT] = front
        return target, end
    else:
        return target, target
    
def delete(front, end,target):

    delete_node = front
    while not delete_node[DATA] == target:
        delete_node = delete_node[NEXT]
        if delete_node == None:
            return front, end

    if delete_node[PREV] == None:
        delete_node[NEXT][PREV] = None
        return delete_node[NEXT], end
    elif delete_node[NEXT] == None:
        delete_node[PREV][NEXT] = None
        return front, delete_node[PREV]
    else:
        delete_node[NEXT][PREV] = delete_node[PREV]
        delete_node[PREV][NEXT] = delete_node[NEXT]
        return front, end
    
def delete_last(front, end):
    
    if not end[PREV]:
        return None, None
    else:
        end[PREV][NEXT] = None
        return front, end[PREV]
    
def delete_first(front, end):
    
    if not front[NEXT]:
        return None, None
    else:
        front[NEXT][PREV] = None
        return front[NEXT], end

def main():
    n_list = int(input())
    target_list = [input().split() for i in range(n_list)]
    print(*dll_processor(target_list))
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

NEXT = 2
DATA = 1
PREV = 0
def dll_processor(operations):
    
    front = None
    end = None
    
    for i,o in enumerate(operations):
        if o[0] == "insert":
            front, end = insert(front, end, [None, int(o[1]), None])
        elif o[0] == "delete":
            front, end = delete(front, end, int(o[1]))
        elif o[0] == "deleteFirst":
            front, end = delete_first(front, end)
        elif o[0] == "deleteLast":
            front, end = delete_last(front, end)
        #print(get_list(front))
        
    return get_list(front)

def get_list(front):
    if not front:
        return []
    
    l = []
    target = front
    while True:
        l.append(target[DATA])
        if not target[NEXT]:
            break
        target = target[NEXT]
    return l

def insert(front, end, target):

    if front:
        front[PREV] = target
        target[NEXT] = front
        return target, end
    else:
        return target, target
    
def delete(front, end,target):

    delete_node = front
    while not delete_node[DATA] == target:
        delete_node = delete_node[NEXT]
        if delete_node == None:
            return front, end

    if delete_node[PREV] == None:
        delete_node[NEXT][PREV] = None
        return delete_node[NEXT], end
    elif delete_node[NEXT] == None:
        delete_node[PREV][NEXT] = None
        return front, delete_node[PREV]
    else:
        delete_node[NEXT][PREV] = delete_node[PREV]
        delete_node[PREV][NEXT] = delete_node[NEXT]
        return front, end
    
def delete_last(front, end):
    
    if not end[PREV]:
        return None, None
    else:
        end[PREV][NEXT] = None
        return front, end[PREV]
    
def delete_first(front, end):
    
    if not front[NEXT]:
        return None, None
    else:
        front[NEXT][PREV] = None
        return front[NEXT], end

def main():
    n_list = int(input())
    target_list = [input().split() for i in range(n_list)]
    print(*dll_processor(target_list))
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

NEXT = 2
DATA = 1
PREV = 0
def dll_processor(operations):
    
    front = None
    end = None
    
    for o in operations:
        if o[0] == "insert":
            front, end = insert(front, end, [None, o[1], None])
        elif o[0] == "delete":
            front, end = delete(front, end, o[1])
        elif o[0] == "deleteFirst":
            front, end = delete_first(front, end)
        elif o[0] == "deleteLast":
            front, end = delete_last(front, end)
        #print(get_list(front))
        
    return get_list(front)

def get_list(front):
    if not front:
        return []
    
    l = []
    target = front
    while True:
        l.append(target[DATA])
        if not target[NEXT]:
            break
        target = target[NEXT]
    return l

def insert(front, end, target):

    if front:
        front[PREV] = target
        target[NEXT] = front
        return target, end
    else:
        return target, target
    
def delete(front, end,target):

    delete_node = front
    while not delete_node[DATA] == target:
        delete_node = delete_node[NEXT]
        if delete_node == None:
            return front, end

    if delete_node[PREV] == None:
        delete_node[NEXT][PREV] = None
        return delete_node[NEXT], end
    elif delete_node[NEXT] == None:
        delete_node[PREV][NEXT] = None
        return front, delete_node[PREV]
    else:
        delete_node[NEXT][PREV] = delete_node[PREV]
        delete_node[PREV][NEXT] = delete_node[NEXT]
        return front, end
    
def delete_last(front, end):
    
    if not end[PREV]:
        return None, None
    else:
        end[PREV][NEXT] = None
        return front, end[PREV]
    
def delete_first(front, end):
    
    if not front[NEXT]:
        return None, None
    else:
        front[NEXT][PREV] = None
        return front[NEXT], end

def main():
    n_list = int(input())
    target_list = [input().split() for i in range(n_list)]
    print(*dll_processor(target_list))
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

NEXT = 2
DATA = 1
PREV = 0
def dll_processor(operations):
    
    front = None
    end = None
    
    for o in operations:
        if o[0] == "insert":
            front, end = insert(front, end, o[1])
        elif o[0] == "delete":
            front, end = delete(front, end, o[1])
        elif o[0] == "deleteFirst":
            front, end = delete_first(front, end)
        elif o[0] == "deleteLast":
            front, end = delete_last(front, end)
        #print(get_list(front))
        
    return get_list(front)

def get_list(front):
    if not front:
        return []
    
    l = []
    target = front
    while True:
        l.append(target[DATA])
        if not target[NEXT]:
            break
        target = target[NEXT]
    return l

def insert(front, end, target):
    node = [None, target, None]
    if front:
        front[PREV] = node
        node[NEXT] = front
        return node, end
    else:
        return node, node
    
def delete(front, end,target):

    delete_node = front
    while not delete_node[DATA] == target:
        delete_node = delete_node[NEXT]
        if delete_node == None:
            return front, end

    if delete_node[PREV] == None:
        delete_node[NEXT][PREV] = None
        return delete_node[NEXT], end
    elif delete_node[NEXT] == None:
        delete_node[PREV][NEXT] = None
        return front, delete_node[PREV]
    else:
        delete_node[NEXT][PREV] = delete_node[PREV]
        delete_node[PREV][NEXT] = delete_node[NEXT]
        return front, end
    
def delete_last(front, end):
    
    if not end[PREV]:
        return None, None
    else:
        end[PREV][NEXT] = None
        return front, end[PREV]
    
def delete_first(front, end):
    
    if not front[NEXT]:
        return None, None
    else:
        front[NEXT][PREV] = None
        return front[NEXT], end

def main():
    n_list = int(input())
    target_list = [input().split() for i in range(n_list)]
    print(*dll_processor(target_list))
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import namedtuple


class DualLinkList(object):
    def __init__(self):
        self.nil = namedtuple('Node', ['key', 'prev', 'next'])
        self.nil.prev = self.nil
        self.nil.next = self.nil

    def insert(self, key):
        x = namedtuple('Node', ['key', 'prev', 'next'])
        x.key = key
        x.next = self.nil.next
        self.nil.next.prev = x
        self.nil.next = x
        x.prev = self.nil

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

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

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

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

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

    def get_keys(self):
        results = []
        cur = self.nil.next
        while cur != self.nil:
            results.append(cur.key)
            cur = cur.next
        return results

    def print(self):
        cur = self.nil.next
        while cur != self.nil:
            print(cur.key)
            cur = cur.next


def process_instructions(dll, insts):
    for ins in insts:
        try:
            code, key = ins.split()
        except ValueError:
            code = ins
        if code == 'insert':
            dll.insert(int(key))
        elif code == 'delete':
            dll.deleteKey(int(key))
        elif code == 'deleteFirst':
            dll.deleteFirst()
        elif code == 'deleteLast':
            dll.deleteLast()
        else:
            raise ValueError
    return dll.get_keys()


if __name__ == '__main__':
    # ??????????????\???
    num = int(input())
    data = [input() for x in range(num)]


    # ??????????????????
    dll = DualLinkList()
    results = process_instructions(dll, data)

    # ???????????¨???
    print('{0}'.format(' '.join(map(str, results))))

Submission status: Time Limit Exceeded

Solution code:
from collections import namedtuple


class DualLinkList(object):
    def __init__(self):
        self.nil = namedtuple('Node', ['key', 'prev', 'next'])
        self.nil.prev = self.nil
        self.nil.next = self.nil

    def insert(self, key):
        x = namedtuple('Node', ['key', 'prev', 'next'])
        x.key = key
        x.next = self.nil.next
        self.nil.next.prev = x
        self.nil.next = x
        x.prev = self.nil

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

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

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

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

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

    def get_keys(self):
        results = []
        cur = self.nil.next
        while cur != self.nil:
            results.append(cur.key)
            cur = cur.next
        return results

    def print(self):
        cur = self.nil.next
        while cur != self.nil:
            print(cur.key)
            cur = cur.next


def process_instructions(dll, insts):
    for ins in insts:
        try:
            code, key = ins.split()
        except ValueError:
            code = ins
        if code == 'insert':
            dll.insert(int(key))
        elif code == 'delete':
            dll.deleteKey(int(key))
        elif code == 'deleteFirst':
            dll.deleteFirst()
        elif code == 'deleteLast':
            dll.deleteLast()
        else:
            raise ValueError
    return dll.get_keys()


if __name__ == '__main__':
    # ??????????????\???
    num = int(input())
    data = [input() for x in range(num)]


    # ??????????????????
    dll = DualLinkList()
    results = process_instructions(dll, data)

    # ???????????¨???
    print('{0}'.format(' '.join(map(str, results))))

Submission status: Time Limit Exceeded

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

NEXT = 2
DATA = 1
PREV = 0
def dll_processor(operations):
    
    front = None
    end = None
    dll = []
    for o in operations:
        if o[0] == "insert":
            dll = [o[1]] + dll
            #front, end = insert(front, end, o[1])
        elif o[0] == "delete":
            if o[1] in dll:
                dll.remove(o[1])
            #front, end = delete(front, end, o[1])
        elif o[0] == "deleteFirst":
            dll = dll[1:]
            #front, end = delete_first(front, end)
        elif o[0] == "deleteLast":
            dll = dll[:-1]
            #front, end = delete_last(front, end)
        #print(get_list(front))
        
    return dll

def get_list(front):
    if not front:
        return []
    
    l = []
    target = front
    while True:
        l.append(target[DATA])
        if not target[NEXT]:
            break
        target = target[NEXT]
    return l

def insert(front, end, target):
    node = [None, target, None]
    if front:
        front[PREV] = node
        node[NEXT] = front
        return node, end
    else:
        return node, node
    
def delete(front, end,target):

    delete_node = front
    while not delete_node[DATA] == target:
        delete_node = delete_node[NEXT]
        if delete_node == None:
            return front, end

    if delete_node[PREV] == None:
        delete_node[NEXT][PREV] = None
        return delete_node[NEXT], end
    elif delete_node[NEXT] == None:
        delete_node[PREV][NEXT] = None
        return front, delete_node[PREV]
    else:
        delete_node[NEXT][PREV] = delete_node[PREV]
        delete_node[PREV][NEXT] = delete_node[NEXT]
        return front, end
    
def delete_last(front, end):
    
    if not end[PREV]:
        return None, None
    else:
        end[PREV][NEXT] = None
        return front, end[PREV]
    
def delete_first(front, end):
    
    if not front[NEXT]:
        return None, None
    else:
        front[NEXT][PREV] = None
        return front[NEXT], end

def main():
    n_list = int(input())
    target_list = [input().split() for i in range(n_list)]
    print(*dll_processor(target_list))
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

NEXT = 2
DATA = 1
PREV = 0
def dll_processor(operations):
    
    front = None
    end = None
    dll = []
    for o in operations:
        if o[0] == "insert":
            dll.insert(0, o[1])
            #front, end = insert(front, end, o[1])
        elif o[0] == "delete":
            if o[1] in dll:
                dll.remove(o[1])
            #front, end = delete(front, end, o[1])
        elif o[0] == "deleteFirst":
            dll = dll[1:]
            #front, end = delete_first(front, end)
        elif o[0] == "deleteLast":
            dll = dll[:-1]
            #front, end = delete_last(front, end)
        #print(get_list(front))
        
    return dll

def get_list(front):
    if not front:
        return []
    
    l = []
    target = front
    while True:
        l.append(target[DATA])
        if not target[NEXT]:
            break
        target = target[NEXT]
    return l

def insert(front, end, target):
    node = [None, target, None]
    if front:
        front[PREV] = node
        node[NEXT] = front
        return node, end
    else:
        return node, node
    
def delete(front, end,target):

    delete_node = front
    while not delete_node[DATA] == target:
        delete_node = delete_node[NEXT]
        if delete_node == None:
            return front, end

    if delete_node[PREV] == None:
        delete_node[NEXT][PREV] = None
        return delete_node[NEXT], end
    elif delete_node[NEXT] == None:
        delete_node[PREV][NEXT] = None
        return front, delete_node[PREV]
    else:
        delete_node[NEXT][PREV] = delete_node[PREV]
        delete_node[PREV][NEXT] = delete_node[NEXT]
        return front, end
    
def delete_last(front, end):
    
    if not end[PREV]:
        return None, None
    else:
        end[PREV][NEXT] = None
        return front, end[PREV]
    
def delete_first(front, end):
    
    if not front[NEXT]:
        return None, None
    else:
        front[NEXT][PREV] = None
        return front[NEXT], end

def main():
    n_list = int(input())
    target_list = [input().split() for i in range(n_list)]
    print(*dll_processor(target_list))
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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


''' ???????????£???????????? '''

functions = {"insert": lambda obj, x: obj.insert(x),
             "delete": lambda obj, x: obj.delete(x),
             "deleteFirst": lambda obj: obj.deleteFirst(),
             "deleteLast": lambda obj: obj.deleteLast()}


class DoublyLinkedList(object):
    """ Doubly Linked List """

    def __init__(self):
        self.list = []

    def insert(self, x):
        """ ???????????????x?????????????´?????????\ """
        self.list = [x] + self.list

    def delete(self, x):
        """ ??????x??????????????????????´??????????"""
        for i in range(len(self.list)):
            if self.list[i] == x:
                self.list.pop(i)
                break

    def deleteFirst(self):
        """ ?????????????????????????´??????????"""
        self.list.pop(0)

    def deleteLast(self):
        """ ????????????????°????????´??????????"""
        self.list.pop()


if __name__ == '__main__':
    N = int(input())

    dll = DoublyLinkedList()

    for i in range(N):
        command = input().split()

        if len(command) == 1:
            functions[command[0]](dll)
        elif len(command) == 2:
            functions[command[0]](dll, int(command[1]))

    print(' '.join(map(str, dll.list)))

Submission status: Time Limit Exceeded

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

''' ???????????£???????????? '''

from collections import deque

functions = {"insert": lambda obj, x: obj.insert(x),
             "delete": lambda obj, x: obj.delete(x),
             "deleteFirst": lambda obj: obj.deleteFirst(),
             "deleteLast": lambda obj: obj.deleteLast()}


class DoublyLinkedList(object):
    """ Doubly Linked List """

    def __init__(self):
        # self.list = []
        self.list = deque()

    def insert(self, x):
        """ ???????????????x?????????????´?????????\ """
        # self.list = [x] + self.list
        self.list.appendleft(x)

    def delete(self, x):
        """ ??????x??????????????????????´??????????"""
        # for i in range(len(self.list)):
        #     if self.list[i] == x:
        #         self.list.pop(i)
        #         break
        if x in self.list:
            self.list.remove(x)

    def deleteFirst(self):
        """ ?????????????????????????´??????????"""
        # self.list.pop(0)
        self.list.popleft()

    def deleteLast(self):
        """ ????????????????°????????´??????????"""
        self.list.pop()


if __name__ == '__main__':
    N = int(input())

    dll = DoublyLinkedList()

    for i in range(N):
        command = input().split()

        if len(command) == 1:
            functions[command[0]](dll)
        elif len(command) == 2:
            functions[command[0]](dll, int(command[1]))

    print(' '.join(map(str, dll.list)))

Submission status: Time Limit Exceeded

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

''' ???????????£???????????? '''

from collections import deque

functions = {"insert": lambda obj, x: obj.insert(x),
             "delete": lambda obj, x: obj.delete(x),
             "deleteFirst": lambda obj: obj.deleteFirst(),
             "deleteLast": lambda obj: obj.deleteLast()}


class DoublyLinkedList(object):
    """ Doubly Linked List """

    def __init__(self):
        # self.list = []
        self.list = deque()

    def insert(self, x):
        """ ???????????????x?????????????´?????????\ """
        # self.list = [x] + self.list
        self.list.appendleft(x)

    def delete(self, x):
        """ ??????x??????????????????????´?????????? """
        # for i in range(len(self.list)):
        #     if self.list[i] == x:
        #         self.list.pop(i)
        #         break
        if x in self.list:
            self.list.remove(x)

    def deleteFirst(self):
        """ ?????????????????????????´?????????? """
        # self.list.pop(0)
        self.list.popleft()

    def deleteLast(self):
        """ ????????????????°????????´?????????? """
        self.list.pop()


if __name__ == '__main__':
    N = int(input())

    dll = DoublyLinkedList()

    for i in range(N):
        command = input().split()

        if len(command) == 1:
            functions[command[0]](dll)
        elif len(command) == 2:
            functions[command[0]](dll, int(command[1]))

    print(' '.join(map(str, dll.list)))

Submission status: Time Limit Exceeded

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

result = list()

def operation(command):
    if command[0] == "insert":
        result.insert(0, int(command[1]))
    elif command[0] == "delete":
        if int(command[1]) in result:
            result.remove(int(command[1]))
    elif command[0] == "deleteFirst":
        result.pop(0)
    elif command[0] == "deleteLast":
        result.pop()

n = int(input())
for i in range(n):
    command = input().split()
    operation(command)
print(*result)

Submission status: Time Limit Exceeded

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

result = list()

def operation(command):
    if command[0] == "insert":
        result.insert(0, int(command[1]))
    elif command[0] == "delete":
        if int(command[1]) in result:
            result.remove(int(command[1]))
    elif command[0] == "deleteFirst":
        del result[0]
    elif command[0] == "deleteLast":
        result.pop()

n = int(input())
for i in range(n):
    command = input().split()
    operation(command)
print(*result)

Submission status: Time Limit Exceeded

Solution code:
arr = []

for i in range(int(input())):
  command_line = input().split(" ")
  command = command_line[0]
  arg = ""
  if len(command_line) > 1: arg = command_line[1]

  if command == "insert":
    arr.insert(0, arg)
  if command == "delete":
    if arg in arr: arr.remove(arg)
  if command == "deleteFirst":
    del arr[0]
  if command == "deleteLast":
    del arr[-1]

print(" ".join(arr))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

arr = []

for i in range(int(input())):
  command_line = input().split(" ")
  command = command_line[0]
  arg = ""
  if len(command_line) > 1: arg = command_line[1]

  if command == "insert":
    arr.insert(0, arg)
  if command == "delete":
    if arg in arr: arr.remove(arg)
  if command == "deleteFirst":
    arr.pop(0)
  if command == "deleteLast":
    arr.pop()

print(" ".join(arr))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

arr = []

for i in range(int(input())):
  command_line = input().split(" ")
  command = command_line[0]
  arg = ""
  if len(command_line) > 1: arg = command_line[1]

  if command == "insert":
    arr.insert(0, arg)
  if command == "delete":
    try:
      arr.remove(arg)
    except ValueError:
      pass
  if command == "deleteFirst":
    arr.pop(0)
  if command == "deleteLast":
    arr.pop()

print(" ".join(arr))

Submission status: Time Limit Exceeded

Solution code:
# f = open('input.txt')

n = int(input())
# n = int(f.readline())
doubly_linked_list = []

for i in range(n):
    cmd = input()
    # cmd = f.readline().strip()
    if cmd == 'deleteFirst':
        doubly_linked_list.pop(0)
    elif cmd == 'deleteLast':
        doubly_linked_list.pop(-1)
    else:
        cmd, x = cmd.split()
        if cmd == 'insert':
            doubly_linked_list.insert(0, x)
        elif cmd == 'delete' and x in doubly_linked_list:
            doubly_linked_list.remove(x)

print(' '.join(doubly_linked_list))

Submission status: Time Limit Exceeded

Solution code:
# f = open('input.txt')

n = int(input())
# n = int(f.readline())
elem_cnt = {}
doubly_linked_list = []

for i in range(n):
    cmd = input()
    # cmd = f.readline().strip()
    if cmd == 'deleteFirst':
        doubly_linked_list.pop(0)
    elif cmd == 'deleteLast':
        doubly_linked_list.pop(-1)
    else:
        cmd, x = cmd.split()
        if cmd == 'insert':
            doubly_linked_list.insert(0, x)
        elif cmd == 'delete':
            try:
                doubly_linked_list.remove(x)
            except ValueError:
                pass

print(' '.join(doubly_linked_list))

Submission status: Time Limit Exceeded

Solution code:
def show(A):
    for i in range(len(A) - 1):
        print(A[i], end=" ")
    print(A[len(A) - 1])

list = []

n = int(input())

for i in range(n):
    line = input().split()
    if line[0] == 'insert':
        list.insert(0, int(line[1]))
    if line[0] == 'delete':
        if int(line[1]) in list:
            list.remove(int(line[1]))
    if line[0] == 'deleteFirst':
        list.remove(list[0])
    if line[0] == 'deleteLast':
        list.pop()

show(list)

Submission status: Time Limit Exceeded

Solution code:
def show(A):
    for i in range(len(A) - 1):
        print(A[i], end=" ")
    print(A[len(A) - 1])

list = []

n = int(input())

for i in range(n):
    line = input().split()
    if line[0] == 'insert':
        list.insert(0, int(line[1]))
    elif line[0] == 'delete':
        if int(line[1]) in list:
            list.remove(int(line[1]))
    elif line[0] == 'deleteFirst':
        list.remove(list[0])
    elif line[0] == 'deleteLast':
        list.pop()

show(list)

Submission status: Time Limit Exceeded

Solution code:
a=[]
for _ in range(int(input())):
    b=input().split()
    if len(b)>1:c=int(b[1])
    b=b[0]
    if b[0]=='i':a=[c]+a
    elif len(b)>10:a=a[1:]
    elif len(b)>6:a.pop()
    else:
        try:a.remove(c)
        except:pass
print(*a)

Submission status: Time Limit Exceeded

Solution code:
import collections
a=collections.deque()
for _ in range(int(input())):
    b=input().split()
    if len(b)>1:c=int(b[1])
    b=b[0]
    if b[0]=='i':a.appendleft(c)
    elif len(b)>10:a.popleft()
    elif len(b)>6:a.pop()
    else:
        try:a.remove(c)
        except:pass
print(*a)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
data_input = []
for i in range(n):
    data_input.append(input().split())
dlList = []
for x in data_input:
    if x[0] == 'insert':
        dlList.insert(0,x[1])
    elif x[0] == 'delete':
        if x[1] in dlList:
            dlList.remove(x[1])
        else :
            pass
    elif x[0] == 'deleteFirst':
        dlList.pop(0)
    elif x[0] == 'deleteLast':
        dlList.pop()
    else:
        pass
print(' '.join(dlList))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
data_input = []
dlList = []
for i in range(n):
    x = input().split()
    if x[0] == 'insert':
        dlList.insert(0,x[1])
    elif x[0] == 'delete':
        if x[1] in dlList:
            dlList.remove(x[1])
        else :
            pass
    elif x[0] == 'deleteFirst':
        dlList.pop(0)
    elif x[0] == 'deleteLast':
        dlList.pop()
    else:
        pass
print(' '.join(dlList))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
data_input = []
dlList = []
for i in range(n):
    x = input().split()
    if x[0] == 'insert':
        dlList.insert(0,x[1])
    elif x[0] == 'delete':
        if x[1] in dlList:
            dlList.remove(x[1])
        else :
            pass
    elif x[0] == 'deleteFirst':
        dlList.pop(0)
    elif x[0] == 'deleteLast':
        dlList.pop()
    else:
        pass

print(dlList.pop(0),end="")
for x in dlList:
    print(" "+x,end="")
print()

Submission status: Time Limit Exceeded

Solution code:
def operation(command, arrey):
    if command[0] == 'insert':
        arrey.insert(0,command[1])
        return arrey
    elif command[0] == 'delete':
        if command[1] in arrey:
            arrey.remove(command[1])
    elif command[0] == 'deleteFirst':
        arrey.pop(0)
    elif command[0] == 'deleteLast':
        arrey.pop(-1)


n = int(input())
list = []

for i in range(n):
    command = input().split()
    operation(command,list)

for i in range(len(list)):
    print(list[i]) if i == len(list)-1 else print(list[i]+' ',end='')