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:
n = int(raw_input())
x = [0 for i in range(n)]
pf = 0
pb = 0

li = []
for i in range(n):
    s = (raw_input().split())
    if s[0] == 'insert':
        pf += 1
        x[pf]=int(s[1])
    elif s[0] == 'delete':
        try:
            p = x[pf::-1].index(int(s[1]))
            x[pf-p] = 0
            while x[pf]==0:
                pb -= 1
            while x[pb]==0:
                pb += 1
        except:
            pass
    elif s[0] == 'deleteFirst':
        x[pf]=0
        pf -= 1
    elif s[0] == 'deleteLast':
        x[pb] = 0
        pb += 1
for e in x[pb:pf+1][::-1]:
    if e != 0:
        print e,

Submission status: Time Limit Exceeded

Solution code:
import sys

A=[[0,0,0]]
size = 0

def insert(A,x):
    global size

    fp=A[0][1]
    try:
        i=[e[0] for e in A].index(-1)
        A[i]=[x,fp,0]
        A[fp][2]=i
        A[0][1]=i
    except:
        A.append([x,fp,0])
        A[fp][2]=len(A)-1
        A[0][1]=len(A)-1     
    size += 1    

def delete(A,x):
    global size
    p =A[0][1]
    while A[p][0]!=x:
        p=A[p][1]
        if p==0: return
    fp=A[p][1]
    bp=A[p][2]
    A[fp][2]=bp
    A[bp][1]=fp
    A[p]=[-1,-1,-1]
    size -= 1
    return

def deletefirst(A):
    global size
    if size == 0: return
    p=A[0][1]
    fp=A[p][1]
    A[0][1]=fp
    A[fp][2]=0
    A[p]=[-1,-1,-1]
    size -= 1
    return 0

def deletelast(A):
    global size
    if size == 0: return
    p=A[0][2]
    bp=A[p][2]
    A[0][2]=bp
    A[bp][1]=0
    A[p]=[-1,-1,-1]
    size -= 1
    return 0

n = int(raw_input())
for i in range(n):
    s = (raw_input().split())
    if s[0] == 'insert':
        insert(A,int(s[1]))
    elif s[0] == 'delete':
        delete(A,int(s[1]))
    elif s[0] == 'deleteFirst':
        deletefirst(A)
    elif s[0] == 'deleteLast':
        deletelast(A)

e = A[0][1]
while e!=0:
    print A[e][0],
    e = A[e][1]

Submission status: Time Limit Exceeded

Solution code:
import sys

A=[[0,0,0]]
size = 0

def insert(A,x):
    global size

    fp=A[0][1]
    try:
        i=[e[0] for e in A].index(-1)
        A[i]=[x,fp,0]
        A[fp][2]=i
        A[0][1]=i
    except:
        A.append([x,fp,0])
        A[fp][2]=len(A)-1
        A[0][1]=len(A)-1     
    size += 1    

def delete(A,x):
    global size
    p =A[0][1]
    while A[p][0]!=x:
        p=A[p][1]
        if p==0: return
    fp=A[p][1]
    bp=A[p][2]
    A[fp][2]=bp
    A[bp][1]=fp
    A[p]=[-1,-1,-1]
    size -= 1
    return

def deletefirst(A):
    global size
    if size == 0: return
    p=A[0][1]
    fp=A[p][1]
    A[0][1]=fp
    A[fp][2]=0
    A[p]=[-1,-1,-1]
    size -= 1
    return 0

def deletelast(A):
    global size
    if size == 0: return
    p=A[0][2]
    bp=A[p][2]
    A[0][2]=bp
    A[bp][1]=0
    A[p]=[-1,-1,-1]
    size -= 1
    return 0

n = int(raw_input())
for i in range(n):
    s = (raw_input().split())
    if s[0] == 'insert':
        insert(A,s[1])
    elif s[0] == 'delete':
        delete(A,s[1])
    elif s[0] == 'deleteFirst':
        deletefirst(A)
    elif s[0] == 'deleteLast':
        deletelast(A)

e = A[0][1]
while e!=0:
    print A[e][0],
    e = A[e][1]

Submission status: Time Limit Exceeded

Solution code:
import sys

A=[[0,0,0]]
size = 0

def insert(A,x):
    global size

    fp=A[0][1]
    A.append([x,fp,0])
    A[fp][2]=len(A)-1
    A[0][1]=len(A)-1     
    size += 1    

