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:
import collections

def func(n, Command):
    L = collections.deque()
    for i in range(n):
        if "deleteFirst" in Command[i]:
            L.popleft()
        elif "deleteLast" in Command[i]:
            L.pop()
        else:
            if "insert" in Command[i]:
                L.appendleft(int(Command[i][7:]))
            else:
                if int(Command[i][7:]) in L:
                    L.remove(int(Command[i][7:]))
    return L

n = int(input())
Command = [input() for _ in range(n)]
print (*func(n, Command))

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    from collections import deque
    N = int(input())
    A = deque()
    for i in range(N):
        s = input().split()
        if s[0] == 'insert':
            A.appendleft(s[1])
        elif s[0] == 'delete':
            try:
                A.remove(s[1])
            except ValueError:
                pass
        elif s[0] == 'deleteFirst':
            A.popleft()
        elif s[0] == 'deleteLast':
            A.pop()
    print(*A)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import deque

n = int(input())
d = deque()

for _ in range(n):
    s = input()
    if "deleteFirst" in s:
        d.popleft()
    elif "deleteLast" in s:
        d.pop()
    else:
        command, k = s.split()
        if command == "insert":
            d.appendleft(k)
        elif command == "delete":
            if k not in d: continue
            d.remove(k)
       
print(" ".join(d))

Submission status: Accepted

Solution code:
from collections import deque

def main():
    n = int(input())
    commands = [input() for _ in range(n)]

    l = deque()
    for command in commands:
        op = command.split(' ')[0]
        
        if op == 'insert':
            x = command.split(' ')[1]
            l.appendleft(x)
        elif op == 'delete':
            x = command.split(' ')[1]
            if (x in l):
                l.remove(x)
        elif op == 'deleteFirst':
            l.popleft()
        elif op == 'deleteLast':
            l.pop()
        
    print(' '.join(l))
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import deque

if __name__ == "__main__":
	n = int(input())
	d = deque()
	for i in range(n):
		command = input()
		if command == 'deleteFirst':
			d.popleft()
		elif command == 'deleteLast':
			d.pop()
		else:
			cmd, key = command.split()
			if cmd == 'insert':
				d.appendleft(key)
			elif cmd == 'delete':
				try:
					d.remove(key)
				except:
					pass
	print(' '.join(d))

Submission status: Accepted

Solution code:
from collections import deque

n = int(input())
l = deque()

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

    if commands == "deleteFirst":
        l.popleft()

    elif commands == "deleteLast":
        l.pop()

    else:
        command, x = commands.split()

        if command == "insert":
            l.appendleft(x)
            

        else:
            try:
                l.remove(x)
            except:
                pass

print(' '.join(l))

Submission status: Accepted

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

Q = deque()

n = int(input())

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

print(" ".join(Q))

Submission status: Accepted

Solution code:
from collections import deque
n = int(input())
command, number = deque([]), deque([])

if __name__ == '__main__':
    A = deque([])
    for _ in range(n):
        tmp = input()
        if tmp == 'deleteFirst':
            A.popleft()
        elif tmp == 'deleteLast':
            A.pop()
        else:
            com, num = tmp.split()
            if com == 'insert': 
                A.appendleft(num)
            elif com == 'delete':
                try:
                    A.remove(num)
                except:
                    pass
    print(' '.join(A))

Submission status: Accepted

Solution code:
from collections import deque
queue=deque()
n=int(input())
for _ in range(n):
  command=input().split()
  if command[0]=="insert":
    queue.appendleft(command[1])
  if command[0]=="delete":
    try:
      queue.remove(command[1])
    except:
      pass
  if command[0]=="deleteFirst":
    queue.popleft()
  if command[0]=="deleteLast":
    queue.pop()
print(*queue)

Submission status: Accepted

Solution code:
from collections import deque

n = int(input())
result = deque()

