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: Memory Limit Exceeded

Solution code:
#http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_3_C&lang=jp
#???????????£????????????
#??¬?????????????????¬????????§????????¨???????????????????????????dummy????????????????????¨??§???????????????????????????????????§??????
def dll_processor(operations):
    
    front = None
    
    for o in operations:
        opcode = o[0]
        if opcode == "insert":
            front = insert(front, { "next":None, "prev":None, "data":o[1] })
        elif opcode == "delete":
            front = delete(front, o[1])
        elif opcode == "deleteFirst":
            front = delete_first(front)
        elif opcode == "deleteLast":
            front = delete_last(front)
        #print(get_list(front))
        
    return get_list(front)

def get_list(front):
    if not front:
        return []
    
    l = []
    target = front
    while True:
        l.append(target["data"])
        if not target["next"]:
            break
        target = target["next"]
    return l

def insert(front, target):

    if front:
        front["prev"] = target
        target["next"] = front

    return target
    
def delete(front, target):

    delete_node = front
    while not delete_node["data"] == target:
        delete_node = delete_node["next"]
        if delete_node == None:
            return front

    if delete_node["prev"] == None:
        delete_node["next"]["prev"] = None
        front = delete_node["next"]
        del delete_node
        return front
    elif delete_node["next"] == None:
        delete_node["prev"]["next"] = None
        del delete_node
        return front
    else:
        delete_node["next"]["prev"] = delete_node["prev"]
        delete_node["prev"]["next"] = delete_node["next"]
        del delete_node
        return front
    
def delete_last(front):
    last = front
    while last["next"]:
        last = last["next"]

    if last == front:
        del front
        return None
    else:
        last["prev"]["next"] = None
        del last
        return front
    
def delete_first(front):
    if not front["next"]:
        del front
        return None
    
    front["next"]["prev"] = None
    new_front = front["next"]
    del front
    return new_front

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

Submission status: Memory Limit Exceeded

Solution code:
#http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_3_C&lang=jp
#???????????£????????????
#??¬?????????????????¬????????§????????¨???????????????????????????dummy????????????????????¨??§???????????????????????????????????§??????
import gc
def dll_processor(operations):
    
    front = None
    end = None
    
    for o in operations:
        if o[0] == "insert":
            front, end = insert(front, end,{ "next":None, "prev":None, "data":o[1] })
        elif o[0] == "delete":
            front, end = delete(front, end, o[1])
        elif o[0] == "deleteFirst":
            front, end = delete_first(front, end)
        elif o[0] == "deleteLast":
            front, end = delete_last(front, end)
        #print(get_list(front))
        
    return get_list(front)

def get_list(front):
    if not front:
        return []
    
    l = []
    target = front
    while True:
        l.append(target["data"])
        if not target["next"]:
            break
        target = target["next"]
    return l

def insert(front, end, target):

    if front:
        front["prev"] = target
        target["next"] = front
        return target, end
    else:
        return target, target
    
def delete(front, end,target):

    delete_node = front
    while not delete_node["data"] == target:
        delete_node = delete_node["next"]
        if delete_node == None:
            return front, end

    if delete_node["prev"] == None:
        delete_node["next"]["prev"] = None
        return delete_node["next"], end
    elif delete_node["next"] == None:
        delete_node["prev"]["next"] = None
        return front, delete_node["prev"]
    else:
        delete_node["next"]["prev"] = delete_node["prev"]
        delete_node["prev"]["next"] = delete_node["next"]
        return front, end
    
def delete_last(front, end):
    
    if not end["prev"]:
        return None, None
    else:
        end["prev"]["next"] = None
        return front, end["prev"]
    
def delete_first(front, end):
    
    if not front["next"]:
        return None, None
    else:
        front["next"]["prev"] = None
        return front["next"], end

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

Submission status: Memory Limit Exceeded

Solution code:
#http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_3_C&lang=jp
#???????????£????????????
#??¬?????????????????¬????????§????????¨???????????????????????????dummy????????????????????¨??§???????????????????????????????????§??????
def dll_processor(operations):
    
    front = None
    end = None
    
    for o in operations:
        if o[0] == "insert":
            front, end = insert(front, end,{ "next":None, "prev":None, "data":o[1] })
        elif o[0] == "delete":
            front, end = delete(front, end, o[1])
        elif o[0] == "deleteFirst":
            front, end = delete_first(front, end)
        elif o[0] == "deleteLast":
            front, end = delete_last(front, end)
        #print(get_list(front))
        
    return get_list(front)