def delete(A,x):
    global size
    p =A[0][1]
    while A[p][0]!=x:
        p=A[p][1]
        if p==0: return
    fp=A[p][1]
    bp=A[p][2]
    A[fp][2]=bp
    A[bp][1]=fp
    A[p]=[-1,-1,-1]
    size -= 1
    return

def deletefirst(A):
    global size
    if size == 0: return
    p=A[0][1]
    fp=A[p][1]
    A[0][1]=fp
    A[fp][2]=0
    A[p]=[-1,-1,-1]
    size -= 1
    return 0

def deletelast(A):
    global size
    if size == 0: return
    p=A[0][2]
    bp=A[p][2]
    A[0][2]=bp
    A[bp][1]=0
    A[p]=[-1,-1,-1]
    size -= 1
    return 0

n = int(raw_input())
for i in range(n):
    s = (raw_input().split())
    if s[0] == 'insert':
        insert(A,s[1])
    elif s[0] == 'delete':
        delete(A,s[1])
    elif s[0] == 'deleteFirst':
        deletefirst(A)
    elif s[0] == 'deleteLast':
        deletelast(A)

e = A[0][1]
while e!=0:
    print A[e][0],
    e = A[e][1]

Submission status: Time Limit Exceeded

Solution code:
import sys

A=[[0,0,0]]
buf=[]
size = 0

def insert(A,x):
    global size,buf
    fp=A[0][1]
    try:
        if buf==[]:
            buf = [i for i,e in enumerate(A) if e==-1]
        else:
            i = buf[-1]
            buf = buf[:-1]
        A[i]=[x,fp,0]
        A[fp][2]=i
        A[0][1]=i
    except:
        A.append([x,fp,0])
        A[fp][2]=len(A)-1
        A[0][1]=len(A)-1     
    size += 1    

def delete(A,x):
    global size
    p =A[0][1]
    while A[p][0]!=x:
        p=A[p][1]
        if p==0: return
    fp=A[p][1]
    bp=A[p][2]
    A[fp][2]=bp
    A[bp][1]=fp
    A[p]=[-1,-1,-1]
    size -= 1
    return

def deletefirst(A):
    global size
    if size == 0: return
    p=A[0][1]
    fp=A[p][1]
    A[0][1]=fp
    A[fp][2]=0
    A[p]=[-1,-1,-1]
    size -= 1
    return 0

def deletelast(A):
    global size
    if size == 0: return
    p=A[0][2]
    bp=A[p][2]
    A[0][2]=bp
    A[bp][1]=0
    A[p]=[-1,-1,-1]
    size -= 1
    return 0

n=int(raw_input())
while n:
    s = (raw_input().split())
    if s[0] == 'insert':
        insert(A,s[1])
    elif s[0] == 'delete':
        delete(A,s[1])
    elif s[0] == 'deleteFirst':
        deletefirst(A)
    elif s[0] == 'deleteLast':
        deletelast(A)
    n -=1
    
e = A[0][1]
while e!=0:
    print A[e][0],
    e = A[e][1]

Submission status: Time Limit Exceeded

Solution code:
import sys

A=[0]
B=[[0,0]]
size = 0

def insert(A,B,x):
    global size,buf
    fp=B[0][0]
    try:
        i=A.index(-1)
        A[i]=[x]
        B[i]=[fp,0]
        B[fp][1]=i
        B[0][0]=i
    except:
        A.append(x)
        B.append([fp,0])
        B[fp][1]=len(B)-1
        B[0][0]=len(B)-1     
    size += 1    

def delete(A,B,x):
    global size
    p =B[0][0]
    while A[p]!=x:
        p=B[p][0]
        if p==0: return
    fp=B[p][0]
    bp=B[p][1]
    B[fp][1]=bp
    B[bp][0]=fp
    A[p]=[-1]
    B[p]=[-1,-1]
    size -= 1
    return

def deletefirst(A,B):
    global size
    if size == 0: return
    p=B[0][0]
    fp=B[p][0]
    B[0][0]=fp
    B[fp][1]=0
    A[p]=[-1]
    B[p]=[-1,-1]
    size -= 1
    return 0

def deletelast(A,B):
    global size
    if size == 0: return
    p=B[0][1]
    bp=B[p][1]
    B[0][1]=bp
    B[bp][0]=0
    A[p]=[-1]
    B[p]=[-1,-1]
    size -= 1
    return 0

