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: Accepted

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

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

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

    def search(self, key):
        cur = self.first.next
        while cur != self.first and cur.key != key:
            cur = cur.next

        return cur

    def delete(self, key):
        self.delete_node(self.search(key))

    def delete_node(self, node):
        if node == self.first:
            return
        else:
            node.prev.next = node.next
            node.next.prev = node.prev

    def delete_first(self):
        self.delete_node(self.first.next)

    def delete_last(self):
        self.delete_node(self.first.prev)


if __name__ == "__main__":
    import sys
    input = sys.stdin.readline # faster input

    linklist = DoublyLinkedList()

    n = int(input())

    for _ in range(n):
        inputs = input().rstrip()
        if inputs[0] == 'i':
            linklist.insert(inputs[7:])
        elif inputs[6] == 'F':
            linklist.delete_first()
        elif inputs[6] == 'L':
            linklist.delete_last()
        else:
            linklist.delete(inputs[7:])

    result = []
    cur = linklist.first.next
    while cur != linklist.first:
        result.append(cur.key)
        cur = cur.next

    print(' '.join(result))

Submission status: Accepted

Solution code:
from collections import deque

n = int(input())
Q= deque()

for _ in range(n):
    str = input()
    if str == "deleteFirst":
        Q.popleft()
    elif str == "deleteLast":
        Q.pop()
    else:
        cmd,key = str.split()
        if cmd == "insert":
            Q.appendleft(key)
        elif cmd == "delete":
            try:
                Q.remove(key)
            except:
                pass
print(" ".join(list(Q)))

Submission status: Accepted

Solution code:
from collections import deque

n = int(input())

que = deque()

for i in range(n):
    order = input()

    if order == "deleteFirst":
        que.popleft()
    elif order == "deleteLast":
        que.pop()

    else:
        order,value = order.split()
        value = int(value)
        if order == "insert":
            que.appendleft(value)
        elif order == "delete":
            try:
                que.remove(value)
            except:
                pass

print(*que)

Submission status: Accepted

Solution code:
import sys
import array
from collections import deque


def main():
    n = int(input())
    #l = array.array('L')
    l = deque()
    for _ in range(n):
        c = sys.stdin.readline().split()
        if c[0] == "insert":
            l.appendleft(int(c[1]))
        elif c[0] == "delete":
            if int(c[1]) in l:
                l.remove(int(c[1]))
        elif c[0] == "deleteFirst":
            l.popleft()
        elif c[0] == "deleteLast":
            l.pop()
    print(*l)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import deque

dll = deque()

n = int(input())
for _ in range(n):
    command = input()
    if command[0] == "i":
        dll.appendleft(command[7:])
    elif command[0] == "d":
        if command[6] == ' ':
            key = command[7:]
            if key in dll:
                dll.remove(key)
        elif command[6] == "F":
            dll.popleft()
        else:
            dll.pop()

print(' '.join(dll))

Submission status: Accepted

Solution code:
from collections import deque

def main():
    dq = deque()
    n = int(input())
    for _ in range(n):
        com = input().split()
        if com[0]=='insert':dq.appendleft(com[1])
        elif com[0]=='delete':
            try:dq.remove(com[1])
            except:pass
        elif com[0]=='deleteFirst':dq.popleft()
        else:dq.pop()
    print(' '.join(dq))



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque
import sys

c=sys.stdin

def main():
    q = deque()
    N=int(input())

    for i in range(N):
        order=c.readline()
        if order[6]==" ":
            if order[0]=="i":
                q.appendleft(int(order[7:]))
            else:
                try :
                    q.remove(int(order[7:]))
                except:
                    pass
        else:
            if order[6]=="F":
                q.popleft()
            else:
                q.pop()
    print(*list(q))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import deque
import sys

input = sys.stdin.readline

n = int(input())
Qs = deque()

