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 Node:
    def __init__(self, key):
        self.key = key
        self.prev = None
        self.next = None
    

class MyDLL:
    def __init__(self):
        # 番兵：仮想的な先頭
        self.sentinel = Node(None)
        self.sentinel.prev = self.sentinel
        self.sentinel.next = self.sentinel
    
    def insert(self, x):
        # 連結リストの先頭にキー x を持つ要素を継ぎ足す。
        new = Node(x)
        new.next = self.sentinel.next # 新しく先頭.next = 番兵.next = 今まで先頭
        self.sentinel.next.prev = new # 番兵.next.prev = 今まで先頭.prev = 新しく先頭
        self.sentinel.next = new      # 番兵.next = 新しく先頭
        new.prev = self.sentinel      # 新しく先頭.prev = 番兵

    def listSearch(self, x):
        cursor = self.sentinel.next # 番兵の次の要素から巡る
        while (cursor != self.sentinel) & (cursor.key != x):
            cursor = cursor.next
        return cursor
    
    def deleteNode(self, node):
        # あるノードを削除するとき
        # 例 A - B - C でBを削除
        if node!=self.sentinel: # Bは番兵ではないことを確認
            node.prev.next = node.next # B.prev.next = A.next = B.next = C
            node.next.prev = node.prev # B.next.prev = C.prev = B.prev = A

    def deleteKey(self, x):
        self.deleteNode(self.listSearch(x))
        
    def deleteFirst(self):
        # リストの先頭の要素を削除する。
        self.deleteNode(self.sentinel.next)
        
    def deleteLast(self):
        # リストの末尾の要素を削除する。        
        self.deleteNode(self.sentinel.prev)
        
    def print(self):
        cursor = self.sentinel.next
        key_list = []
        while cursor != self.sentinel:
            key_list.append(cursor.key)
            cursor = cursor.next   
        print(' '.join(key_list))


def main():
    # input()
    # n = 1786329# int(input())
    n = int(input())
    commands = (input() for i in range(n)) # generator式リスト内包表記
    dll = MyDLL()
    for c in commands:
        if 'insert' in c:
            dll.insert(c.split()[1])
        elif 'First' in c:
            dll.deleteFirst()
        elif 'Last' in c:
            dll.deleteLast()
        elif 'delete' in c:
            dll.deleteKey(c.split()[1])
        else:
            print("the command dont't exist")
            print(c)
    dll.print()
main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

dlist = []

for i in range(n):
    arr = input().split()
    cmd = ''
    key = 0
    cmd = arr[0]
    if len(arr) > 1:
        key = int(arr[1])
    if cmd == 'insert':
        dlist.insert(0, key)
    elif cmd == 'delete':
        if dlist.count(key) > 0:
            idx = dlist.index(key)
            if idx != -1:
                dlist.pop(idx)
    elif cmd == 'deleteFirst':
        dlist.pop(0)
    elif cmd == 'deleteLast':
        dlist.pop(-1)

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

dlist = []

for i in range(n):
    arr = input().split()
    cmd = ''
    key = 0
    cmd = arr[0]
    if len(arr) > 1:
        key = arr[1]
    if cmd == 'insert':
        dlist.insert(0, key)
    elif cmd == 'delete':
        if dlist.count(key) > 0:
            idx = dlist.index(key)
            if idx != -1:
                dlist.pop(idx)
    elif cmd == 'deleteFirst':
        dlist.pop(0)
    elif cmd == 'deleteLast':
        dlist.pop(-1)

print(' '.join(dlist))

Submission status: Time Limit Exceeded

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

for _ in range(n):
  op = list(map(str,input().split()))
  if(op[0] == "insert"):
    dList.appendleft(op[1])
  elif(op[0] == "delete"):
    try:
        dList.remove(op[1])
    except:
        pass
  elif(op[0] == "deleteFirst"):
    dList.popleft()
  elif(op[0] == "deleteLast") :
    dList.pop()