n=int(raw_input())
while n:
    s = (raw_input().split())
    if s[0] == 'insert':
        insert(A,B,s[1])
    elif s[0] == 'delete':
        delete(A,B,s[1])
    elif s[0] == 'deleteFirst':
        deletefirst(A,B)
    elif s[0] == 'deleteLast':
        deletelast(A,B)
    n -=1
    
e = B[0][0]
while e!=0:
    print A[e],
    e = B[e][0]

Submission status: Time Limit Exceeded

Solution code:
import sys,gc

class Deque:
    class Cell2:
        def __init__(self, x, y = None, z = None):
            self.data = x
            self.next = y
            self.prev = z

    def __init__(self):
        head = Deque.Cell2(None)
        head.next = head        
        head.prev = head
        self.size = 0
        self.head = head

    def push_front(self, x):
        h = self.head
        q = h.next
        p = Deque.Cell2(x, q, h)
        h.next = p
        q.prev = p
        self.size += 1

    def pop_back(self):
        if self.size == 0: raise IndexError
        h = self.head
        q = h.prev
        p = q.prev
        p.next = h
        h.prev = p
        self.size -= 1
        return q.data

    def pop_front(self):
        if self.size == 0: raise IndexError
        h = self.head
        q = h.next
        p = q.next
        p.prev = h
        h.next = p
        self.size -= 1
        return q.data

    def delete(self,x):
        h=self.head.next
        while h.data!=x:
            h=h.next
            if h==self.head: return
        q=h.next
        p=h.prev
        q.prev=p
        p.next=q
        self.size -= 1
        return

    def __str__(self):
        if self.size == 0: return
        buff = ''
        n = self.size
        cp = self.head.next
        while n > 1:
            buff += '%s ' % cp.data
            cp = cp.next
            n -= 1
        buff += '%s' % cp.data
        return buff
    
A=Deque()
n=int(raw_input())
while n:
    s = (raw_input().split())
    if s[0] == 'insert':
        A.push_front(int(s[1]))
    elif s[0] == 'delete':
        A.delete(int(s[1]))
    elif s[0] == 'deleteFirst':
        A.pop_front()
    elif s[0] == 'deleteLast':
        A.pop_back()
    n -=1
    
print A

Submission status: Time Limit Exceeded

Solution code:
import sys,gc

class Deque:
    class Cell2:
        def __init__(self, x, y = None, z = None):
            self.data = x
            self.next = y
            self.prev = z

    def __init__(self):
        head = Deque.Cell2(None)
        head.next = head        
        head.prev = head
        self.size = 0
        self.head = head

    def push_front(self, x):
        h = self.head
        q = h.next
        p = Deque.Cell2(x, q, h)
        h.next = p
        q.prev = p
        self.size += 1

    def pop_back(self):
        gc.enable()
        if self.size == 0: raise IndexError
        h = self.head
        q = h.prev
        p = q.prev
        p.next = h
        h.prev = p
        self.size -= 1
        gc.disable()
        return q.data

    def pop_front(self):
        gc.enable()
        if self.size == 0: raise IndexError
        h = self.head
        q = h.next
        p = q.next
        p.prev = h
        h.next = p
        self.size -= 1
        gc.disable()
        return q.data

    def delete(self,x):
        gc.enable()
        h=self.head.next
        while h.data!=x:
            h=h.next
            if h==self.head: return
        q=h.next
        p=h.prev
        q.prev=p
        p.next=q
        self.size -= 1
        gc.disable()
        return

    def __str__(self):
        if self.size == 0: return
        buff = ''
        n = self.size
        cp = self.head.next
        while n > 1:
            buff += '%s ' % cp.data
            cp = cp.next
            n -= 1
        buff += '%s' % cp.data
        return buff
    
gc.disable()
A=Deque()
n=int(raw_input())
while n:
    s = (raw_input().split())
    if s[0] == 'insert':
        A.push_front(int(s[1]))
    elif s[0] == 'delete':
        A.delete(int(s[1]))
    elif s[0] == 'deleteFirst':
        A.pop_front()
    elif s[0] == 'deleteLast':
        A.pop_back()
    n -=1
    
print A

Submission status: Time Limit Exceeded

Solution code:
import sys,gc