for i in range(n):
  command = input()
  if command == "deleteLast":
    result.pop()
  elif command == "deleteFirst":
    result.popleft()
  else:
    com, value = command.split()
    value = int(value)
    if com == "insert":
      result.appendleft(value)
    elif com =="delete":
      try:
        result.remove(value)
      except:
        pass
      
print(*result, sep=" ")

Submission status: Accepted

Solution code:
from collections import deque


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

print(" ".join(num))

Submission status: Accepted

Solution code:
# ALDS1_3_C
import sys
class Cell():
    def __init__(self, x):
        self.key = x
        self.prev = None
        self.next = None


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

    def insert(self, x):
        new = Cell(x)
        tmp = self.head

        if tmp is None:
            new.next = None
            self.tail = new

        else:
            new.next = tmp
            tmp.prev = new

        self.head = new
        new.prev = None
        return

    def delete(self, x):

        if (self.head is None) & (self.tail is None):
            return

        tmp = self.head

        while tmp.key != x:
            tmp = tmp.next
            if tmp is None:  # 一番後ろまで要素　xが存在しなかったら
                return

        if self.head is self.tail:
            self.head = None
            self.tail = None
        elif tmp is self.head:
            tmp.next.prev = None
            self.head = tmp.next
        elif tmp is self.tail:
            tmp.prev.next = None
            self.tail = tmp.prev
        else:
            tmp.prev.next = tmp.next
            tmp.next.prev = tmp.prev
        return

    def deleteFirst(self):
        if self.head:
            self.head = self.head.next

        if self.head:
            self.head.prev = None
        else:
            self.tail = None
        return

    def deleteLast(self):
        if self.tail:
            self.tail = self.tail.prev

        if self.tail:
            self.tail.next = None
        else:
            self.head = None
        return

    def show(self):
        tmp = self.head

        while tmp:
            print(tmp.key, end='')
            if tmp.next:
                print(' ', end='')
            tmp = tmp.next
        else:
            print('')
        return

    def shown(self):
        tmp = self.head
        keys = []
        while tmp:
            keys.append(tmp.key)
            tmp = tmp.next
        keys = ' '.join(keys)
        print(keys)
        return

def main():
    l = DoubleLinkedList()

    N = int(sys.stdin.readline())

    for i in range(N):
        comand = sys.stdin.readline().strip()

        if comand == 'deleteFirst':
            l.deleteFirst()
        elif comand == 'deleteLast':
            l.deleteLast()
        else:
            comand, number = comand.split()
            if comand == 'insert':
                l.insert(number)
            else:
                l.delete(number)

    l.show()


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# coding: utf-8

from collections import deque

class DoublyLinkedList(object):

    def __init__(self):
        self.queue = deque()
    
    def __call__(self, orders):
        for order in orders:
            order = order.split()

            if order[0] == 'insert':
                self.insert(order[1])
            elif order[0] == 'delete':
                self.delete(order[1])
            elif order[0] == 'deleteFirst':
                self.delete_first()
            elif order[0] == 'deleteLast':
                self.delete_last()
        print(' '.join(self.queue))

    def insert(self, x):
        self.queue.appendleft(x)
    
    def delete(self, x):
        try:
            self.queue.remove(x)
        except:
            pass
    
    def delete_last(self):
        self.queue.pop()
    
    def delete_first(self):
        self.queue.popleft()
    

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

    n = int(input())
    orders = []
    for _ in range(n):
        orders.append(input())
    doubly_linked_list(orders)

Submission status: Accepted

Solution code:
from collections import deque
import sys
n=int(input())
N=[]
O=deque()
for i in range(n):
  N=input().split()
  if N[0]=='insert':
    O.appendleft(N[1])
  elif N[0]=='delete':
    try:
      O.remove(N[1])
    except ValueError:
      pass  # do nothing!
  elif N[0]=='deleteFirst':
    O.popleft()
  elif N[0]=='deleteLast':
    O.pop()
print(' '.join(map(str, O)))

Submission status: Accepted