print(*dList)

Submission status: Time Limit Exceeded

Solution code:
array = []
def func_insert(num):
    array.insert(0, num)

def func_delete(num):
    try:
        array.remove(num)
    except ValueError:
        pass

def func_deleteFirst():
    array.pop(0)

def func_deleteLast():
    array.pop()


n = int(input())
for _ in range(n):
    command = input().split()
    if command[0] == 'insert':
        func_insert(command[1])

    elif command[0] == 'delete':
        func_delete(command[1])

    elif command[0] == 'deleteFirst':
        func_deleteFirst()

    else:
        func_deleteLast()

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

Submission status: Time Limit Exceeded

Solution code:
array = []
def func_insert(num):
    array.insert(0, num)

def func_delete(num):
    try:
        array.remove(num)
    except ValueError:
        pass

def func_deleteFirst():
    array.pop(0)

def func_deleteLast():
    array.pop()


n = int(input())
for _ in range(n):
    command = input().split()
    if command[0].startswith('i'):
        func_insert(command[1])

    elif command[0].startswith('d') and command[0].endswith('e'):
        func_delete(command[1])

    elif command[0] == 'deleteFirst':
        func_deleteFirst()

    else:
        func_deleteLast()

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

Submission status: Time Limit Exceeded

Solution code:
array = []
def func_insert(num):
    array.insert(0, num)

def func_delete(num):
    try:
        array.remove(num)
    except ValueError:
        pass

def func_deleteFirst():
    array.pop(0)

def func_deleteLast():
    array.pop()


n = int(input())
for _ in range(n):
    command = input().split()
    if command[0].startswith('i'):
        func_insert(command[1])

    elif command[0].startswith('d') and command[0].endswith('e'):
        func_delete(command[1])

    elif command[0][6] == 'F':
        func_deleteFirst()

    else:
        func_deleteLast()

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

Submission status: Time Limit Exceeded

Solution code:
class node:
    def __init__(self, key):
        self.key = key
        self.prev = self
        self.next = self

nil = node(0)

def listSearch(key):
    cur = nil.next
    while(1):
        if cur == nil: break
        if cur.key == key:
            return  cur
        cur = cur.next
    return None

def deleteNode(el):
    el.prev.next = el.next
    el.next.prev = el.prev
    del(el)

def deleteFirst():
    if nil.next == nil: return
    deleteNode(nil.next)

def deleteLast():
    if nil.prev == nil: return
    deleteNode(nil.prev)

def delete(key):
    el = listSearch(key)
    if el is not None: deleteNode(el)

def insert(key):
    el = node(key)
    el.next = nil.next
    el.prev = nil
    nil.next = el
    el.next.prev = el

def main():
    n = input()
    for i in range(n):
        com = raw_input()
        if com[0] == 'i':
            insert(int(com.split()[1]))
        elif com[0] == 'd':
            if com[6] == 'F':
                deleteFirst()
            elif com[6] == 'L':
                deleteLast()
            else:
                delete(int(com.split()[1]))

    els = list()
    cur = nil.next
    while(1):
        if (cur == nil): break
        els.append(cur.key)
        cur = cur.next
    if len(els) > 1: print(" ".join(map(str, els)))
    elif len(els) == 1: print(str(els[0]))
    else: print("")

main()

Submission status: Time Limit Exceeded

Solution code:
class node:
    def __init__(self, key = 0):
        self.key = key
        self.prev = self
        self.next = self

nil = node()

def deleteNode(el):
    el.prev.next = el.next
    el.next.prev = el.prev
    del(el)

def deleteFirst():
    if nil.next == nil: return
    deleteNode(nil.next)

def deleteLast():
    if nil.prev == nil: return
    deleteNode(nil.prev)

def delete(key):
    cur = nil.next
    while cur != nil:
        if cur.key == key:
            deleteNode(cur)
            break
        cur = cur.next