for i in range(n):
    
    OPs = input().split()
    
    op = OPs[0]
    
    if op[0] == 'i':
        Qs.appendleft(OPs[1])
        continue
    if op == 'delete':
        try:
            Qs.remove(OPs[1])
        except ValueError:
            pass
        continue
    if op == 'deleteFirst':
        Qs.popleft()
        continue
    if op == 'deleteLast':
        Qs.pop()
        continue

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

Submission status: Accepted

Solution code:
from collections import deque

n = int(input())
a = deque()

for _ in range(n):
    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()
    else:
        a.pop()
print(*a)

Submission status: Accepted

Solution code:
from collections import deque

n = int(input())
A = deque()

for i in range(n):
  order = input().split()
  if order[0] == "insert":
    A.appendleft(order[1])
  elif order[0] == "delete":
    try:
      A.remove(order[1])
    except ValueError:
      pass
  elif order[0] == "deleteFirst":
    A.popleft()
  elif order[0] == "deleteLast":
    A.pop()

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

Submission status: Accepted

Solution code:
from collections import deque

lst = deque()

for l in range(int(input())):
  dat_set = input().split()
  command = dat_set[0]
  try:
    if command == 'insert':
      lst.appendleft(dat_set[-1])
    elif command == 'delete':
      lst.remove(dat_set[-1])
    elif command == 'deleteFirst':
      lst.popleft()
    elif command == 'deleteLast':
      lst.pop()
  except:
    pass

print(*lst)

Submission status: Accepted

Solution code:
from collections import deque
n = int(input())
q = deque()
for i in range(n):
    cmd = input().split()
    if cmd[0] == "insert":
        q.appendleft(cmd[1])
    elif cmd[0] == "delete":
        try:
            q.remove(cmd[1])
        except ValueError:
            pass
    elif cmd[0] == "deleteFirst":
        q.popleft()
    elif cmd[0] == "deleteLast":
        q.pop()

print(" ".join(q))

Submission status: Accepted

Solution code:
from collections import deque

n = int(input())
Q = deque()
for _ in range(n):
    query = input()
    if query=='deleteFirst':
        Q.popleft()
    elif query=='deleteLast':
        Q.pop()
    else:
        q, x = query.split()
        if q=='insert':
            Q.appendleft(x)
        else:
            try:
                Q.remove(x)
            except ValueError:
                pass

print(' '.join(Q))

Submission status: Accepted

Solution code:
from collections import deque

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

Submission status: Accepted

Solution code:
from collections import deque

lst = deque()

for l in range(int(input())):
  dat_set = input().split()
  command = dat_set[0]
  try:
    if command == 'insert':
      lst.appendleft(dat_set[-1])
    elif command == 'delete':
      lst.remove(dat_set[-1])
    elif command == 'deleteFirst':
      lst.popleft()
    elif command == 'deleteLast':
      lst.pop()
  except:
    pass

print(*lst)

Submission status: Accepted

Solution code:
class Node():
    def __init__(self,num,next=None,prev=None):
        self.num=num
        self.next=next
        self.prev=prev
class Doubly():
    def __init__(self):
        self.start=self.end=None
    def insert(self,x):
        it=Node(x)
        if self.end is None:
            self.end=self.start=it
        else:
            it.next=self.start
            self.start.prev=it
            self.start=it
    def delete(self,x):
        poin=self.start
        while(poin is not None):
            if poin.num==x:
                if poin.prev is None and poin.next is None:
                    self.start = self.end = None
                elif poin is self.start:#先頭
                    self.start.next.prev=None
                    self.start=poin.next
                elif poin is self.end:#末尾
                    poin.prev.next =None
                    self.end=poin.prev
                else:
                    poin.prev.next=poin.next
                    poin.next.prev=poin.prev
                break
            poin=poin.next
    # def deleteFirst(self):
    #     self.start=self.start.next
    # def deleteLast(self):
    #     self.end.prev.next =None
    #     self.end=self.end.prev
    def deleteFirst(self):
        if self.start is self.end:
            self.start = self.end = None
        else:
            self.start.next.prev = None
            self.start = self.start.next
    def deleteLast(self):
        if self.start is self.end:
            self.start = self.end = None
        else:
            self.end.prev.next = None
            self.end = self.end.prev
    def print(self):
        var=self.start
        lis=[]
        while var is not None:
            lis.append(var.num)
            var = var.next
        return ' '.join(lis)