class Deque:
    class Cell2:
        def __init__(self, x, y = None, z = None):
            self.data = x
            self.next = y
            self.prev = z

    def __init__(self):
        head = Deque.Cell2(None)
        head.next = head        
        head.prev = head
        self.size = 0
        self.head = head

    def push_front(self, x):
        h = self.head
        q = h.next
        p = Deque.Cell2(x, q, h)
        h.next = p
        q.prev = p
        self.size += 1

    def pop_back(self):
        if self.size == 0: raise IndexError
        h = self.head
        q = h.prev
        p = q.prev
        p.next = h
        h.prev = p
        self.size -= 1
        return q.data

    def pop_front(self):
        if self.size == 0: raise IndexError
        h = self.head
        q = h.next
        p = q.next
        p.prev = h
        h.next = p
        self.size -= 1
        return q.data

    def delete(self,x):
        gc.enable()
        h=self.head.next
        while h.data!=x:
            h=h.next
            if h==self.head: return
        q=h.next
        p=h.prev
        q.prev=p
        p.next=q
        self.size -= 1
        gc.disable()
        return

    def __str__(self):
        if self.size == 0: return
        buff = ''
        n = self.size
        cp = self.head.next
        while n > 1:
            buff += '%s ' % cp.data
            cp = cp.next
            n -= 1
        buff += '%s' % cp.data
        return buff
    
gc.disable()
A=Deque()
n=int(raw_input())
while n:
    s = (raw_input().split())
    if s[0] == 'insert':
        A.push_front(int(s[1]))
    elif s[0] == 'delete':
        A.delete(int(s[1]))
    elif s[0] == 'deleteFirst':
        A.pop_front()
    elif s[0] == 'deleteLast':
        A.pop_back()
    n -=1
    
print A

Submission status: Time Limit Exceeded

Solution code:
import sys

    
A=[]
n=int(raw_input())
while n:
    s = (raw_input().split())
    if s[0] == 'insert':
        A.insert(0,int(s[1]))
    elif s[0] == 'delete':
        try:
            A.remove(int(s[1]))
        except:
            pass
    elif s[0] == 'deleteFirst':
        A.pop(0)
    elif s[0] == 'deleteLast':
        A.pop()
    n -=1
    
for e in A:
    print e,

Submission status: Time Limit Exceeded

Solution code:
import sys,gc

    
A=[]
n=int(raw_input())
while n:
    s = (raw_input().split())
    if s[0] == 'insert':
        A.insert(0,int(s[1]))
    elif s[0] == 'delete':
        try:
            A.remove(int(s[1]))
        except:
            pass
    elif s[0] == 'deleteFirst':
        A.pop(0)
    elif s[0] == 'deleteLast':
        A.pop()
    n -=1
    
for e in A:
    print e,

Submission status: Time Limit Exceeded

Solution code:
import sys,gc

    
A=[]
n=int(raw_input())
while n:
    s = (raw_input().split())
    if s[0] == 'insert':
        A.insert(0,s[1])
    elif s[0] == 'delete':
        try:
            A.remove(s[1])
        except:
            pass
    elif s[0] == 'deleteFirst':
        A.pop(0)
    elif s[0] == 'deleteLast':
        A.pop()
    n -=1
    
for e in A:
    print int(e),

Submission status: Time Limit Exceeded

Solution code:
import sys,gc

    
A=[]
n=int(raw_input())
while n:
    s = raw_input()
    if s[0] == 'i':
        A.insert(0,s[7:])
    elif s[6] == ' ':
        try:
            A.remove(s[7:])
        except:
            pass
    elif s[6] == 'F':
        A.pop(0)
    elif s[6] == 'L':
        A.pop()
    n -=1
    
for e in A:
    print int(e),

Submission status: Time Limit Exceeded

Solution code:
import sys,gc
    
A=[]
fp =0
bp =0

n=int(raw_input())
while n:
    s = raw_input()
    if s[0] == 'i':
        A.append(s[7:])
        fp += 1
    elif s[6] == ' ':
        i=-1
        try:
            while True:
                i = A[i+1:].index(s[7:])
        except:
            if i!=-1:
                del A[i]
                fp -=1
    elif s[6] == 'F':
        A.pop()
        fp -=1
    elif s[6] == 'L':
        bp +=1
    n -=1

for e in A[bp:fp+1][::-1]:
    if e!=-1:
        print int(e),

