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: Wrong Answer

Solution code:
n = int(input())
dList = []

for _ in range(n):
  op = list(map(str,input().split()))
  if(op[0] == "insert"):
    dList.append(op[1])
  elif(op[0] == "delete"):
    for j,k in enumerate(dList):
      if(op[1] == k):
        dList.pop(j)
  elif(op[0] == "deleteFirst"):
    dList.pop(len(dList)-1)
  elif(op[0] == "deleteLast") :
    dList.pop(0)

print(*dList)

Submission status: Wrong Answer

Solution code:
n = int(input())
dList = []

for _ in range(n):
  op = list(map(str,input().split()))
  if(op[0] == "insert"):
    dList.append(op[1])
  elif(op[0] == "delete"):
    for j,k in enumerate(dList):
      if(op[1] == k):
        dList.pop(j)
  elif(op[0] == "deleteFirst"):
    dList.pop(len(dList)-1)
  elif(op[0] == "deleteLast") :
    dList.pop(0)

print(*dList[::-1])

Submission status: Wrong Answer

Solution code:
from collections import deque

n = int(input())

dlist = deque()

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.appendleft(key)
    elif cmd == 'delete':
        try:
            dlist.remove(key)
        except:
            continue
    elif cmd == 'deleteFirst':
        dlist.pop()
    elif cmd == 'deleteLast':
        dlist.popleft()

print(' '.join(dlist))

Submission status: Wrong Answer

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)))
    else: print(els)

main()

Submission status: Wrong Answer

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])

Submission status: Wrong Answer

Solution code:
from collections import deque
l=deque()
for _ in range(input()):
    a=raw_input().split()
    c=a[0][-4]
    if c=="i":   l.popleft()
    elif c=="L": l.pop()
    elif c=="s": l.extendleft(a[1])
    else:
        try:     l.remove(a[1])
        except:  pass

print " ".join(l)

Submission status: Wrong Answer

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:
			array.remove(inst[1])
	if inst[0] == "deleteFirst":
		array.pop()
	if inst[0] == "deleteLast":
		array.pop(0)
print " ".join(i for i in array)

Submission status: Wrong Answer

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:
			array.remove(inst[1])
	if inst[0] == "deleteFirst":
		array.pop()
	if inst[0] == "deleteLast":
		array.pop(0)