from sys import stdin
N=int(input())
DB=Doubly()
# code=[input().split() for k in range(N)]
# for _ in range(N):
#     c=input().split()
#     if len(c)==1:
#         eval('DB.'+c[0]+'()')
#     else:
#         eval('DB.'+c[0]+'(c[1])')
for _ in range(N):
        cmd = stdin.readline().strip().split()
        if cmd[0] == 'insert':
            DB.insert(cmd[1])
        elif cmd[0] == 'delete':
            DB.delete(cmd[1])
        elif cmd[0] == 'deleteFirst':
            DB.deleteFirst()
        elif cmd[0] == 'deleteLast':
            DB.deleteLast()
print(DB.print())

Submission status: Accepted

Solution code:
from collections import deque

n = int(input())
cmdlst = [input() for cmd in range(n)]
lst = deque()

for cmd in cmdlst:
    if cmd == 'deleteFirst':
        lst.popleft()
    elif cmd == 'deleteLast':
        lst.pop()
    else:
        cmd,x = cmd.split()
        if cmd == 'insert':
            lst.appendleft(x)
        else:
            try:
                lst.remove(x)
            except:
                pass
print(*lst)

Submission status: Accepted

Solution code:
from collections import deque

n = int(input())
data = deque()
for i in range(n):
    command = input().split()
    if command[0] == "insert":
        data.appendleft(command[1])
    elif command[0] == "delete":
        try:
            data.remove(command[1])
        except:
            pass
    elif command[0] == "deleteFirst":
        data.popleft()
    elif command[0] == "deleteLast":
        data.pop()
print(*data)

Submission status: Accepted

Solution code:
from collections import deque

# 双方向連結リストの実装
N = int(input())
commands = [input() for _ in range(N)]

d = deque()

for c in commands:
    if c == "deleteFirst":
        if d:
            d.popleft()

    elif c == 'deleteLast':
        if d:
            d.pop()

    elif 'delete ' in c:
        _, key = c.split()
        key = int(key)
        if key in d:
            d.remove(key)

    elif 'insert ' in c:
        _, key = c.split()
        key = int(key)
        d.appendleft(key)

print(*d)

Submission status: Accepted

Solution code:
from collections import deque

LIST = deque()

num_command = int(input())

for loop in range(num_command):
    input_command = input()
    if input_command == "deleteFirst":
        LIST.popleft()
    elif input_command == "deleteLast":
        LIST.pop()
    else:
        com,num = input_command.split()
        if com == "insert":
            LIST.appendleft(num)
        elif com == "delete":
            try:
                LIST.remove(num)
            except:
                pass
print(" ".join(LIST))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque

n = int(input())

commnad = ''
key = 0
dll = deque()
cur = 0

for i in range(n):
    tmp = input()
    if ' ' in tmp:
        command, key = tmp.split(' ')
    else:
        command = tmp

    if command == 'insert':
        dll.appendleft(key)
    elif command == 'delete':
        try:
            dll.remove(key)
        except:
            pass
    elif command == 'deleteFirst':
        dll.popleft()
    elif command == 'deleteLast':
        dll.pop()

res = ''
for i in dll:
    res += i
    res += ' '
print(res[:-1])

Submission status: Accepted

Solution code:
import sys
n = int(sys.stdin.readline())
lines = sys.stdin.readlines()
#n = int(input())
#import collections
#dll = collections.deque()
class Node:
  def __init__(self,v):
    self.val = v
    self.prev = None
    self.next = None