def get_list(front):
    if not front:
        return []
    
    l = []
    target = front
    while True:
        l.append(target["data"])
        if not target["next"]:
            break
        target = target["next"]
    return l

def insert(front, end, target):

    if front:
        front["prev"] = target
        target["next"] = front
        return target, end
    else:
        return target, target
    
def delete(front, end,target):

    delete_node = front
    while not delete_node["data"] == target:
        delete_node = delete_node["next"]
        if delete_node == None:
            return front, end

    if delete_node["prev"] == None:
        delete_node["next"]["prev"] = None
        tmp = delete_node["next"]
        del delete_node
        return tmp, end
    elif delete_node["next"] == None:
        delete_node["prev"]["next"] = None
        tmp = delete_node["prev"]
        del delete_node
        return front, tmp
    else:
        delete_node["next"]["prev"] = delete_node["prev"]
        delete_node["prev"]["next"] = delete_node["next"]
        del delete_node
        return front, end
    
def delete_last(front, end):
    
    if not end["prev"]:
        del front, end
        return None, None
    else:
        end["prev"]["next"] = None
        tmp = end["prev"]
        del end
        return front, tmp
    
def delete_first(front, end):
    
    if not front["next"]:
        del front, end
        return None, None
    else:
        front["next"]["prev"] = None
        tmp = front["next"]
        del front
        return tmp, end

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

Submission status: Memory Limit Exceeded

Solution code:
#http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_3_C&lang=jp
#???????????£????????????
#??¬?????????????????¬????????§????????¨???????????????????????????dummy????????????????????¨??§???????????????????????????????????§??????
def dll_processor(operations):
    
    front = None
    end = None
    
    for o in operations:
        if o[0] == "insert":
            front, end = insert(front, end,{ "next":None, "prev":None, "data":int(o[1]) })
        elif o[0] == "delete":
            front, end = delete(front, end, int(o[1]))
        elif o[0] == "deleteFirst":
            front, end = delete_first(front, end)
        elif o[0] == "deleteLast":
            front, end = delete_last(front, end)
        #print(get_list(front))
        
    return get_list(front)

def get_list(front):
    if not front:
        return []
    
    l = []
    target = front
    while True:
        l.append(target["data"])
        if not target["next"]:
            break
        target = target["next"]
    return l

def insert(front, end, target):

    if front:
        front["prev"] = target
        target["next"] = front
        return target, end
    else:
        return target, target
    
def delete(front, end,target):

    delete_node = front
    while not delete_node["data"] == target:
        delete_node = delete_node["next"]
        if delete_node == None:
            return front, end

    if delete_node["prev"] == None:
        delete_node["next"]["prev"] = None
        return delete_node["next"], end
    elif delete_node["next"] == None:
        delete_node["prev"]["next"] = None
        return front, delete_node["prev"]
    else:
        delete_node["next"]["prev"] = delete_node["prev"]
        delete_node["prev"]["next"] = delete_node["next"]
        return front, end
    
def delete_last(front, end):
    
    if not end["prev"]:
        return None, None
    else:
        end["prev"]["next"] = None
        return front, end["prev"]
    
def delete_first(front, end):
    
    if not front["next"]:
        return None, None
    else:
        front["next"]["prev"] = None
        return front["next"], end

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

Submission status: Memory Limit Exceeded

Solution code:
#http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_3_C&lang=jp
#???????????£????????????
#??¬?????????????????¬????????§????????¨???????????????????????????dummy????????????????????¨??§???????????????????????????????????§??????
NEXT = 2
DATA = 1
PREV = 0
def dll_processor(operations):
    
    front = None
    end = None
    
    for o in operations:
        if o[0] == "insert":
            front, end = insert(front, end, [None, int(o[1]), None])
        elif o[0] == "delete":
            front, end = delete(front, end, int(o[1]))
        elif o[0] == "deleteFirst":
            front, end = delete_first(front, end)
        elif o[0] == "deleteLast":
            front, end = delete_last(front, end)
        #print(get_list(front))
        
    return get_list(front)

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

def insert(front, end, target):

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

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

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

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

Submission status: Memory Limit Exceeded

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


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


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

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

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

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

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

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

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

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


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

print(L.getKeys())