def insert(key):
    el = node(key)
    el.next = nil.next
    el.prev = nil
    nil.next = el
    el.next.prev = el

def main():
    n = input()
    for i in range(n):
        com = raw_input()
        if com[0] == 'i':
            insert(int(com.split()[1]))
        elif com == 'deleteFirst':
            deleteFirst()
        elif com == 'deleteLast':
            deleteLast()
        else:
            delete(int(com.split()[1]))

    els = list()
    cur = nil.next
    while(cur != nil):
        els.append(cur.key)
        cur = cur.next
    if len(els) > 1: print(" ".join(map(str, els)))
    elif len(els) == 1: print(str(els[0]))
    else: print("")

main()

Submission status: Time Limit Exceeded

Solution code:
class node:
    def __init__(self, key = u'0'):
        self.key = key
        self.prev = self
        self.next = self

nil = node()

def deleteNode(el):
    el.prev.next = el.next
    el.next.prev = el.prev
    del(el)

def deleteFirst():
    if nil.next == nil: return
    deleteNode(nil.next)

def deleteLast():
    if nil.prev == nil: return
    deleteNode(nil.prev)

def delete(key):
    cur = nil.next
    while cur != nil:
        if cur.key == key:
            deleteNode(cur)
            break
        cur = cur.next

def insert(key):
    el = node(key)
    el.next = nil.next
    el.prev = nil
    nil.next = el
    el.next.prev = el

def main():
    n = input()
    for i in range(n):
        com = raw_input()
        if com[0] == 'i':
            insert(com.split()[1])
        elif com == 'deleteFirst':
            deleteFirst()
        elif com == 'deleteLast':
            deleteLast()
        else:
            delete(com.split()[1])

    els = list()
    cur = nil.next
    while cur != nil:
        els.append(cur.key)
        cur = cur.next
    if len(els) > 1: print(" ".join(els))
    else: print(els[0])

main()

Submission status: Time Limit Exceeded

Solution code:
n = input()
array = []
for i in range(n):
	inst = raw_input().split()
	if inst[0] == "insert":
		array.insert(0,inst[1])
	if inst[0] == "delete" and inst[1] in array:
		array.remove(inst[1])
	if inst[0] == "deleteFirst" and len(array) > 0:
		del array[0]
	if inst[0] == "deleteLast" and len(array) > 0:
		del array[len(array)-1]
print " ".join(i for i in array)

Submission status: Time Limit Exceeded

Solution code:
n = input()
array = []
for i in range(n):
	inst = raw_input().split()
	if inst[0] == "insert":
		array.insert(0,inst[1])
	if inst[0] == "delete" and inst[1] in array:
		array.remove(inst[1])
	if inst[0] == "deleteFirst":
		del array[0]
	if inst[0] == "deleteLast":
		del array[len(array)-1]
print " ".join(i for i in array)

Submission status: Time Limit Exceeded

Solution code:
n = input()
array = []
for i in range(n):
	inst = raw_input().split()
	if inst[0] == "insert":
		array.insert(0,inst[1])
	if inst[0] == "delete":
		if inst[1] in array:
			array.remove(inst[1])
	if inst[0] == "deleteFirst":
		del array[0]
	if inst[0] == "deleteLast":
		del array[len(array)-1]
print " ".join(i for i in array)

Submission status: Time Limit Exceeded

Solution code:
n = input()
array = []
for i in range(n):
	inst = raw_input().split()
	if inst[0] == "insert":
		array.insert(0,inst[1])
	if inst[0] == "delete":
		if inst[1] in array:
			array.remove(inst[1])
	if inst[0] == "deleteFirst":
		array.pop(0)
	if inst[0] == "deleteLast":
		array.pop(len(array)-1)
print " ".join(i for i in array)

Submission status: Time Limit Exceeded