class DLL():
  def __init__(self):
    self.tail = Node(None)
    self.tail.next = self.tail
    self.tail.prev = self.tail
  def insert(self,x):
    n = Node(x)
    pre = self.tail.next
    n.next = pre
    n.prev = self.tail
    pre.prev = n
    self.tail.next = n
  def delete(self,x):
    node = self.tail.next
    while node != self.tail:
      if node.val == x:
        p = node.prev
        n = node.next
        p.next = n
        n.prev = p
        return
      node = node.next
  def deleteFirst(self):
    pre = self.tail.next
    if pre == self.tail:
      return
    node = self.tail.next
    p = node.prev
    n = node.next
    p.next = n
    n.prev = p
  def deleteLast(self):
    if self.tail.next == self.tail:
      return
    node = self.tail.prev
    p = node.prev
    n = node.next
    p.next = n
    n.prev = p
  def inspect(self):
    node = self.tail.next
    ret = []
    while node != self.tail:
      ret.append(str(node.val))
      node = node.next
    print(" ".join(ret))
dll=DLL()
for i in range(n):
  q = lines[i]
  if q[0] == "i":
      dll.insert(int(q.split()[1]))
  elif q[6] == " ":
      dll.delete(int(q.split()[1]))
  elif q[6] == "L":
      dll.deleteLast()
  elif q[6] == "F":
      dll.deleteFirst()
dll.inspect()
#print(" ".join(map(str,dll)))

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

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

    num_list = deque([])
    for i in range(N):
        command = input()
        if "insert" in command:
            key = command.split()[1]
            num_list.appendleft(key)
        else:
            if "First" in command:
                num_list.popleft()
            elif "Last" in command:
                num_list.pop()
            else:
                key = command.split()[1]
                if (key in num_list):
                    num_list.remove(key)

    print(" ".join(list(num_list)))

Submission status: Accepted

Solution code:
import sys

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

class LinkedList: 
    def __init__(self): 
        self.top = Node(None) 
        self.top.next = self.top
        self.top.prev = self.top
    def insert(self, x):
        a = Node(x)
        self.top.next.prev = a
        a.next = self.top.next
        self.top.next = a
        a.prev = self.top
    def search(self, x):
        nil = self.top
        node = nil.next
        while node != nil and node.data != x:
            node = node.next
        return node
    def _delete(self, node):
        if node != self.top:
            np = node.prev
            nn = node.next
            np.next = nn
            nn.prev = np
    def delete_data(self, x):
        node = self.search(x)
        self._delete(node)
    def deleteFirst(self):
        nil = self.top
        self._delete(nil.next)
    def deleteLast(self):
        nil = self.top
        self._delete(nil.prev)
    def output(self):
        nil = self.top
        node = nil.next
        datalist = []
        while node != nil:
            datalist.append(node.data)
            node = node.next
        print(" ".join(datalist))

n = int(input()) 
L = LinkedList()
nil = L.top
for cmd in sys.stdin:
    cmd = cmd.strip("\n")
    if "insert" in cmd:
        L.insert(cmd[7:])
    elif "deleteFirst" in cmd:
        L.deleteFirst()
    elif "deleteLast" in cmd: 
        L.deleteLast()
    elif "delete " in cmd:
        L.delete_data(cmd[7:])

L.output()

Submission status: Accepted

Solution code:
from collections import deque

n = int(input())
dq = deque()
for _ in range(n):
    query = input()
    if query == "deleteFirst":
        dq.popleft()
    elif query == "deleteLast":
        dq.pop()
    else:
        op, arg = query.split()
        if op == "insert":
            dq.appendleft(arg)
        else:
            tmp = deque()
            while dq:
                item = dq.popleft()
                if item == arg:
                    break
                else:
                    tmp.append(item)
            while tmp:
                dq.appendleft(tmp.pop())
print(*dq)

Submission status: Accepted

Solution code:
from collections import deque
from enum import Enum
import sys
import math

BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001


LIST = deque()

num_command = int(input())

for loop in range(num_command):

    input_command = input()

    if input_command == 'deleteFirst':
        LIST.popleft()
    elif input_command == 'deleteLast':
        LIST.pop();
    else:
        com,num = input_command.split()

        if com == 'insert':
            LIST.appendleft(num)
        elif com == 'delete':
            try:
                LIST.remove(num)
            except:
                pass

print(' '.join(LIST))

Submission status: Accepted