Submission status: Memory Limit Exceeded

Solution code:
import sys

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(s[1])
    elif s[0] == 'delete':
        A.delete(s[1])
    elif s[0] == 'deleteFirst':
        A.pop_front()
    elif s[0] == 'deleteLast':
        A.pop_back()
    n -=1
    
print A

Submission status: Memory Limit Exceeded

Solution code:
n = int(raw_input())
cmdlist = [raw_input().split() for i in range(n)]

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

mylist = []
for cmd in cmdlist:
	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: Accepted

Solution code:
from collections import deque

if __name__ == '__main__':
    n = int(input())
    dll = deque()
    for _ in range(n):
        command = input().split()
        if command[0] == 'insert':
            dll.appendleft(command[1])
        elif command[0] == 'delete':
            try:
                dll.remove(command[1])
            except ValueError:
                pass
        elif command[0] == 'deleteFirst':
            dll.popleft()
        elif command[0] == 'deleteLast':
            dll.pop()
    print(*dll)

Submission status: Accepted

Solution code:
from collections import deque

def main():
    lst = deque()

    n = int(input())

    for _ in range(n):
        cmd = input().split()

        if 'insert' == cmd[0]:
            lst.appendleft(cmd[1])
        if 'delete' == cmd[0]:
            try:
                lst.remove(cmd[1])
            except:
                pass
        if 'deleteFirst' == cmd[0]:
            lst.popleft()
        if 'deleteLast' == cmd[0]:
            lst.pop()

    sep = ''
    for n in lst:
        print(sep+n, end='')
        sep = ' '
    print()

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque

# 命令数入力
n = int(input())
# 命令入力
# order_list = [input().split() for i in range(n)]


# 双方向連結リスト
doubly_linked_list = deque()

for i in range(n):
    
    order = input().split()
    
    if order[0] == "insert":
        doubly_linked_list.appendleft(order[1])
    
    elif order[0] == "delete":
        if order[1] in doubly_linked_list:
            doubly_linked_list.remove(order[1])
    
    elif order[0] =="deleteFirst":
        doubly_linked_list.popleft()
    
    elif order[0] == "deleteLast":
        doubly_linked_list.pop()
        
# 最終結果出力
print(*doubly_linked_list)

Submission status: Accepted

Solution code:
from collections import deque

# 命令数入力
n = int(input())
# 命令入力
# order_list = [input().split() for i in range(n)]


# 双方向連結リスト
doubly_linked_list = deque()

for _ in range(n):
    
    order = input().split()
    
    if order[0] == "insert":
        doubly_linked_list.appendleft(order[1])
    
    elif order[0] == "delete":
        if order[1] in doubly_linked_list:
            doubly_linked_list.remove(order[1])
    
    elif order[0] =="deleteFirst":
        doubly_linked_list.popleft()
    
    elif order[0] == "deleteLast":
        doubly_linked_list.pop()
        
# 最終結果出力
print(*doubly_linked_list)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque
from sys import stdin
q = deque()
input()
for s in stdin:
    c, *k = s.split()
    k = k[0] if k else None
    if c[0] == 'i':
        q.appendleft(k)
    elif c[0] == 'd':
        if c[6:7] == 'F':
            q.popleft()
        elif c[6:7] == 'L':
            q.pop()
        else:
            if k in q:
                q.remove(k)
print(*q)

Submission status: Accepted

Solution code:
from collections import deque
from sys import stdin
q = deque()
input()
for s in stdin:
    if s == '\n':
        break
    c, *k = s.split()
    k = k[0] if k else None
    if c == 'insert':
        q.appendleft(k)
    elif c == 'delete':
        if k in q:
            q.remove(k)
    elif c == 'deleteFirst':
        q.popleft()
    else:
        q.pop()
print(*q)

Submission status: Accepted

Solution code:
import sys
from collections import deque


def main():
    line = sys.stdin.readlines()
    deq = deque()
    for i in range(1, int(line[0]) + 1):
        input_command = line[i]

        if input_command[0] == 'i':         # 'insert index'
            deq.appendleft(input_command[7:-1])
        elif input_command[0] == 'd':
            if input_command[6] == ' ':     # 'delete index'
                key = input_command[7:-1]
                if key in deq:
                    deq.remove(key)
            elif input_command[6] == 'F':   # 'deleteFirst':
                deq.popleft()
            elif input_command[6] == 'L':   # 'deleteLast':
                deq.pop()

    print(' '.join(deq))
    return