Solution code:
n = input()
array = []
for i in range(n):
	inst = raw_input().split()
	if inst[0] == "insert":
		array.append(inst[1])
	if inst[0] == "delete":
		if inst[1] in array:
			x = array[::-1].index(inst[1])
			if x != -1:
				del array[-x-1]
	if inst[0] == "deleteFirst":
		array.pop()
	if inst[0] == "deleteLast":
		array.pop(0)
print " ".join(map(str,array[::-1]))

Submission status: Time Limit Exceeded

Solution code:
n = input()
array = []
for i in range(n):
	inst = raw_input().split()
	if inst[0] == 'insert':
		array.append(int(inst[1]))
	if inst[0] == "delete":
		if int(inst[1]) in array:
			x = array[::-1].index(int(inst[1]))
			if x != -1 and int(inst[1]):
				del array[-x-1]
	if inst[0] == "deleteFirst":
		array.pop()
	if inst[0] == "deleteLast":
		array.pop(0)
print " ".join(map(str,array[::-1]))

Submission status: Time Limit Exceeded

Solution code:
n = input()
array = []
for i in range(n):
	inst = raw_input().split()
	if inst[0][0] == 'i':
		array.append(int(inst[1]))
	elif inst[0] == "delete":
		if int(inst[1]) in array:
			x = array[::-1].index(int(inst[1]))
			if x != -1 and int(inst[1]):
				del array[-x-1]
	elif inst[0][6] == 'F':
		array.pop()
	else:
		array.pop(0)
print " ".join(map(str,array[::-1]))

Submission status: Time Limit Exceeded

Solution code:
n=int(raw_input())
l = []
for x in xrange(n):
    msg = raw_input().split()
    cmd = msg[0]
    if cmd == "insert":
        l.insert(0, msg[1])
    elif cmd == "delete":
        try:
            l.remove(msg[1])            
        except Exception, e:
            pass
    elif cmd == "deleteFirst":
        l.pop(0)
    elif cmd == "deleteLast":
        l.pop()

for x in l:
    print x,

Submission status: Time Limit Exceeded

Solution code:
n=int(raw_input())
l = []
for x in xrange(n):
    msg = raw_input().split()
    cmd = msg[0]
    if cmd == "insert":
        l.insert(0, msg[1])
    elif cmd == "delete":
        if msg[1] in l:
            l.remove(msg[1])
    elif cmd == "deleteFirst":
        l.pop(0)
    elif cmd == "deleteLast":
        l.pop()

for x in l:
    print x,

Submission status: Time Limit Exceeded

Solution code:
n=int(raw_input())
l = []
for x in xrange(n):
    m = raw_input().split()
    c = m[0][-4]
    if c == "s":
        l.insert(0, m[1])
    elif c == "i":
        l.pop(0)
    elif c == "L":
        l.pop()
    else:
        try:
            l.remove(m[1])            
        except Exception, e:
            pass

print " ".join(l)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
que = deque()
for i in range(int(input())):
    command = input()
    if command == 'deleteFirst':
        que.popleft()
    elif command == 'deleteLast':
        que.pop()
    else:
        c, n = command.split()
        if c == 'insert':
            que.appendleft(int(n))
        elif c == 'delete':
            try:
                que.remove(int(n))
            except:
                pass
print(' '.join([str(x) for x in que]))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
que = deque()
for i in range(int(input())):
    command = input()
    if command == 'deleteFirst':
        que.popleft()
    elif command == 'deleteLast':
        que.pop()
    else:
        c, n = command.split()
        if c == 'insert':
            que.appendleft(n)
        elif c == 'delete':
            try:
                que.remove(n)
            except:
                pass
print(' '.join(que))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
que = deque()
for i in range(int(input())):
    command = input()
    if command[6] == 'F':
        que.popleft()
    elif command[6] == 'L':
        que.pop()
    else:
        c, n = command.split()
        if c[0] == 'i':
            que.appendleft(n)
        elif c[0] == 'd':
            try:
                que.remove(n)
            except:
                pass