Submission status: Time Limit Exceeded

Solution code:
import sys,gc

    
A=[]
fp =0
bp =0

n=int(raw_input())
while n:
    s = raw_input()
    if s[0] == 'i':
        A.append(s[7:])
        fp += 1
    elif s[6] == ' ':
        i=-1
        try:
            while True:
                i = A[i+1:].index(s[7:])
        except:
            if i!=-1:
                del A[i]
                fp -=1
    elif s[6] == 'F':
        A.pop()
        fp -=1
    elif s[6] == 'L':
        A[bp]=-1
        bp +=1
    n -=1

for e in A[bp:fp+1][::-1]:
    if e!=-1:
        print int(e),

Submission status: Time Limit Exceeded

Solution code:
n = int(raw_input())

cmddict = {
	"insert":"%s.insert(0,%d)",
	"delete":"%s.remove(%d)",
	"deleteFirst":"%s.pop(0)",
	"deleteLast":"%s.pop()",
}

mylist = []
for i in range(n):
	cmd = raw_input().split()
	try:
		cmd[1] = int(cmd[1])
		try: exec(cmddict[cmd[0]] % ("mylist", cmd[1]))
		except: pass
		
	except IndexError:
		exec(cmddict[cmd[0]] % ("mylist"))
		
for i in mylist:
	print i,

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin
n = int(stdin.readline())
dll = []
while n:
    n -= 1
    cmd = stdin.readline().rstrip()
    if cmd.startswith('insert '):
        dll.insert(0, cmd[7:])
    elif cmd.startswith('delete '):
        key = cmd[7:]
        if dll.count(key):
            dll.remove(key)
    elif cmd.startswith('deleteFirst'):
        del dll[0]
    elif cmd.startswith('deleteLast'):
        del dll[-1]
print(*dll)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin
from array import array
n = int(stdin.readline())
dll = array('i')
while n:
    n -= 1
    cmd = stdin.readline()
    if cmd.startswith('insert '):
        dll.insert(0, int(cmd[7:]))
    elif cmd.startswith('deleteFirst'):
        del dll[0]
    elif cmd.startswith('deleteLast'):
        del dll[-1]
    elif cmd.startswith('delete '):
        key = int(cmd[7:])
        if dll.count(key):
            dll.remove(key)
print(*dll)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#from __future__ import print_function
import time
import sys
import io
import re
import math
start = time.clock()
i = 0
ans=[]
n=int(raw_input())
for _ in range(n):
    a=raw_input().split()
    if a[0]=='insert': ans.insert(0,a[1])
    elif a[0]=='deleteFirst': del ans[0]
    elif a[0]=='deleteLast': del ans[-1]
#    else: ans.remove(str(a[1]))
    else:
        if a[1] in ans:
            ans.remove(a[1])