main()

Submission status: Accepted

Solution code:
from collections import deque
dq = deque()
for _ in [None]*int(input()):
    s = input()
    if s == "deleteFirst":
        dq.popleft()
    elif s == "deleteLast":
        dq.pop()
    else:
        a, b = s.split()
        if a == "insert":
            dq.appendleft(b)
        else:
            if b in dq:
                dq.remove(b)
print(" ".join(dq))

Submission status: Accepted

Solution code:
class Node(object):
    def __init__(self, num, prv = None, nxt = None):
        self.num = num
        self.prv = prv
        self.nxt = nxt
  
  
class DoublyLinkedList(object):
    def __init__(self):
        self.start = self.last = None
  
    def insert(self, num):
        new_elem = Node(num)
  
        if self.start is None:
            self.start = self.last = new_elem
        else:
            new_elem.nxt = self.start
            self.start.prv = new_elem
            self.start = new_elem
  
    def delete_num(self, target):
        it = self.start
        while it is not None:
            if it.num == target:
                if it.prv is None and it.nxt is None:
                    self.start = self.last = None
                else:
                    if it.prv is not None:
                        it.prv.nxt = it.nxt
                    else:
                        self.start = self.start.nxt
  
                    if it.nxt is not None:
                        it.nxt.prv = it.prv
                    else:
                        self.last = self.last.prv
                break
            it = it.nxt
  
    def delete_start(self):
        if self.start is self.last:
            self.start = self.last = None
        else:
            self.start.nxt.prv = None
            self.start = self.start.nxt
  
    def delete_last(self):
        if self.start is self.last:
            self.start = self.last = None
        else:
            self.last.prv.nxt = None
            self.last = self.last.prv
  
    def get_content(self):
        ret = []
        it = self.start
  
        while it is not None:
            ret.append(it.num)
            it = it.nxt
  
        return ' '.join(ret)
  
  
def _main():
    from sys import stdin
  
    n = int(input())
  
    lst = DoublyLinkedList()
      
    for _ in range(n):
        cmd = stdin.readline().strip().split()
        if cmd[0] == 'insert':
            lst.insert(cmd[1])
        elif cmd[0] == 'delete':
            lst.delete_num(cmd[1])
        elif cmd[0] == 'deleteFirst':
            lst.delete_start()
        elif cmd[0] == 'deleteLast':
            lst.delete_last()
  
    print(lst.get_content())
  
  
if __name__ == '__main__':
    _main()

Submission status: Accepted

Solution code:
from collections import deque
n = int(input())
data = deque(list(input() for _ in range(n)))

ans = deque([])
co = 0

for i in data:
    if i[6] == ' ':
        if i[0] == 'i':
            ans.insert(0, i[7:])
            co += 1
        else:
            try:
                ans.remove(i[7:])
                co -= 1
            except:
                pass
    else:
        if i[6] == 'F':
            del ans[0]
            co -= 1
        else:
            del ans[-1]
            co -= 1

for i in range(0, co-1):
    print(ans[i], end=' ')
print(ans[co-1])

Submission status: Accepted

Solution code:
from collections import deque

n = int(input())
d = deque()
for _i in range(n):
    line = input().split()
    order = line[0]
    if order in ('insert', 'delete'):
        key = line[1]
    if order == 'insert':
        d.appendleft(key)
    elif order == 'delete':
        try:
            d.remove(key)
        except ValueError:
            pass
    elif order == 'deleteFirst':
        d.popleft()
    elif order == 'deleteLast':
        d.pop()
    else:
        raise ValueError('Invalid order: {order}')

print(' '.join(d))

Submission status: Accepted

Solution code:
from collections import deque

n = int(input())
d = deque()
for _i in range(n):
    line = input().split()
    order = line[0]
    if order == 'deleteFirst':
        d.popleft()
    elif order == 'deleteLast':
        d.pop()
    else:
        key = line[1]
        if order == 'insert':
            d.appendleft(key)
        elif order == 'delete':
            try:
                d.remove(key)
            except ValueError:
                pass
        else:
            raise ValueError('Invalid order: {order}')

print(' '.join(d))

Submission status: Accepted

Solution code:
from collections import deque