Solution code:
import sys

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

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

    def insertzero(self,value):
        new  = Node(value)
        p = self.head
        self.head = new
        new.next = p
        if p is None:
            self.tail = new
        else:
            p.prev = new

    def deletefirst(self):
        if self.head is None:
            return
        p = self.head
        if p.next is None:
            self.head = None
            self.tail = None
        else:
            q = p.next
            q.prev = None
            self.head = q

    def deletelast(self):
        if self.tail is None:
            return
        p = self.tail
        q = p.prev
        if q is None:
            self.head =None
            self.tail =None
        else:
            q.next = None
            self.tail = q

    def delete(self,value):
        p = self.head
        while p is not None:
            if p.value == value:
                if p.prev is None:
                    self.deletefirst()
                    return
                elif p.next is None:
                    self.deletelast()
                    return
                else:
                    p.next.prev = p.prev
                    p.prev.next = p.next
                    return
            else:
                p = p.next

    def show(self):
        p = self.head
        #print("L:",end="")  
        l = []
        c = 0
        while p is not None:
            l.append(p.value)
            p=p.next
        print(" ".join(l))

    def rshow(self):
        p = self.tail
        c = 0
        while p is not None:
            if c > 0:
                print(" ",end="")
            else:
                c +=1
            print(p.value,end="")
            p = p.prev
        print()

def main():

    list  = LinkList()
    num = int(input())

    for _ in range(num):
        #a = input().split()                                                                                                                   
        line = sys.stdin.readline()
        a = line.rstrip().split()
        if a[0][0] == "i":
            list.insertzero(a[1])
        elif len(a[0]) == 6:
            list.delete(a[1])
        elif len(a[0]) == 11:
            list.deletefirst()
        elif len(a[0]) == 10:
            list.deletelast()
        else:
            pass

    list.show()

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
from collections import deque
n=int(input())
l=deque()
for i in range(n):
    c=list(input().split())
    if c[0]=="insert":
        c[1]=int(c[1])
        l.appendleft(c[1])
    elif c[0]=="delete":
        c[1]=int(c[1])
        try:
            l.remove(c[1])
        except:
            pass
    elif c[0]=="deleteFirst":
        a=l.popleft()
    else:
        a=l.pop()

print(*l)

Submission status: Accepted

Solution code:
from collections import deque
import sys

n = int(input())
li = deque()

def insertitem(item):
    li.appendleft(item)

def deleteitem(item):
    try:
        li.remove(item)
    except ValueError:
        pass

def deletefirst():
    li.popleft()

def deletelast():
    li.pop()

for i in sys.stdin:
    if i[6] == ' ':
        item = i[7:-1]
        if i[5] == 't':
            insertitem(item)
        else:
            deleteitem(item)
    elif i[6] == 'F':
        deletefirst()
    else:
        deletelast()

print(*li)

Submission status: Accepted

Solution code:
import sys
from collections import deque

def test():

	n = int(sys.stdin.readline())
	DLlist = deque()

	for i in range(n):
	
		command = sys.stdin.readline().split()

		if command[0] == 'insert':
			DLlist.appendleft(int(command[1]))

		elif command[0] == 'deleteFirst':
			DLlist.popleft()

		elif command[0] == 'deleteLast':
			DLlist.pop()

		elif command[0] == 'delete':
			x = int(command[1])
			for j in range(len(DLlist)):
				if DLlist[j] == x:
					DLlist.remove(x)
					break
				

	for i in range(len(DLlist)-1):
		print(f"{DLlist[i]} ",end='')
	print(f"{DLlist[len(DLlist)-1]}")
					

if __name__ == "__main__":
	test()