#    print ans
print ' '.join(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#from __future__ import print_function
import time
import sys
import io
import re
import math
start = time.clock()
i = 0
ans=[]
n=int(raw_input())
for _ in range(n):
    a=sys.stdin.readline().split()
    if a[0]=='insert': ans.insert(0,a[1])
    elif a[0]=='deleteFirst': ans.pop(0)
    elif a[0]=='deleteLast': ans.pop()
#    else: ans.remove(str(a[1]))
    else:
        if a[1] in ans:
            ans.remove(a[1])
#    print ans
print ' '.join(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#from __future__ import print_function
import time
import sys
import io
import re
import math
start = time.clock()
i = 0
ans=[]
n=int(raw_input())
for _ in range(n):
    a=sys.stdin.readline().split()
    chk=a[0]
    if chk=='insert': ans.insert(0,a[1])
    elif chk=='deleteFirst': ans.pop(0)
    elif chk=='deleteLast': ans.pop()
#    else: ans.remove(str(a[1]))
    else:
        if a[1] in ans:
            ans.remove(a[1])
#    print ans
print ' '.join(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#from __future__ import print_function
import time
import sys
import io
import re
import math
start = time.clock()
i = 0
ans=[]
#n=int(raw_input())
#for _ in range(n):
for _ in range(int(raw_input())):
    a=sys.stdin.readline().split()
    chk=a[0]
    if chk=='insert': ans.insert(0,a[1])
#    elif chk=='deleteFirst': ans.pop(0)
    elif chk=='deleteFirst': del ans[0]
#    elif chk=='deleteLast': ans.pop()
    elif chk=='deleteLast': del ans[-1]
#    else: ans.remove(str(a[1]))
    else:
#        if a[1] in ans:
        if ans.count(a[1])>0:
            ans.remove(a[1])
#    print ans
print ' '.join(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#from __future__ import print_function
import time
import sys
import io
import re
import math
start = time.clock()
i = 0
ans=[]
#n=int(raw_input())
#for _ in range(n):
for _ in range(int(raw_input())):
#    a=sys.stdin.readline().split()
    a=sys.stdin.readline().strip()
#    chk=a[0]
    chk=a[6]
    if a[0]=='i': ans.insert(0,a[7:])
#    elif chk=='deleteFirst': ans.pop(0)
    elif chk=='F': del ans[0]
#    elif chk=='deleteLast': ans.pop()
    elif chk=='L': del ans[-1]
#    else: ans.remove(str(a[1]))
    else:
#        if a[1] in ans:
        if ans.count(a[7:])>0:
            ans.remove(a[7:])
#print ans
#    print ans
print ' '.join(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#from __future__ import print_function
import time
import sys
import io
import re
import math
start = time.clock()
i = 0
ans=[]
#n=int(raw_input())
#for _ in range(n):
for _ in range(int(raw_input())):
#    a=sys.stdin.readline().split()
    a=sys.stdin.readline().strip()
#    chk=a[0]
    chk=a[6]
    if a[0]=='i': ans.insert(0,a[7:])
#    elif chk=='deleteFirst': ans.pop(0)
    elif chk=='F': del ans[0]
#    elif chk=='deleteLast': ans.pop()
    elif chk=='L': del ans[-1]
#    else: ans.remove(str(a[1]))
    else:
#        if a[1] in ans:
        if ans.count(a[7:])>0:
            ans.remove(a[7:])
for j in ans:
    print int(j),

Submission status: Time Limit Exceeded

Solution code:
n = int(raw_input())
ls = []
for i in range(n):
    cmd = map(str, raw_input().split())
    if cmd[0] == "insert":
        ls.insert(0, cmd[1])
    elif cmd[0] == "delete":
        if ls.count(cmd[1]) > 0:
            ls.remove(cmd[1])
    elif cmd[0] == "deleteFirst":
        del ls[:1]
    elif cmd[0] == "deleteLast":
        del ls[len(ls)-1:]
print " ".join(map(str, ls))

Submission status: Time Limit Exceeded

Solution code:
n = int(raw_input())
ls = []
l = 0
for i in range(n):
    cmd = map(str, raw_input().split())
    if cmd[0] == "insert":
        ls.insert(0, cmd[1])
        l += 1
    elif cmd[0] == "deleteFirst":
        del ls[:1]
        l -= 1
    elif cmd[0] == "deleteLast":
        del ls[l-1:]
        l -= 1
    elif cmd[0] == "delete":
        if ls.count(cmd[1]) > 0:
            ls.remove(cmd[1])
            l -= 1
print " ".join(map(str, ls))

Submission status: Time Limit Exceeded

Solution code:
n = int(raw_input())
ls = []
for i in range(n):
    cmd = raw_input()
    if cmd[0] == "i":
        ls.append(int(cmd[6:]))
    elif cmd[6] == "F":
        del ls[len(ls)-1:]
    elif cmd[6] == "L":
        del ls[:1]
    else:
        try:
            p = ls[::-1].index(int(cmd[7:]))
            if p != -1:
                del ls[-p-1]
        except:
            pass
print " ".join(map(str, ls[::-1]))

Submission status: Time Limit Exceeded

Solution code:
n = int(raw_input())
ls = []
for i in range(n):
	cmd = raw_input()
	if cmd[0] == "i":
		ls.insert(0,cmd[7:])
	elif cmd[6] == " ":
		try:
			ls.remove(cmd[7:])
		except:
			pass
	elif cmd[6] == "F":
		ls.pop(0)
	else:
		ls.pop(-1)

print " ".join(map(str, ls))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

n = int(input())

q = deque()
for i in range(n):
    cmd = input().strip()
    if cmd == 'deleteFirst':
        q.popleft()
        continue
    if cmd == 'deleteLast':
        q.pop()
        continue
    cmd, x = cmd.split()
    if cmd == 'insert':
        q.appendleft(x)
        continue
    if cmd == 'delete':
        try:
            q.remove(x)
        except:
            pass
        continue

print(' '.join(q))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

def deletefirst():
    q.popleft()

def deletelast():
    q.pop()

def insert():
    q.appendleft(cmd[1])

def delete():
    try:
        q.remove(cmd[1])
    except:
        pass

run = {
    'insert': insert,
    'delete': delete,
    'deleteFirst': deletefirst,
    'deleteLast': deletelast,
}
n = int(input())

q = deque()
for i in range(n):
    cmd = input().strip().split()
    run[cmd[0]]()

print(' '.join(q))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

n = int(input())

q = deque()
for i in range(n):
    cmd = input().strip().split()
    try:
        if cmd[0][0] == 'i':
            q.appendleft(cmd[1])
        elif cmd[0][-1] == 'e':
            q.remove(cmd[1])
        elif cmd[0][6] == 'F':
            q.popleft()
        else:
            q.pop()
    except:
        pass

print(' '.join(q))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

n = int(input())

q = deque()
for i in range(n):
    cmd = input().strip().split()
    if cmd[0] == 'insert':
        q.appendleft(cmd[1])
        continue
    if cmd[0] == 'delete':
        try:
            q.remove(cmd[1])
        except:
            pass
        continue
    if cmd[0] == 'deleteFirst':
        q.popleft()
        continue
    if cmd[0] == 'deleteLast':
        q.pop()
        continue

print(' '.join(q))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

n = int(input())

q = deque()
for i in range(n):
    cmd = input().strip()
    if cmd == 'deleteFirst':
        q.popleft()
        continue
    if cmd == 'deleteLast':
        q.pop()
        continue
    cmd, x = cmd.split()
    x = int(x)
    if cmd == 'insert':
        q.appendleft(x)
        continue
    if cmd == 'delete':
        try:
            q.remove(x)
        except:
            pass
        continue

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
q = []
bottom = 0
for i in range(n):
    cmd = input()
    if cmd[0] == 'i':
        q.append(cmd[7:])
    elif cmd[6] == ' ':
        try:
            q.pop(~q[::-1].index(cmd[7:]))
        except:
            pass
    elif cmd[6] == 'F':
        q.pop()
    else:
        bottom += 1

print(' '.join(q[bottom:][::-1]))

Submission status: Memory Limit Exceeded

Solution code:

class Node(object):

    def __init__(self, _pre, _next, value):
        self._pre = _pre
        self._next = _next
        self._value = value

    @property
    def pre(self):
        return self._pre

    @pre.setter
    def pre(self, _pre):
        self._pre = _pre

    @property
    def next(self):
        return self._next

    @next.setter
    def next(self, _next):
        self._next = _next

    @property
    def value(self):
        return self._value

    @value.setter
    def value(self, _value):
        self._value = _value


class DoublyLinkedList(object):

    def __init__(self):
        self.dummy = Node(None, None, None)
        self.dummy.pre = self.dummy
        self.dummy.next = self.dummy

    def first(self):
        '''return first node of list'''
        return self.dummy.next

    def last(self):
        '''return last node of list'''
        return self.dummy.pre

    def end(self):
        return self.dummy

    def insert_after(self, value, node):
        '''考え方として、Valueをうけとって、クラス内でノードを自己で作成する。'''
        new_node = Node(node.pre, node, value)
        node.pre.next = new_node
        node.pre = new_node

    def insert(self, value):
        self.insert_after(value, self.first())

    def search(self, key):
        n = self.first()
        while n != self.end() and n.value != key:
            n = n.next
        return n

    def print_all(self):
        n = self.first()
        result = []
        while n != self.end():
            result.append(str(n.value))
            n = n.next
        return " ".join(result)


    def delete(self, key, n=None):
        '''ポインターの結合を外すことで削除とする。'''
        if n is None: n = self.search(key)
        n.pre.next = n.next
        n.next.pre = n.pre

    def deleteFirst(self):
        self.delete(None, self.first())

    def deleteLast(self):
        self.delete(None, self.last())


_list = DoublyLinkedList()

n = input()

for _ in range(int(n)):
    com, *num = input().split()
    if num:
        eval("_list.{}({})".format(com, *num))
    else:
        if com == "deleteFirst":
            _list.deleteFirst()
        else:
            _list.deleteLast()


print(_list.print_all())