Solution code:
from collections import deque
import sys
input = sys.stdin.readline

n = int(input())
lst = deque([])
for i in range(n):
    inp = input().split()
    op = inp[0]
    try:
        x = inp[1]
    except:
        pass
    if op == 'insert':
        lst.appendleft(x)
    if op == 'delete':
        try:
            lst.remove(x)
        except:
            pass
    if op == 'deleteFirst':
        lst.popleft()
    if op == 'deleteLast':
        lst.pop()
print(*lst)

Submission status: Accepted

Solution code:
from collections import deque

d=deque()

N=int(input())

for n in range(N):
    a=input()
    if a=="deleteFirst":
        d.popleft()
    elif a=="deleteLast":
        d.pop()
    else:
        a,b=a.split()
        if a=="insert":
            d.appendleft(b)
        elif a=="delete":
            if d.count(b):
                d.remove(b)
                
print(' '.join(list(d)))

Submission status: Accepted

Solution code:
from collections import deque

L = deque()

n = int(input())

for i in range(n):
    input_com = input()

    if input_com == "deleteFirst":
        L.popleft()
    elif input_com == "deleteLast":
        L.pop()
    else:
        com, num = input_com.split()

        if com == "insert":
            L.appendleft(num)
        elif com == "delete":
            try:
                L.remove(num)
            except:
                pass

print(*L)

Submission status: Accepted

Solution code:
#alds13c
from collections import deque
class DoubleLinkyList:
    def __init__(self):
        self.lst=[]
    def insert(self,x):
        self.lst.insert(0,x)
    def delete_x(self,x):
        if x in self.lst:
            self.lst.pop(self.lst.index(x))
    def deleteFirst(self):
        self.lst.pop(0)
    def deleteLast(self):
        self.lst.pop(-1)
        
n = int(input())
dll = deque()

for i in range(n):
    A = input().split(" ")
    if A[0] == "insert":
        dll.appendleft((A[1]))
        #continue
    if A[0] == "delete" and A[1] in dll:
        dll.remove((A[1]))
    if A[0]=="deleteFirst":
        dll.popleft()
    if A[0]=="deleteLast":
        dll.pop()

result = list(dll)
print(" ".join(result))
#print(dll)

Submission status: Accepted

Solution code:
class Element:
    def __init__(self, x):
        self.x = x
        self.lh = None
        self.rh = None

class MyDeque:
    def __init__(self):
        self.top = self.tail = None
    
    def insert(self, x):
        elm = Element(x)
        if self.top is None:
            self.top = self.tail = elm
        else:
            elm.rh = self.top
            self.top.lh = self.top = elm
        
    def delete(self, x):
        elm = self.top
        while elm is not None:
            if elm.x == x:
                if elm.lh is None and elm.rh is None:
                    self.top = self.tail = None
                else:    
                    if elm.lh is None:
                        self.top = elm.rh
                    else:
                        elm.lh.rh = elm.rh
                        
                    if elm.rh is None:
                        self.tail = elm.lh
                    else:
                        elm.rh.lh = elm.lh
                break
            
            elm = elm.rh
            
    def delete_first(self):
        if self.top is self.tail:
            self.top = self.tail = None
        else:
            self.top = self.top.rh
            self.top.lh = None
                
    def delete_last(self):
        if self.top is self.tail:
            self.top = self.tail = None
        else:
            self.tail = self.tail.lh
            self.tail.rh = None
                
    
    def print_que(self):
        elm = self.top
        q=[]
        while elm is not None:
            q.append(elm.x)
            elm = elm.rh
        print(*q)

from sys import stdin

que = MyDeque()
N = int(input())
for _ in range(N):
    cmd = stdin.readline().strip().split()
    if cmd[0] == 'insert':
        que.insert(cmd[1])
        #que.print_que()        
        
    elif cmd[0] == 'delete':
        que.delete(cmd[1])
        #que.print_que()
        
    elif cmd[0] == 'deleteFirst':
        que.delete_first()
        #que.print_que()
        
    elif cmd[0] == 'deleteLast':
        que.delete_last()
        #que.print_que()
    