Submission status: Accepted

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


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

    def insert(self, key):
        node = Node(key)

        if self.head is None:
            self.head = self.tail = node
        else:
            node.next = self.head
            self.head.prev = node
            self.head = node

    def deleteKey(self, key):
        node = self.head

        while node:
            if node.key == key:
                if node.prev is None and node.next is None:
                    self.head = self.tail = None
                else:
                    # prevの処理
                    if node.prev is not None:  # 先頭でないなら
                        node.prev.next = node.next
                    else:  # 先頭なら
                        self.head = self.head.next

                    # nextの処理
                    if node.next is not None:  # 最後でないなら
                        node.next.prev = node.prev
                    else:  # 最後なら
                        self.tail = self.tail.prev
                break
            node = node.next

    def deleteFirst(self):
        if self.head is self.tail:
            self.head = self.tail = None
        else:
            self.head.next.prev = None
            self.head = self.head.next

    def deleteLast(self):
        if self.head is self.tail:
            self.head = self.tail = None
        else:
            self.tail.prev.next = None
            self.tail = self.tail.prev

    def createAnswerList(self):
        ret = []
        node = self.head

        while node:
            ret.append(node.key)
            node = node.next

        return " ".join(ret)


if __name__ == "__main__":
    from sys import stdin

    count = int(input())

    linked_list = DoublyLinkedList()

    for i in range(count):
        order = stdin.readline().strip().split()
        if order[0] == "insert":
            linked_list.insert(order[1])
        elif order[0] == "delete":
            linked_list.deleteKey(order[1])
        elif order[0] == "deleteFirst":
            linked_list.deleteFirst()
        elif order[0] == "deleteLast":
            linked_list.deleteLast()

    print(linked_list.createAnswerList())

Submission status: Accepted

Solution code:
import sys

class MyNode:
    __slots__ = ['prev', 'next', 'val']

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

class MyLinkList:
    __slots__ = ['head', 'tail']

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

    def generate(self):
        _node = self.head
        while True:
            if _node is None:
                break
            yield _node.val
            _node = _node.next

    def insert(self, x):
        node = MyNode(x)
        if self.head is None:
            self.head = self.tail = node
        else:
            self.head.prev = node
            node.next = self.head
            self.head = node

    def delete(self, x):
        if self.head.val == x:
            return self.deleteFirst()

        node = self.head.next
        while node.next is not None:
            if node.val == x:
                node.prev.next = node.next
                node.next.prev = node.prev
                # node.prev = None
                # node.next = None
                return True

            node = node.next

        if self.tail.val == x:
            return self.deleteLast()

        return False

    def deleteFirst(self):
        if self.head == self.tail:
            self.head = self.tail = None
            return True

        self.head = self.head.next
        # self.head.prev.next = None
        self.head.prev = None

        return True

    def deleteLast(self):
        if self.head == self.tail:
            self.head = self.tail = None
            return True

        self.tail = self.tail.prev
        # self.tail.next.prev = None
        self.tail.next = None

        return True

def _main():
    link_list = MyLinkList()

    n = int(input())
    
    for i in range(n):
        line = sys.stdin.readline()

        if line[0] == 'i':
            link_list.insert(line[7:-1])
        else:
            if line[6] == 'F':
                link_list.deleteFirst()
            elif line[6] == 'L':
                link_list.deleteLast()
            else:
                link_list.delete(line[7:-1])

    lst = [x for x in link_list.generate()]
    print(" ".join(lst))

if __name__ == '__main__':
    _main()

Submission status: Accepted

Solution code:
from collections import deque
n=int(input())
Deq=deque()
for i in range(n):
    command=input().split()
    if len(command)>1:
        x=command[1]
    if command[0]=="insert":
        Deq.appendleft(x)

    elif command[0]=="delete":
        try:
            Deq.remove(x)
        except:
            pass

    elif command[0]=="deleteFirst":
        Deq.popleft()


    elif command[0]=="deleteLast":
        Deq.pop()
print(*Deq)

Submission status: Accepted

Solution code:
from collections import deque
import sys
# S = sys.stdin.readline()
n=int(sys.stdin.readline())
dq = deque()

def command(cm):
	if cm[0]=='insert':
		dq.appendleft(cm[1])
	if cm[0]=='delete':
		try:dq.remove(cm[1])
		except ValueError:pass
	if cm[0]=='deleteFirst':
		dq.popleft()
	if cm[0]=='deleteLast':
		dq.pop()