print(' '.join(que))

Submission status: Time Limit Exceeded

Solution code:
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_3_C
# Doubly Linked List
# Result:
import sys

class DoublyLikedList(object):
    def __init__(self):
        self.head = None
        self.tail = None

    def __str__(self):
        vals = []
        pointer = self.head
        while pointer != None:
            vals.append(str(pointer.val))
            pointer = pointer.next

        if len(vals) == 0:
            return ''
        else:
            return ' '.join(vals)

    def insert(self, x):
        itm = ListItem(x)
        if self.head == None:
            self.head = itm
            self.tail = itm
        else:
            itm.next = self.head
            self.head.prev = itm
            self.head = itm

    def delete(self, x):
        pointer = self.head
        while pointer != None:
            if pointer.val == x:
                if pointer == self.head:
                    self.delete_first()
                elif pointer == self.tail:
                    self.delete_last()
                else:
                    if pointer.prev != None:
                        pointer.prev.next = pointer.next
                    if pointer.next != None:
                        pointer.next.prev = pointer.prev
                break
            pointer = pointer.next

    def delete_first(self):
        if self.head == None:
            return
        self.head = self.head.next
        if self.head == None:
            self.tail = None
            return
        else:
            self.head.prev = None

    def delete_last(self):
        if self.tail == None:
            return
        if self.tail.prev == None:
            self.tail = None
            self.head = None
        else:
            self.tail = self.tail.prev
            self.tail.next = None

class ListItem(object):
    def __init__(self, x):
        self.val = x
        self.prev = None
        self.next = None

### main
lst = DoublyLikedList()
n = int(sys.stdin.readline().rstrip())
for e in range(0, n):
    strs = sys.stdin.readline().rstrip().split(' ')
    if strs[0] == 'insert':
        lst.insert(int(strs[1]))
    elif strs[0] == 'delete':
        lst.delete(int(strs[1]))
    elif strs[0] == 'deleteFirst':
        lst.delete_first()
    elif strs[0] == 'deleteLast':
        lst.delete_last()

print lst

Submission status: Time Limit Exceeded

Solution code:
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_3_C
# Doubly Linked List
# Result: Time Limit Exceeded on test_case_10
import sys

class DoublyLikedList(object):
    def __init__(self):
        self.head = None
        self.tail = None

    def __str__(self):
        vals = []
        pointer = self.head
        while pointer != None:
            vals.append(str(pointer.val))
            pointer = pointer.next

        if len(vals) == 0:
            return ''
        else:
            return ' '.join(vals)

    def insert(self, x):
        itm = ListItem(x)
        if self.head == None:
            self.head = itm
            self.tail = itm
        else:
            itm.next = self.head
            self.head.prev = itm
            self.head = itm

    def delete(self, x):
        pointer = self.head
        while pointer != None:
            if pointer.val == x:
                if pointer == self.head:
                    self.delete_first()
                elif pointer == self.tail:
                    self.delete_last()
                else:
                    if pointer.prev != None:
                        pointer.prev.next = pointer.next
                    if pointer.next != None:
                        pointer.next.prev = pointer.prev
                break
            pointer = pointer.next

    def delete_first(self):
        if self.head == None:
            return
        self.head = self.head.next
        if self.head == None:
            self.tail = None
            return
        else:
            self.head.prev = None

    def delete_last(self):
        if self.tail == None:
            return
        if self.tail.prev == None:
            self.tail = None
            self.head = None
        else:
            self.tail = self.tail.prev
            self.tail.next = None

class ListItem(object):
    def __init__(self, x):
        self.val = x
        self.prev = None
        self.next = None

### main
lst = DoublyLikedList()
n = int(sys.stdin.readline().rstrip())
for e in range(0, n):
    strs = sys.stdin.readline().rstrip().split(' ')
    if strs[0] == 'insert':
        lst.insert(int(strs[1]))
    elif strs[0] == 'delete':
        lst.delete(int(strs[1]))
    elif strs[0] == 'deleteFirst':
        lst.delete_first()
    elif strs[0] == 'deleteLast':
        lst.delete_last()