print " ".join(map(str,array[::-1]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

for x in l:
    print x,

Submission status: Wrong Answer

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 == 'd':
            try:
                que.remove(n)
            except:
                pass
print(' '.join(que))

Submission status: Wrong Answer

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 )
	elif "deleteFirst" == op[0] :
		del eles[0]
	elif "deleteLast" == op[0]:
		eles.pop( )

print( " ".join( eles ) )

Submission status: Wrong Answer

Solution code:
n = int( raw_input( ) )
eles = []
for i in range( n ):
	op = raw_input( ).split( " " )
	if "insert" == op[0]:
		eles[ 0: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: Wrong Answer

Solution code:
import sys

eles = []
for line in sys.stdin:
	op = line.split( " " )
	if "insert" == op[0]:
		eles.insert( 0, op[1] )
	elif "delete" == op[0]:
		if op[1].rstrip( ) in eles:
			eles.remove( op[1] )
	elif "deleteFirst" == op[0] :
		del eles[0]
	elif "deleteLast" == op[0]:
		eles.pop( )

print( " ".join( eles ) )

Submission status: Wrong Answer

Solution code:
import sys

eles = []
for line in sys.stdin:
	op = line.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: Wrong Answer

Solution code:
import sys

lines = sys.stdin.readlines()
n = int( lines[0] )
eles = []
for i in range( n ):
	op = lines[i].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: Wrong Answer

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( output )

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

n = int( sys.stdin.readline( ) )
que = deque( )
lines = sys.stdin.readlines()
for line in lines:
	op = line.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: Wrong Answer

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(0)
    else:
        for i,j in enumerate(ans):
            if j==s[1]:
                ans.pop(i)
                break
for i in ans:
    print i,

Submission status: Wrong Answer

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':
        R.remove(value)
    if order == 'deleteFirst':
        R.pop()
    if order == 'deleteLast':
        R.pop(len(R)-1)
print ' '.join(map(str, R))

Submission status: Wrong Answer

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':
		list.remove(int(inst[1]))
	elif inst[0]=='deleteFirst':
		del list[0]
	else :list.pop()
print list

Submission status: Wrong Answer

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':
		if inst[1] in list:
			list.remove(int(inst[1]))
	elif inst[0]=='deleteFirst':
		del list[0]
	else :list.pop()

for i in list:
	print i,

Submission status: Wrong Answer

Solution code:
N = int(raw_input())
A = []
B = []

def insert(B, x):
    B.insert(0, x)
    return B
def delete(B, x):
    i = 0
    for num in B:
        if num == x:
            B.pop(i)
            break
        i += 1
    return B
def deleteFirst(B):
    B.pop(0)
    return B
def deleteLast(B):
    B.pop()
    return B
operation = {'insert':insert, 'delete':delete, 'deleteFirst':deleteFirst, 'deleteLast':deleteLast}

for i in range(N):
    a = raw_input().split()
    if len(a) == 2:
        A.append([a[0], int(a[1])])
    else:
        A.append([a[0]])


for item in A:
    if len(item) == 2:
        command, num = item
        B = operation[command](B, num)
    else:
        command = item[0]
        B = operation[command](B)

print B

Submission status: Wrong Answer

Solution code:
class D_Linked_List:
    """Doubly Linked List class."""
    class Node:
        def __init__(self, key, next = None, prev = None):
            self.next = next
            self.prev = prev
            self.key = key

    def __init__(self):
        self.nil = D_Linked_List.Node(None)
        self.nil.next = self.nil
        self.nil.prev = self.nil

    def insert(self, key):
        node_x = D_Linked_List.Node(key, self.nil.next, self.nil)
        self.nil.next.prev = node_x
        self.nil.next = node_x

    def _listSearch(self, key):
        cur_node = self.nil.next
        while (cur_node != self.nil) and (cur_node.key != key):
            cur_node = cur_node.next
        return cur_node

    def _deleteNode(self, node):
        if node == self.nil:
            return None
        node.prev.next = node.next
        node.next.prev = node.prev

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

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

    def deleteKey(self, key):
        node = self._listSearch(key)
        self._deleteNode(node)

    def show_keys(self):    # ????????????node???key????????????????????§???????????????
        cur_node = self.nil.next
        keys = []
        while cur_node != self.nil:
            keys.append(cur_node.key)
            cur_node = cur_node.next
        print(' '.join(keys))


import sys

d_ll = D_Linked_List()

for i in sys.stdin:
    if i[0] == 'i':
        x = i[7:-1]
        d_ll.insert(x)
    elif len(i) > 6:
        if i[6] == 'F':
            d_ll.deleteFirst()
        else:
            d_ll.deleteLast()
    elif i[0] == 'd':
        x = i[7:-1]
        d_ll.deleteKey(x)
    else:   # ??\??????????????¨?????????command??§??????????????§???pass?????????
        pass

d_ll.show_keys()

Submission status: Wrong Answer

Solution code:
class D_Linked_List:
    """Doubly Linked List class."""
    class Node:
        def __init__(self, key, next = None, prev = None):
            self.next = next
            self.prev = prev
            self.key = key

    def __init__(self):
        self.nil = D_Linked_List.Node(None)
        self.nil.next = self.nil
        self.nil.prev = self.nil

    def insert(self, key):
        node_x = D_Linked_List.Node(key, self.nil.next, self.nil)
        self.nil.next.prev = node_x
        self.nil.next = node_x

    def _listSearch(self, key):
        cur_node = self.nil.next
        while (cur_node != self.nil) and (cur_node.key != key):
            cur_node = cur_node.next
        return cur_node

    def _deleteNode(self, node):
        if node == self.nil:
            return None
        node.prev.next = node.next
        node.next.prev = node.prev

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

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

    def deleteKey(self, key):
        node = self._listSearch(key)
        self._deleteNode(node)

    def show_keys(self):    # ????????????node???key????????????????????§???????????????
        cur_node = self.nil.next
        keys = []
        while cur_node != self.nil:
            keys.append(cur_node.key)
            cur_node = cur_node.next
        print(' '.join(keys))


import sys

d_ll = D_Linked_List()

for i in sys.stdin:
    if i[0] == 'i':
        x = i[7:-1]
        d_ll.insert(x)
    elif len(i) > 8:
        if i[6] == 'F':
            d_ll.deleteFirst()
        else:
            d_ll.deleteLast()
    elif i[0] == 'd':
        x = i[7:-1]
        d_ll.deleteKey(x)
    else:   # ??\??????????????¨?????????command??§??????????????§???pass?????????
        pass

d_ll.show_keys()

Submission status: Wrong Answer

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

n = input()
class DoubleLinkedList(object):
    def __init__(self):
        pass
        
    def insert(self, value):
        new = DoubleLinkedList()
        new.next = self.next
        new.prev = self
        self.next.prev = new
        self.next = new
        new.value = value
        
    def delete(self):
        next = self.next
        prev = self.prev
        self.next.prev = prev
        self.prev.next = next

head = DoubleLinkedList()
tail = DoubleLinkedList()
head.next = tail
tail.prev = head

for i in xrange(n):
    command = raw_input().split()
    if command[0] is "insert":
        head.insert(int(command[1]))
    elif command[0] is "delete":
        key = int(command[1])
        e = head.next
        while True:
            if e is tail:
                break
            if e.value is key:
                e.delete()
                break
            e = e.next
    elif command[0] is "deleteFirst":
        if not head.next is tail:
            head.next.delete()
    elif command[0] is "deleteLast":  
        if not tail.prev is head:
            tail.prev.delete()
    
e = head.next
while True:
    if e is tail:
        break
    print e.value,
    e = e.next

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
n = input()
l = []
tail = 0
for i in xrange(n):
    cmd = raw_input().split()
    if cmd[0] == "insert":
        l.append(cmd[1])
    elif cmd[0] == "delete":
        l.remove(cmd[1])
    elif cmd[0] == "deleteFirst":
        l.pop()
    elif cmd[0] == "deleteLast":
        tail += 1

print " ".join(l[tail:][::-1])

Submission status: Wrong Answer

Solution code:
class Node:
    def __init__(self,v):
        self.next = None
        self.prev = None
        self.data = v

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

    def __str__(self):
        ret = ''
        n = self.head
        while True:
            if n == None:
                break
            else:
                ret += str(n.data) + ' '
                n = n.next
        return ret[:-1]


    def insert(self,v):
        n = Node(v)
        if self.head == None:
            self.head = n
            self.tail = n
        else:
            n.next = self.head
            n.next.prev = n
            self.head = n

    def delete(self,v):
        n = self.head
        while True:
            if n == None:
                break
            else:
                if n.data == v:
                    if n.next == None:
                        self.tail = n.prev
                    else:
                        n.next.prev = n.prev
                    if n.prev == None:
                        self.head = n.next
                    else:
                        n.prev.next = n.next
                n = n.next

    def deleteFirst(self):
        if self.head != None:
            self.head = self.head.next
            self.head.prev = None
        else:
            raise


    def deleteLast(self):
        if self.tail != None:
            self.tail = self.tail.prev
            self.tail.next = None
        else:
            raise

n = int(raw_input())
commends = []
for i in range(n):
    commends.append(raw_input())

l = DoublyLinkedList()
for c in commends:
    if c == "deleteFirst":
        l.deleteFirst()
    elif c == "deleteLast":
        l.deleteLast()
    else:
        op,val = c.split(' ')
        val = int(val)
        if op == "insert":
            l.insert(val)
        elif op == "delete":
            l.delete(val)

print l

Submission status: Wrong Answer

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

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

for i in range(n):
    command = input().split()
    if command[0] == "insert":
        ans.insert(0, command[1])
    elif ans == []:
        continue
    elif command[0] == "delete":
        try:
            ans.remove(command[1])
        except:
            pass
    elif command[0] == "deleteFirst":
        ans.pop(0)
    else:
        ans.pop()
    print (ans)

print (' '.join(ans))

Submission status: Wrong Answer

Solution code:
from collections import deque

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

def delete(x, dll):
    dll.remove(x)

def deleteFirst(dll):
    dll.popleft()

def deleteLast(dll):
    dll.pop()

n = int(input())
command1 = {"insert": insert, "delete": delete}
command2 = {"deleteFirst": deleteFirst, "deleteLast": deleteLast}
dll = deque()
for i in range(n):
    input_line = [str(s) for s in input().split()]
    if(input_line[0] in command1):
        command1[input_line[0]](int(input_line[1]), dll)
    else:
        command2[input_line[0]](dll)
print(dll)

Submission status: Wrong Answer

Solution code:
class DoublyLinkedList:
    def __init__(self):
        self.first = Node(-1)
        self.first.setNext(Node(-1))

    def insert(self, value):
        newNode = Node(value)
        newNode.setNext(self.first.getNext())
        newNode.setPrev(self.first)
        self.first.getNext().setPrev(newNode)
        self.first.setNext(newNode)

    def delete(self, value):
        self.first.delete(value)

    def deleteFirst(self):
        if self.first.getNext().getValue() != -1:
            self.first.setNext(self.first.getNext().getNext())

    def deleteLast(self):
        self.first.deleteLast()

    def printAll(self):
        if self.first.getNext().getValue() != -1:
            self.first.getNext().printAll()

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

    def getValue(self):
        return self.value

    def setValue(self, value):
        self.value = value

    def getNext(self):
        return self.next

    def setNext(self, next):
        self.next = next

    def getPrev(self):
        return self.prev

    def setPrev(self, prev):
        self.prev = prev

    def delete(self, value):
        nextValue = self.next.getValue()
        if nextValue == value:
            self.next = self.next.getNext()
            self.next.setPrev(self)
        elif nextValue != -1:
            self.next.delete(value)

    def deleteLast(self):
        if self.next.value == -1:
            self.prev.setNext(self.next)
            self.next.setPrev(self.prev)
        else:
            self.next.deleteLast()

    def printAll(self):
        if self.next.getValue() != -1:
            print self.value,
            self.next.printAll()
        else:
            print self.value

if __name__ == "__main__":
    dll = DoublyLinkedList()

    N = input()
    for i in range(N):
        raw = raw_input().split()
        com = raw[0]
        if len(raw) > 1:
            value = int(raw[1])
        if com == "insert":
            dll.insert(value)
        elif com == "delete":
            dll.delete(value)
        elif com == "deleteFirst":
            dll.deleteFirst()
        else:
            dll.deleteLast()
    dll.printAll()

Submission status: Wrong Answer

Solution code:
A = []
n = input()

for i in range(n):
    command = [0, 0]
    temp = raw_input().split()
    if temp[0] == 'deleteFirst' or 'deleteLast':
        command[0] = temp[0]
    else:
        command[0] = temp[0]
        command[1] = int(temp[1])

    if command[0] == 'insert':
        A.insert(0, command[1])
    elif command[0] == 'delete':
        A.remove(command[1])
    elif command[0] == 'deleteFirst':
        A.pop(0)
    elif command[0] == 'deleteLast':
        A.pop()

for i in range(len(A)):
    print A[i],

Submission status: Wrong Answer

Solution code:
A = []
n = input()

for i in range(n):
    command = raw_input().split()

    print command

    if command[0] == 'insert':
        A.insert(0, command[1])
    elif command[0] == 'delete':
        if A.count(command[0]) != 0:
            A.remove(command[1])
    elif command[0] == 'deleteFirst':
        A.pop(0)
    elif command[0] == 'deleteLast':
        A.pop()

    print A

for i in range(len(A)):
    print A[i],

Submission status: Wrong Answer

Solution code:
A = []
n = input()

for i in range(n):
    command = raw_input().split()

    if command[0] == 'insert':
        A.insert(0, command[1])
    elif command[0] == 'delete':
        if A.count(command[0]) != 0:
            A.remove(command[1])
    elif command[0] == 'deleteFirst':
        A.pop(0)
    elif command[0] == 'deleteLast':
        A.pop()

for i in range(len(A)):
    print A[i],

Submission status: Wrong Answer

Solution code:
class DoublyLinkedList:
    def __init__(self, value = -1):
        self.value = value
        self.prev = self
        self.next = self

    def insert(self, value):
        newNode = DoublyLinkedList(value)
        newNode.next = self.next
        newNode.prev = self
        self.next.prev = newNode
        self.next = newNode

    def delete(self, value):
        node = self.next
        while node.value != -1:
            if node.value == value:
                node.next.prev = node.prev
                node.prev.next = node.next
            node = node.next

    def deleteFirst(self):
        if self.next.value != -1:
            self.next = self.next.next
            self.next.prev = self

    def deleteLast(self):
        if self.prev.value != -1:
            self.prev = self.prev.prev
            self.prev.next = self

    def printAll(self):
        node = self.next
        while node.next.value != -1:
            print node.value,
            node = node.next
        print node.value

if __name__ == "__main__":
    dll = DoublyLinkedList()

    N = input()
    for i in range(N):
        raw = raw_input().split()
        com = raw[0]
        if len(raw) > 1:
            value = int(raw[1])
        if com == "insert":
            dll.insert(value)
        elif com == "delete":
            dll.delete(value)
        elif com == "deleteFirst":
            dll.deleteFirst()
        else:
            dll.deleteLast()
    dll.printAll()

Submission status: Wrong Answer

Solution code:
def insert(o,dll):
    dll.insert(0,o)
def delete(o,dll):
    dll.remove(o)
def deleteFirst(dll):
    dll.pop(0)
def deleteLast(dll):
    dll.pop(-1)

def main():
    n = input()
    dll = []
    for _ in xrange(n):
        tmp = raw_input().split()
        if len(tmp) == 1:
            print dll
            print tmp[0]
            if tmp[0] == "deleteFirst":
                deleteFirst(dll)
            elif tmp[0] == "deleteLast":
                deleteLast(dll)
            else:
                print "This command is invalid."
                break
        elif len(tmp)==2:
            command = tmp[0]
            operand = tmp[1]
            print dll
            print command, operand
            if command == "insert":
                insert(operand,dll)
            elif command == "delete":
                delete(operand,dll)
            else:
                print "This command is invalid."
                break
    print " ".join(map(str,dll))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import time
def main():
    n = input()
    dll = []
    for _ in xrange(n):
        tmp = raw_input().split()
        if len(tmp) == 1:
            # print dll
            # print tmp[0]
            if tmp[0] == "deleteFirst":
                dll.pop(0)
            elif tmp[0] == "deleteLast":
                dll.pop(-1)
            else:
                print "This command is invalid."
                break
        elif len(tmp)==2:
            command = tmp[0]
            o = tmp[1]
            # print dll
            # print command, operand
            if command == "insert":
                dll.insert(0,o)
            elif command == "delete":
                if o in set(dll):
                    dll.remove(o)
            else:
                print "This command is invalid."
                break
    print " ".join(map(str,dll))

if __name__ == '__main__':
    f = time.clock()
    main()
    t = time.clock()
    print t-f

Submission status: Wrong Answer

Solution code:
#! -*-coding:utf-8-*-
# linked list????§???????????????????
class Deque:
    class Cell:
        def __init__(self,x,y=None,z=None):
            self.data = x
            self.next = y
            self.prev = z
    def __init__(self):
        head = Deque.Cell(None)
        head.next = head
        head.prev = head
        self.size = 0
        self.head = head
    def my_insert(self,x):
        h = self.head
        q = h.next
        p = Deque.Cell(x,q,h)
        h.next = p
        q.prev = p
        self.size +=1
    def my_del(self,x):
        if self.size==0:
            return
        h = self.head.next
        num=self.size
        while num > 0:
            if h.data == x:
                p = h.prev
                n = h.next
                h.prev.next = n
                h.next.prev = p
                self.size -=1
            h=h.next
            num -= 1
        # cp_h=h
        # while True:
        #     h = h.next
        #     if h==cp_h:
        #         break
        #     if h.data == x:
        #         p = h.prev
        #         n = h.next
        #         h.prev.next = n
        #         h.next.prev = p
        #         self.size -=1
    def my_deleteFirst(self):
        if self.size==0:
            return
        h = self.head.next
        p = h.prev
        n = h.next
        h.prev.next = n
        h.next.prev = p
        self.size-=1
    def my_deleteLast(self):
        if self.size==0:
            return
        h=self.head.prev
        p = h.prev
        n = h.next
        h.prev.next=n
        h.next.prev = p
        self.size-=1
    # ??¨???
    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

def main():
    q = Deque()
    num = input()
    i=0
    while True:
        if i>=num:
            break
        tmp = raw_input().split()
        if len(tmp)==1:
            if tmp[0]=="deleteFirst":
                q.my_deleteFirst()
            elif tmp[0]=="deleteLast":
                q.my_deleteLast()
            else:
                print "This command is invalid."
                return
        elif len(tmp)==2:
            if tmp[0]=="insert":
                q.my_insert(int(tmp[1]))
            elif tmp[0]=="delete":
                q.my_del(int(tmp[1]))
            else:
                print "This command is invalid."
                return
        else:
            print "This command is invalid."
            return
        i+=1
    print q

if __name__ == '__main__':
    main()