for i in range(n):
	command(sys.stdin.readline().split())
print(' '.join(dq))

Submission status: Accepted

Solution code:
class Elm:
    def __init__(self, value, ptr_prev, ptr_next):
        self.value = value
        self.prev = ptr_prev
        self.next = ptr_next
#        self.prev = None
#        self.next = None

class DoublyLinkedList:
    def __init__(self):
        self.nil = Elm("NIL", None, None)
        self.nil.prev = self.nil # tail
        self.nil.next = self.nil # head
        
def insertVal(self, value):
    new_elm = Elm(value, self.nil, self.nil.next)
    new_elm.next.prev = new_elm
    self.nil.next = new_elm

def deleteVal(self, value):
    ptr = self.nil.next
    while ptr.value != value:
        if ptr.next == self.nil:
            return
        ptr = ptr.next
#    if ptr.value == value:
#        ptr.prev.next = ptr.next
#        ptr.next.prev = ptr.prev
    ptr.prev.next = ptr.next
    ptr.next.prev = ptr.prev

def deleteFirst(self):
    self.nil.next = self.nil.next.next
    self.nil.next.prev = self.nil

def deleteLast(self):
    self.nil.prev = self.nil.prev.prev
    self.nil.prev.next = self.nil

def show(self):
    ptr = self.nil.next
    while ptr!=self.nil:
#    while True:
        if ptr.next == self.nil:
            print(ptr.value)
            return
        print(ptr.value, end=" ")
        ptr = ptr.next
        
def selectOps(dll, command):
#    ops = command[0]
    if command[0] == "insert":
        insertVal(dll, command[1])
    elif command[0] == "delete":
        deleteVal(dll, command[1])        
    elif command[0] == "deleteFirst":
        deleteFirst(dll)
#    elif ops == "deleteLast":
    else:
        deleteLast(dll)
#    else:
#        print("Error: invalid oparation")

    
import sys

n = int(input())
l = DoublyLinkedList()

for i in range(n):
    command = sys.stdin.readline().split()
#    command = input().split(" ")
#    selectOps(dll=l, command=command)
    if command[0] == "insert":
        insertVal(l, command[1])
    elif command[0] == "delete":
        deleteVal(l, command[1])        
    elif command[0] == "deleteFirst":
        deleteFirst(l)
#    elif ops == "deleteLast":
    else:
        deleteLast(l)
show(l)

Submission status: Accepted

Solution code:
# doubly Linked listの設計
from sys import stdin


class DoublyLinkedList():

    class Cell():
        def __init__(self, value):
            self.value = value
            self.next = None
            self.previous = None

    def __init__(self):
        self.head = DoublyLinkedList.Cell(None)
        self.tail = DoublyLinkedList.Cell(None)
        self.head.next = self.tail
        self.tail.previous = self.head

    def insert(self, v):
        new_cell = DoublyLinkedList.Cell(v)
        new_cell.next = self.head.next
        new_cell.previous = self.head
        self.head.next.previous = new_cell
        self.head.next = new_cell

    def delete(self, v):
        tmp_cell = self.head.next
        while tmp_cell.value is not None:
            if tmp_cell.value == v:
                tmp_cell_next = tmp_cell.next
                tmp_cell_previous = tmp_cell.previous
                tmp_cell_previous.next = tmp_cell_next
                tmp_cell_next.previous = tmp_cell_previous
                break
            tmp_cell = tmp_cell.next

    def deleteFirst(self):
        self.head.next = self.head.next.next
        self.head.next.previous = self.head

    def deleteLast(self):
        self.tail.previous = self.tail.previous.previous
        self.tail.previous.next = self.tail


def print_res(dl):
    res = []
    cur = dl.head.next
    while cur.next is not None:
        res.append(cur.value)
        cur = cur.next
    print(" ".join(res))