print lst

Submission status: Time Limit Exceeded

Solution code:
n = int( raw_input( ) )
eles = []
for i in range( n ):
	op = raw_input( ).split( " " )
	if "insert" == op[0]:
		eles.insert( 0, op[1] )
	elif "delete" == op[0]:
		if op[1] in eles:
			eles.remove( op[1] )
	elif "deleteFirst" == op[0] :
		del eles[0]
	elif "deleteLast" == op[0]:
		eles.pop( )

print( " ".join( eles ) )

Submission status: Time Limit Exceeded

Solution code:
n = int( raw_input( ) )
eles = []
for i in range( n ):
	op = raw_input( ).split( " " )
	if "insert" == op[0]:
		eles.insert( 0, op[1] )
	elif "delete" == op[0]:
		for i, val in enumerate( eles ):
			if op[1] == val:
				eles.pop( i )
				break
	elif "deleteFirst" == op[0] :
		del eles[0]
	elif "deleteLast" == op[0]:
		eles.pop( )

print( " ".join( eles ) )

Submission status: Time Limit Exceeded

Solution code:
n = int( raw_input( ) )
eles = []
for i in range( n ):
	op = raw_input( ).split( " " )
	if "insert" == op[0]:
		eles.insert( 0, op[1] )
	elif "delete" == op[0]:
		for i,val in enumerate( eles ):
			if op[1] == val:
				eles.pop( i )
				break
	elif "deleteFirst" == op[0] :
		eles.pop( 0 )
	elif "deleteLast" == op[0]:
		eles.pop( )

print( " ".join( eles ) )

Submission status: Time Limit Exceeded

Solution code:
n = int( raw_input() )
eles = []
for i in range( n ):
	op = raw_input( ).split( " " )
	if "insert" == op[0]:
		eles.insert( 0, op[1] )
	elif "delete" == op[0]:
		if op[1] in eles:
			eles.remove( op[1] )
	elif "deleteFirst" == op[0] :
		eles.pop( 0 )
	elif "deleteLast" == op[0]:
		eles.pop( )

print( " ".join( eles ) )

Submission status: Time Limit Exceeded

Solution code:
import sys

lines = sys.stdin.readlines()
n = int( lines[0] )
eles = []
for i in range( 1, n+1 ):
	op = lines[i].rstrip().split( " " )
	if "insert" == op[0]:
		eles.insert( 0, op[1] )
	elif "delete" == op[0]:
		if op[1] in eles:
			eles.remove( op[1] )
	elif "deleteFirst" == op[0] :
		eles.pop( 0 )
	elif "deleteLast" == op[0]:
		eles.pop( )

print( " ".join( eles ) )

Submission status: Time Limit Exceeded

Solution code:
import sys

lines = sys.stdin.readlines()
n = int( lines[0] )
eles = []
for i in range( 1, n+1 ):
	op = lines[i].rstrip().split( " " )
	if "insert" == op[0]:
		eles.insert( 0, op[1] )
	elif "delete" == op[0]:
		if op[1] in eles:
			eles.remove( op[1] )
	elif "deleteFirst" == op[0] :
		eles.pop( 0 )
	elif "deleteLast" == op[0]:
		eles.pop( )

output = []
for val in eles:
	output.append( val )
	output.append( " " )

output.pop( )
print( "".join( output ) )

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

n = int( input( ) )
que = deque( )
for i in range( n ):
	op = input( ).split( " " )
	if "insert" == op[0]:
		que.appendleft( op[1] )
	elif "delete" == op[0]:
		if op[1] in que:
			que.remove( op[1] )
	elif "deleteFirst" == op[0] :
		que.popleft( )
	elif "deleteLast" == op[0]:
		que.pop( )