que.print_que()

Submission status: Accepted

Solution code:
import sys

class Node:
    """
    key: Value of the node
    next: Pointer to the next node
    prev: Pointer to the previous node
    """

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


class DoublyLinkedList:
    def __init__(self):
        """
        next always point to the very first node in a list
        prev always point to the last node in a list
        """
        self.next = None
        self.prev = None

    def insert(self, key):
        """
        :param key: key to insert into a list (inserted to the first place in the list)
        :return: None
        """
        newNode = Node(key)
        # First node ever added to the list
        if self.next is None:
            newNode.next = None
            newNode.prev = self
            self.next = newNode
            self.prev = newNode
        # Second nodes and forth
        else:
            newNode.next = self.next
            newNode.prev = self
            self.next.prev = newNode
            self.next = newNode

    def listSearch(self, key):
        """
        :param key: key of a note to search for
        :return: Node (the first Node from the beginning of a list)
        """
        cur = self.next
        while cur is not None and cur.key != key:
            cur = cur.next
        return cur

    def deleteNode(self, node):
        """
        :param node: Node to delete
        :return: None
        """
        if node is None:
            return
        # If the node is the last node
        elif node.next is None:
            node.prev.next = None
            self.prev = node.prev
        # If the node is not the last node
        else:
            node.prev.next = node.next
            node.next.prev = node.prev

    def delete(self, key):
        """
        Delete the first node that has the key.
        :param key: key that the node to be deleted has
        :return: None
        """
        self.deleteNode(self.listSearch(key))

    def deleteFirst(self):
        """
        Delete the first node in a list
        :return: None
        """
        cur = self.next
        self.deleteNode(cur)

    def deleteLast(self):
        """
        Delete the last node in a list
        :return: None
        """
        cur = self.prev
        self.deleteNode(cur)

    def showKeys(self):
        """
        Print keys of nodes in a list divided by a space
        :return: None
        """
        cur = self.next
        keys = []
        while cur is not None:
            keys.append(cur.key)
            cur = cur.next
        keys = map(str, keys)
        print(' '.join(keys))

n = int(input())
d = DoublyLinkedList()

# input() seems to be too slow for this (result in TLE for large inputs)
for i in sys.stdin:
    if 'insert' in i:
        x = i[7:-1]
        d.insert(x)
    elif 'deleteFirst' in i:
        d.deleteFirst()
    elif 'deleteLast' in i:
        d.deleteLast()
    elif 'delete' in i:
        x = i[7:-1]
        d.delete(x)
    else:
        pass

d.showKeys()

Submission status: Accepted

Solution code:
from collections import deque
import sys
input = sys.stdin.readline
n = int(input())
d = deque()
for _ in range(n):
    l = input().strip()
    if "deleteFirst" == l:d.popleft();continue
    elif "deleteLast" == l: d.pop();continue
    c, s = l.split()
    if c == "insert": d.appendleft(s);continue
    elif s in d: d.remove(s)
print(*d)

Submission status: Accepted

Solution code:
from collections import deque
from enum import Enum
import sys
import math

BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001


LIST = deque()

num_command = int(input())

for loop in range(num_command):

    input_command = input()

    if input_command == 'deleteFirst':
        LIST.popleft()
    elif input_command == 'deleteLast':
        LIST.pop();
    else:
        com,num = input_command.split()

        if com == 'insert':
            LIST.appendleft(num)
        elif com == 'delete':
            try:
                LIST.remove(num)
            except:
                pass

print(' '.join(LIST))

Submission status: Accepted

Solution code:
from collections import deque 

n = int(input())

data = deque()

for i in range(n):
    command = input().split()
    
    if command[0] == "insert":
        data.appendleft(command[1])
    elif command[0] == "delete":
        # if command[1] in data:
        try:
            data.remove(command[1])
        except ValueError:
            pass
    elif command[0] == "deleteFirst":
        data.popleft()

    elif command[0] == "deleteLast":
        data.pop()

print(" ".join(data))