n = int(input())
d = deque()
for _i in range(n):
    order = input()
    if order == 'deleteFirst':
        d.popleft()
    elif order == 'deleteLast':
        d.pop()
    else:
        order, key = order.split()
        if order == 'insert':
            d.appendleft(key)
        elif order == 'delete':
            try:
                d.remove(key)
            except ValueError:
                pass
        else:
            raise ValueError('Invalid order: {order}')

print(' '.join(d))

Submission status: Accepted

Solution code:
import sys
from collections import deque

input()
d = deque()
for l in sys.stdin:
	s = l.split()
	if s[0] == 'insert':
		d.appendleft(s[1])
	elif s[0] == 'delete':
		try: d.remove(s[1])
		except: pass
	elif s[0] == 'deleteFirst':
		d.popleft()
	elif s[0] == 'deleteLast':
		d.pop()
print(*d)

Submission status: Accepted

Solution code:
import sys
from collections import deque

input()
d = deque()
for l in sys.stdin:
	s = l.split()
	if s[0] == 'insert':
		d.appendleft(s[1])
	elif s[0] == 'deleteFirst':
		d.popleft()
	elif s[0] == 'deleteLast':
		d.pop()
	elif s[0] == 'delete':
		try: d.remove(s[1])
		except: pass
print(*d)

Submission status: Accepted

Solution code:
from collections import deque

n = int(input())
C = [input() for i in range(n)]

A = deque([])
for c in C:
	if c == 'deleteFirst':
		A.popleft()
	elif c == 'deleteLast':
		A.pop()
	elif 'delete' in c:
		q = int(c[7:])
		if q in A:
			A.remove(q)
	elif 'insert' in c:
		q = int(c[7:])
		A.appendleft(q)

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

Submission status: Accepted

Solution code:
from collections import deque
q = deque()

for _ in range(int(input())):
	b=input()
	if b[0] == 'i':
		q.appendleft(b[7:])
	elif b[6] == ' ':
		try:
			q.remove(b[7:])
		except:
			pass
	elif len(b) > 10:
		q.popleft()
	elif len(b) > 6:
		q.pop()
print(*q)

Submission status: Accepted

Solution code:
from collections import deque

n = int(input())

q = deque()

for _ in range(n):
	com = input()
	if com[0] == "i":
		q.appendleft(com[7:])
	elif len(com) == 11:
		q.popleft()
	elif len(com) == 10:
		q.pop()
	else:
		try:
			q.remove(com[7:])
		except:
			pass


print(*q)

Submission status: Accepted

Solution code:
from collections import deque
from sys import stdin

n = int(input())

q = deque()

for _ in range(n):
	cmd = stdin.readline().strip().split()
	if cmd[0] == "insert":
		q.appendleft(cmd[1])
	elif cmd[0] == "deleteFirst":
		q.popleft()
	elif cmd[0] == "deleteLast":
		q.pop()
	elif cmd[0] == "delete":
		try:
			q.remove(cmd[1])
		except:
			pass

print(*q)

Submission status: Accepted

Solution code:
from collections import deque

n = int(input())
commands = [input() for _ in range(n)] # splitとかしてると間に合わない

l = deque()
for com in commands:
    if com[0]=='i':
        l.appendleft(com[7:])
    elif com[6]=='F':
        l.popleft()
    elif com[6]=='L':
        l.pop()
    else:
        try:
            l.remove(com[7:])
        except:
            pass

s = ' '.join(l)
print(s)

Submission status: Accepted

Solution code:

import collections,sys
def s():
  d = collections.deque()
  input()
  for e in sys.stdin:
    if "i"==e[0]: d.appendleft(e[7:-1])
    else:
      if " "==e[6]:
        m = e[7:0-1]
        if m in d: d.remove(m)
      elif "i"==e[7]: d.popleft()
      else: d.pop()
  print(*d)

s()

Submission status: Accepted

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.popleft()
    elif cmd == 'deleteLast':
        dlist.pop()

print(' '.join(dlist))

Submission status: Accepted

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.appendleft(a[1])
    else:
        try:     l.remove(a[1])
        except:  pass

print " ".join(l)

Submission status: Accepted

Solution code:
n = input()
array = []
base = 0
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:
		base += 1
print (" ".join(map(str,array[base:][::-1])))

Submission status: Accepted

Solution code:
n = input()
array = []
base = 0
for i in xrange(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:
        base += 1
print (" ".join(map(str,array[base:][::-1])))