print( " ".join( que ) )

Submission status: Time Limit Exceeded

Solution code:
ans=[]
n=int(raw_input())
while n:
    n-=1
    s=map(str,raw_input().split())
    if s[0][0]=='i':
        ans.insert(0,s[1])
    elif s[0][-3]=='r':
        ans.pop(0)
    elif s[0][-3]=='a':
        ans.pop()
    else:
        for i,j in enumerate(ans):
            if j==s[1]:
                ans.pop(i)
                break
for i in ans:
    print i,

Submission status: Time Limit Exceeded

Solution code:
import sys
i = 0
ans=[]
n=int(raw_input())
while n:
    n-=1
    s=sys.stdin.readline().rstrip().split(' ')
    if s[0][0]=='i':
        ans.insert(0,s[1])
    elif s[0][-3]=='r':
        ans.pop(0)
    elif s[0][-3]=='a':
        ans.pop()
    else:
        for i,j in enumerate(ans):
            if j==s[1]:
                ans.pop(i)
                break
print ' '.join(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
n=int(raw_input())
ans=[]
while n:
    n-=1
    s=sys.stdin.readline().rstrip().split(' ')
    if s[0][0]=='i':
        ans.insert(0,s[1])
    elif s[0][-3]=='r':
        ans.pop(0)
    elif s[0][-3]=='a':
        ans.pop()
    else:
        for i,j in enumerate(ans):
            if j==s[1]:
                ans.pop(i)
                break
sys.stdout.write(' '.join(ans))
print

Submission status: Time Limit Exceeded

Solution code:
import sys
n=int(raw_input())
ans=[]
while n:
    n-=1
    s=sys.stdin.readline().rstrip().split(' ')
    if s[0][0]=='i':
        ans.insert(0,s[1])
    elif s[0][-3]=='r':
#        ans.pop(0)
        del ans[0]
    elif s[0][-3]=='a':
        del ans[-1]
#        ans.pop()
    else:
        for i,j in enumerate(ans):
            if j==s[1]:
                del ans[i]
#                ans.pop(i)
                break
sys.stdout.write(' '.join(ans))
print

Submission status: Time Limit Exceeded

Solution code:
class LinkedList():
  def __init__(self):
    self.linked_list = []
  def insert(self,x):
    self.linked_list.insert(0,x)
  def delete(self,x):
    try:
       self.linked_list.remove(x)
    except:
       pass
  def deleteFirst(self):
    self.linked_list.pop(0)
  def deleteLast(self):
    self.linked_list.pop(-1)
  def get(self):
    return ' '.join(self.linked_list)

linkedlist = LinkedList()
for i in range(int(raw_input())):
  line = raw_input()
  if line.count(' '):
    c, x = line.split(' ')
  else:
    c = line
    x = ''
  if c == "insert":
    linkedlist.insert(x)
  elif c == "delete":
     linkedlist.delete(x)
  elif c == "deleteFirst":
     linkedlist.deleteFirst()
  else:
     linkedlist.deleteLast()

print linkedlist.get()

Submission status: Time Limit Exceeded

Solution code:
import collections
n = int(input())
que = collections.deque()
for _ in range(n):
    command = input()
    if 'insert' in command:
        command, num = command.split()
        que.appendleft(int(num))
    elif 'deleteFirst' in command:
        que.popleft()
    elif 'deleteLast' in command:
        que.pop()
    elif 'delete' in command:
        command, num = command.split()
        if int(num) in que:
            que.remove(int(num))
print(' '.join(map(str, que)))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

n = int(input())
que = deque()

for i in range(n):
    command = input()
    if command == "deleteFirst":
        que.popleft()
    elif command == "deleteLast":
        que.pop()
    else:
        a, b = command.split()
        if a == "insert":
            que.appendleft(b)
        else:
            try:
                que.remove(b)
            except:
                pass
print(*que)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

n = int(input())
que = deque()

for i in range(n):
    command = input()
    if command == "deleteFirst":
        que.popleft()
    elif command == "deleteLast":
        que.pop()
    elif command[0] == "i":
        que.appendleft(command[7:])
    else:
        try:
            que.remove(command[7:])
        except:
            pass
print(*que)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

n = int(input())
que = deque()

for i in range(n):
    command = input()
    if command[0] == "i":
        que.appendleft(command[7:])
    elif "deleteFirst" in command:
        que.popleft()
    elif "deleteLast" in command:
        que.pop()
    else:
        try:
            que.remove(command[7:])
        except:
            pass
print(*que)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

n = int(input())
que = deque()

for i in range(n):
    command = input()
    if command[0] == "i":
        que.appendleft(command[7:])
    elif "deleteFirst" in command:
        que.popleft()
    elif "deleteLast" in command:
        que.pop()
    else:
        try:
            que.remove(command[7:])
        except:
            pass
print(" ".join(que))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

n = int(input())
que = deque()

for i in range(n):
    command = input()
    if command[0] == "i":
        que.appendleft(command[7:])
    elif command[6] == " ":
        try:
            que.remove(command[7:])
        except:
            pass
    elif command[6] == "F":
        que.popleft()
    else:
        que.pop()
print(" ".join(que))

Submission status: Time Limit Exceeded

Solution code:
n = int(raw_input())
R = []
for i in xrange(n):
    L = raw_input().split()
    order = L[0]
    value = 0
    if len(L) > 1:
        value = int(L[1])
    if order == 'insert':
        R.insert(0, value)
    if order == 'delete' and value in R:
        R.remove(value)
    if order == 'deleteFirst':
        R.pop(0)
    if order == 'deleteLast':
        R.pop()
print ' '.join(map(str, R))

Submission status: Time Limit Exceeded

Solution code:
list=[]
n=int(raw_input())
for i in range(n):
	inst=raw_input().split()
	
	if inst[0]=='insert':
		list.insert(0,int(inst[1]))
	elif inst[0]=='delete':
			try:
				list.remove(int(inst[1]))
			except:
				pass
	elif inst[0]=='deleteFirst':
		del list[0]
	else :list.pop()

for i in list:
	print i,

Submission status: Time Limit Exceeded

Solution code:
list=[]
n=int(raw_input())
for i in range(n):
	inst=raw_input().split()
	
	if inst[0][0]=='i':
		list.insert(0,int(inst[1]))
	elif inst[0]=='delete':
			try:
				list.remove(int(inst[1]))
			except:
				pass
	elif inst[0]=='deleteFirst':
		del list[0]
	else :list.pop()

for i in list:
	print i,

Submission status: Time Limit Exceeded

Solution code:
list=[]
n=int(raw_input())
for i in range(n):
	inst=raw_input().split()
	
	if inst[0][0]=='i':
		list.insert(0,int(inst[1]))
	elif inst[0]=='delete':
			try:
				list.remove(int(inst[1]))
			except:
				pass
	elif inst[0]=='deleteFirst':
		del list[0]
	else :list.pop()


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

Submission status: Time Limit Exceeded

Solution code:
list=[]
n=int(raw_input())
for i in range(n):
	inst=raw_input().split()
	
	if inst[0][0]=='i':
		list.insert(0,inst[1])
	elif inst[0]=='delete':
			try:
				list.remove(inst[1])
			except:
				pass
	elif inst[0]=='deleteFirst':
		del list[0]
	else :list.pop()


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

Submission status: Time Limit Exceeded

Solution code:
list=[]
n=int(raw_input())
for i in range(n):
	inst=raw_input().split()
	
	if inst[0][0]=='i':
		list.insert(0,inst[1])
	elif inst[0]=='delete':
			try:
				list.remove(inst[1])
			except:
				pass
	elif inst[0]=='deleteFirst':
			list.pop(0)
	else :list.pop()


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