def main():
    dl = DoublyLinkedList()
    line_num = int(stdin.readline())
    for i in range(line_num):
        command = stdin.readline().strip()
        if command == "deleteFirst":
            dl.deleteFirst()
        elif command == "deleteLast":
            dl.deleteLast()
        elif command.startswith("insert"):
            dl.insert(command.split(" ")[1].strip())
        else:
            dl.delete(command.split(" ")[1].strip())
    print_res(dl)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque

def OpeList():
    L = deque()
    num_command = int(input())

    for i in range(num_command):
        input_command = input()
        if input_command == 'deleteFirst':
            L.popleft()
        elif input_command == 'deleteLast':
            L.pop()
        else:
            com, num =  input_command.split()
            if com == 'insert':
                L.appendleft(num)
            elif com == 'delete':
                try:
                    #ここでLの中身を全部見るからってfor i in range(len(L))とすると
                    #C[i][1]が全部消える(∵C[i][1]を発見した時点で止まらないから)
                    L.remove(num)
                except:
                    pass
    print(' '.join(L))

OpeList()

Submission status: Accepted

Solution code:

import sys
from collections import deque


def input(): return sys.stdin.readline().strip()


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


def delete(x, l):
    if len(l) == 0:
        return
    index = l.index(x) if x in l else None
    if index is None:
        return
    else:
        l.remove(x)


def delete_first(l):
    if len(l) == 0:
        return
    l.popleft()


def delete_last(l):
    if len(l) == 0:
        return
    l.pop()


n = int(input())
l = deque()

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

    if command[0] == 'insert':
        insert(command[1], l)

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

    elif command[0] == 'deleteFirst':
        delete_first(l)

    elif command[0] == 'deleteLast':
        delete_last(l)

print(' '.join(l))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

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, 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):
    node = [None, target, None]
    if front:
        front[PREV] = node
        node[NEXT] = front
        return node, end
    else:
        return node, node

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

Solution code:
#double linked list
#using collection dequeue
from collections import deque

n = int(input())
que = deque()
command = ""
key = 0
for i in range(n):
    line = input()
    if " " in line:
        command, key = line.split()
    else:
        command = line

    if command == "insert":
        que.appendleft(key)
    elif command == "delete":
        try:
            que.remove(key)
        except:
            pass
    elif command == "deleteFirst":
        que.popleft()
    elif command == "deleteLast":
        que.pop()
print(" ".join(que))

Submission status: Accepted

Solution code:
from collections import deque

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

#   def keys(self):
#     cur = self.head.next
#     keys = []
#     while cur != None and cur.key != None:
#       keys.append(cur.key)
#       cur = cur.next
#     return keys

#   def search(self, key):
#     cur = self.head.next
#     while cur != None and cur.key != key and cur.key != None:
#       cur = cur.next
#     return cur

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

#   def deleteKey(self, key):
#     node = self.search(key)
#     if node != None and node.key != None:
#       self.deleteNode(node)

#   def deleteNode(self, node):
#     if node == None and node.key == None:
#       return
#     node.prev.next = node.next
#     node.next.prev = node.prev

#   def deleteFirst(self):
#     self.deleteNode(self.head.next)

#   def deleteLast(self):
#     self.deleteNode(self.head.prev)    

n = int(input())
commands = [input() for _ in range(n)]

q = deque()
for command in commands:
  op = command.split()
  if op[0] == "insert":
    q.appendleft(op[1])
  elif op[0] == "delete":
    try:
      q.remove(op[1])
    except ValueError as err:
      pass
  elif op[0] == "deleteFirst":
    q.popleft()
  elif op[0] == "deleteLast":
    q.pop()
print(" ".join(map(str, q)))

Submission status: Accepted

Solution code:
import sys
from collections import deque

queue = deque()
n = int(input())
for _ in range(n):
    op = sys.stdin.readline().strip().split()
    if op[0] == 'deleteFirst':
        queue.popleft()
    elif op[0] == 'deleteLast':
        queue.pop()
    elif op[0] == 'insert':
        queue.appendleft(int(op[1]))
    elif op[0] == 'delete':
        try:
            queue.remove(int(op[1]))
        except ValueError:
            pass

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