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  command 1   command 2  ...  command n   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 ≤ 10 9  The number of elements in the list does not exceed 10 6  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

Solution (Python):

from collections import deque
n = int(input())
deque = deque()
input_lines = [input().split() for x in range(n)]

for i in range(n):
    inp = input_lines[i]
    if inp[0] == 'deleteFirst':
        deque.popleft()
    elif inp[0] == 'deleteLast':
        deque.pop()
    elif inp[0] == 'insert':
        deque.appendleft(inp[1])
    else:
        if inp[1] in deque:
            deque.remove(inp[1])

print(*deque)

Solution (Python):

from collections import deque
n = int(input())
deque = deque()
input_lines = [input().split() for x in range(n)]

for i in range(n):
    inp = input_lines[i]
    if inp[0] == 'deleteFirst':
        deque.popleft()
    elif inp[0] == 'deleteLast':
        deque.pop()
    elif inp[0] == 'insert':
        deque.appendleft(inp[1])
    else:
        if inp[1] in deque:
            deque.remove(inp[1])

print(' '.join(deque))

Solution (Python):
from collections import deque
import sys
 
deque = deque()
num = int(sys.stdin.readline())
line = sys.stdin.readlines()
 
for i in range(num):
 
    order = line[i].split()
 
    if order[0] == 'insert':
        deque.appendleft(order[1])
    elif order[0] == 'deleteFirst':
        deque.popleft()
    elif order[0] == 'deleteLast':
        deque.pop()
    else:
        if order[1] in deque:
            deque.remove(order[1])
 
print(' '.join(deque))

Solution (Python):

from collections import deque
n = int(input())
deque = deque()
input_lines = [input().split() for x in range(n)]

for i in range(n):
    inp = input_lines[i]
    if inp[0] == 'insert':
        deque.appendleft(inp[1])
    elif inp[0] == 'deleteFirst':
        deque.popleft()
    elif inp[0] == 'deleteLast':
        deque.pop()
    else:
        if inp[1] in deque:
            deque.remove(inp[1])

print(*deque)

Solution (Python):

from collections import deque
n = int(input())
deque = deque()
input_lines = [input() for x in range(n)]

for i in range(n):
    inp = input_lines[i].split()
    if inp[0] == 'insert':
        deque.appendleft(inp[1])
    elif inp[0] == 'deleteFirst':
        deque.popleft()
    elif inp[0] == 'deleteLast':
        deque.pop()
    else:
        if inp[1] in deque:
            deque.remove(inp[1])

print(*deque)

Solution (Python):


from collections import deque

COMMAND = ("insert", "delete", "deleteFirst", "deleteLast")

if __name__ == "__main__":
    n = int(input())
    ans = deque()
    for i in range(n):
        com = input().split()
        if com[0] == COMMAND[0]:
            ans.appendleft(com[1])
        elif com[0] == COMMAND[1]:
            if com[1] in ans:
                ans.remove(com[1])
        elif com[0] == COMMAND[2]:
            ans.popleft()
        elif com[0] == COMMAND[3]:
            ans.pop()
    print(" ".join(ans))

Solution (Python):


from collections import deque
import sys

COMMAND = ("insert", "delete", "deleteFirst", "deleteLast")

if __name__ == "__main__":
    n = int(sys.stdin.readline())
    ans = deque()
    inp = sys.stdin.readlines()
    for i in range(n):
        com = inp[i].split()
        if com[0] == COMMAND[0]:
            ans.appendleft(com[1])
        elif com[0] == COMMAND[1]:
            if com[1] in ans:
                ans.remove(com[1])
        elif com[0] == COMMAND[2]:
            ans.popleft()
        elif com[0] == COMMAND[3]:
            ans.pop()
    print(" ".join(ans))

Solution (Python):
from collections import deque
n = int(input())
a = deque()
for i in range(n):
    s = input().split(" ")
    if s[0] == "insert":
        a.appendleft(int(s[1]))
    elif s[0] == "delete":
        if int(s[1]) in a:
            a.remove(int(s[1]))
    elif s[0] == "deleteLast":
        a.pop()
    elif s[0] == "deleteFirst":
        a.popleft()
b = list(a)
for i in range(len(b)):
    if i == len(b)-1:
        print(b[i])
    else:
        print(b[i],end=" ")

Solution (Python):
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))

Solution (Python):

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

Solution (Python):

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(stdin.readline().strip())

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

Solution (Python):
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()

Solution (Python):
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()

Solution (Python):
from collections import deque

n = int(input())
data = deque()

for i in range(n):
	command = input().split()
	if len(command) == 2:
		command,x = command
	else:
		command = command[0]
	if command == 'insert':
		data.appendleft(x)
	elif command == 'delete':
		if x in data:
			data.remove(x)
	elif command == 'deleteFirst':
		data.popleft()
	elif command == 'deleteLast':
		data.pop()

print(*data)

Solution (Python):


"""
???????????£????????????
??\?????????????????????????????????????????£???????????????????£??????????????????????

insert x: ??£?????????????????????????????? x ?????????????´?????¶?????¶???????
delete x: ?????? x ??????????????????????´??????£????????????????????????????????????????????????????´?????????¨???????????´????????????????????????
deleteFirst?????????????????????????´???????????????????
deleteLast: ????????????????°????????´???????????????????
"""
import sys

NoValue = -1
Prev = 0
Next = 1
Value = 2

class Dlist:
    """ ??????????????????????????? """

    def __init__(self):
        """ ????????????????????? """
        self.items = [0] * 2000000
        self.clear()

    def clear(self):
        """ ????´?????????????????????¢ """
        self.si = NoValue
        self.ei = NoValue
        self.pos = NoValue
        self.uniqid = 0
        self.cnt = 0
        
    def insert(self,x):
        """ ??£?????????????????????????????? x ?????????????´?????¶?????¶???? """

        n = NoValue
        if self.cnt == 0: 
            self.ei = self.uniqid
        else:
            self.items[self.si][Prev] = self.uniqid
            n = self.si
        self.items[self.uniqid] = [NoValue, n, x]
        self.si = self.uniqid
        self.uniqid += 1
        self.cnt += 1
    
    def delete(self,x):
        """ ?????? x ??????????????????????´??????£????????????????????????????????????????????????????´?????????¨???????????´????????????????????????"""

        if self.cnt > 0:
            hit = False
            i = self.si
            while hit == False:
                if i == -1:
                    break
                if self.items[i][Value] == x:
                    hit = True
                    if i == self.si:
                        self.deleteFirst() 
                    elif i == self.ei:
                        self.deleteLast() 
                    else:
                        p = self.items[i][Prev]
                        n = self.items[i][Next]
                        self.items[n][Prev] = p
                        self.items[p][Next] = n

                        self.cnt -= 1
                else:
                    i = self.items[i][Next]

    def deleteFirst(self):
        """ ?????????????????????????´???????????????? """
        if self.cnt < 2:
            self.clear()
        else:
            n = self.items[self.si][Next]
            if n != NoValue:
                self.items[n][Prev] = NoValue

            self.si = n
            self.cnt -= 1

    def deleteLast(self):
        """ ????????????????°????????´???????????????????"""
        if self.cnt < 2:
            self.clear()
        else:
            p = self.items[self.ei][Prev]
            if p != -1:
                self.items[p][Next] = NoValue

            self.ei = p
            self.cnt -= 1

    def getList(self):
        a = []
        if self.cnt > 0:
            i = self.si
            while i != -1:
                a.append(self.items[i][Value])
                i = self.items[i][Next]
        return a

def main():
    """ ????????? """
    num = int(input().strip())
    istr = sys.stdin.read()
    cmds = list(istr.splitlines())
    dl = Dlist()

    for i in cmds:
        if i[6] == "F":
            dl.deleteFirst()
        elif i[6] == "L":
            dl.deleteLast()
        elif i[0] == "i":
            dl.insert(i[7:])
        elif i[0] == "d":
            dl.delete(i[7:])






    print(" ".join(dl.getList()))


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque
n=int(input())
dq=deque()

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

Solution (Python):
from collections import deque
import sys
n = int(input())
deq = deque()
sub_deq = {}
for cmd in sys.stdin.readlines():
    if cmd == 'deleteFirst\n':
        while deq:
            data = deq.pop()
            if data[1]:
                data[1] = 0
                break
    elif cmd == 'deleteLast\n':
        while deq:
            data = deq.popleft()
            if data[1]:
                data[1] = 0
                break
    else:
        cmd, val = cmd.split()
        if cmd == 'insert':
            data = [val, 1]
            deq.append(data)
            if val not in sub_deq:
                sub_deq[val] = [data]
            else:
                sub_deq[val].append(data)
        elif cmd == 'delete':
            if val in sub_deq:
                deq_v = sub_deq[val]
                while deq_v:
                    data = deq_v.pop()
                    if data[1]:
                        data[1] = 0
                        break
sys.stdout.write(" ".join(data[0] for data in reversed(deq) if data[1]))
sys.stdout.write('\n')

Solution (Python):
from collections import deque
from sys import stdin
n = int(input())
ddl = deque()
for i in stdin:
    inp = i.split()
    if len(inp) == 2:
        op, data = inp
        data = int(data)
    else: op, data = inp[0], None
    if op == 'insert':
        ddl.appendleft(data,)
    elif op == 'delete':
        try:
            ddl.remove(data)
        except ValueError:
            pass
    elif op == 'deleteFirst':
        ddl.popleft()
    elif op == 'deleteLast':
        ddl.pop()

print(' '.join([str(item) for item in ddl]))

Solution (Python):
from collections import deque
from sys import stdin
n = int(input())
line = stdin.readlines()
ddl = deque()
for i in range(n):
    inp = line[i].split()
    if len(inp) == 2:
        op, data = inp
        data = int(data)
    else: op, data = inp[0], None
    if op == 'insert':
        ddl.appendleft(data,)
    elif op == 'delete':
        try:
            ddl.remove(data)
        except ValueError:
            pass
    elif op == 'deleteFirst':
        ddl.popleft()
    elif op == 'deleteLast':
        ddl.pop()

print(' '.join([str(item) for item in ddl]))

Solution (Python):
from collections import deque
import sys
 
dq = deque()
num = int(sys.stdin.readline())
line = sys.stdin.readlines()
 
for i in range(num):
 
    order = line[i].split()
 
    if order[0] == 'insert':
        dq.appendleft(order[1])
    elif order[0] == 'deleteFirst':
        dq.popleft()
    elif order[0] == 'deleteLast':
        dq.pop()
    else:
        if order[1] in dq:
            dq.remove(order[1])
 
print(' '.join(dq))

Solution (Python):
from collections import deque
n=int(input())
commands=[input().split() for i in range(n)]
    
List=deque()
for list_command in commands:
    if 'insert' in list_command:
        List.appendleft(list_command[1])
    elif 'delete' in list_command:
        try:
            List.remove(list_command[1])
        except:
            pass
    elif 'deleteFirst' in list_command:
        List.popleft()
    elif 'deleteLast' in list_command:
        List.pop()
  
print(*List)

Solution (Python):
from collections import deque
queue = deque([])
n = int(input())
for _ in range(n):
    command = input()
    if command == "deleteFirst":
        _ = queue.popleft()
    elif command == "deleteLast":
        _ = queue.pop()
    elif "delete" in command:
        _, x = command.split()
        try:
            queue.remove(x)
        except ValueError:
            pass
    else:
        _, x = command.split()
        queue.appendleft(x)
print(" ".join(queue))

Solution (Python):
n = int(raw_input())
L = []
c = 0
for i in xrange(n):
    cmd = raw_input()
    if cmd[0] == "i":
        L.append(cmd[7:])
    elif cmd[6] == " ":
        try:
            L.pop(~L[::-1].index(cmd[7:]))
        except:
            pass
    elif cmd[6] == "F":
        L.pop()
    else:
        c += 1

print " ".join(map(str, L[c:][::-1]))

Solution (Python):
import sys
from collections import deque
input_lines = sys.stdin.readlines()

doublyLinkedList = deque()


for x in input_lines[1:]:
    oneLine = x.split()
    if 'insert' in oneLine[0]:
        doublyLinkedList.appendleft((int)(oneLine[1]))
    elif 'delete' == oneLine[0]:
        remove_value = (int)(oneLine[1])
        if remove_value in doublyLinkedList:
            doublyLinkedList.remove(remove_value)
    elif 'deleteFirst'in oneLine[0]:
        doublyLinkedList.popleft()
    elif 'deleteLast' in oneLine[0]:
        doublyLinkedList.pop()

print(*doublyLinkedList)

Solution (Python):
from collections import deque

n = int(input())
d = deque()
for i in range(n):
    tmp = input().split()
    ope = tmp[0]
    if ope == "insert":
        m = tmp[1]
        d.appendleft(m)
    elif ope == "delete":
        m = tmp[1]
        if m in d:
            d.remove(m)
    elif ope == "deleteFirst":
        d.popleft()
    elif ope == "deleteLast":
        d.pop()
print(*d)

Solution (Python):
import sys
from collections import deque
n = int(sys.stdin.readline())
lines = sys.stdin.readlines()

Q = deque()
for i in range(n):
    command = lines[i].split()
    if command[0] == 'insert':
        Q.appendleft(command[1])
    elif command[0] == 'delete':
        try:
            Q.remove(command[1])
        except:
            pass
    elif command[0] == 'deleteFirst':
        Q.popleft()
    elif command[0] == 'deleteLast':
        Q.pop()
print(' '.join(Q))

Solution (Python):
from collections import deque
class DoublyLinkedKist:
    DLlist = deque()

    def command(self, cmd):
        if cmd[0] == 'insert':
            self.DLlist.appendleft(cmd[1])
        elif cmd[0] == 'delete':
            if cmd[1] in self.DLlist:
                self.DLlist.remove(cmd[1])
        elif cmd[0] == 'deleteFirst':
            self.DLlist.popleft()
        elif cmd[0] == 'deleteLast':
            self.DLlist.pop()

if __name__ == '__main__':
    DL = DoublyLinkedKist()
    for i in range(int(input())):
        DL.command(input().split())
    print(*DL.DLlist)

Solution (Python):

import sys


class Cell(object):
    __slots__ = ["value", "pre", "next"]

    def __init__(self, value=None, pre=None, _next=None):
        if pre is None and _next is None:
            self.value = None
            self.pre = self
            self.next = self
        else:
            self.value = value
            self.pre = pre
            self.next = _next


class MyList(object):
    def __init__(self):
        self._nil = Cell()

    def insert(self, value):
        self._nil.next = Cell(value, self._nil, self._nil.next)
        self._nil.next.next.pre = self._nil.next

    def delete(self, value):
        self._delete_to(self._search(value))

    def delete_first(self):
        self._delete_to(self._nil.next)

    def delete_last(self):
        self._delete_to(self._nil.pre)

    def toList(self):
        gen = self._val_generator()
        return [val for val in gen()]

    def _val_generator(self):
        def gen():
            current = self._nil.next
            while current is not self._nil:
                yield current.value
                current = current.next
        return gen

    def _delete_to(self, cell):
        if cell is not self._nil:
            cell.next.pre = cell.pre
            cell.pre.next = cell.next

    def _search(self, value):
        current = self._nil.next
        while current is not self._nil and current.value != value:
            current = current.next
        return current


def doubly_inked_list(commands):
    lst = MyList()
    for command in commands:
        if command[0] == "insert":
            lst.insert(command[1])
        elif command[0] == "delete":
            lst.delete(command[1])
        elif command[0] == "deleteFirst":
            lst.delete_first()
        elif command[0] == "deleteLast":
            lst.delete_last()
        else:
            raise ValueError
    return lst.toList()


if __name__ == "__main__":
    commands = [val.split() for val in sys.stdin.read().splitlines()]
    pop = commands.pop(0)
    print(" ".join(doubly_inked_list(commands)))

Solution (Python):
from collections import deque
import sys

dq = deque()
num = int(sys.stdin.readline())
line = sys.stdin.readlines()

for i in range(num):

    order = line[i].split()

    if order[0] == 'insert':
        dq.appendleft(order[1])
    elif order[0] == 'deleteFirst':
        dq.popleft()
    elif order[0] == 'deleteLast':
        dq.pop()
    else:
        if order[1] in dq:
            dq.remove(order[1])

print(' '.join(dq))

Solution (Python):
from collections import deque

queue = deque([])

n = int(input())

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

last = queue.pop()
for i in queue:
    print(i, end=" ")

print(last)

Solution (Python):
from collections import deque

queue = deque([])

for _ in range(int(input())):
    commands = input().split(" ")
    command = commands[0]
    if command == "insert":
        queue.appendleft(commands[1])
    elif command == "delete":
        try:
            queue.remove(commands[1])
        except ValueError:
            pass
    elif command == "deleteFirst":
        queue.popleft()
    elif command == "deleteLast":
        queue.pop()

print(" ".join(queue))

Solution (Python):
from collections import deque

queue = deque()

for _ in range(int(input())):
    commands = input().split(" ")
    if commands[0] == "insert":
        queue.appendleft(commands[1])
    elif commands[0] == "delete":
        try:
            queue.remove(commands[1])
        except ValueError:
            pass
    elif commands[0] == "deleteFirst":
        queue.popleft()
    elif commands[0] == "deleteLast":
        queue.pop()

print(" ".join(queue))

Solution (Python):
import sys
from collections import deque
n = int(input())
q = deque()
for i in range(n):
    c = sys.stdin.readline()[:-1]
    if c[0] == 'i':
        q.appendleft(c[7:])
    elif c[6] == ' ':
        try:
            q.remove(c[7:])
        except:
            pass
    elif c[6] == 'F':
        q.popleft()
    else:
        q.pop()
print(*q)

Solution (Python):
from collections import deque

def main():

    queue = deque()

    for _ in range(int(input())):
        commands = input().split(" ")
        if commands[0] == "insert":
            queue.appendleft(commands[1])
        elif commands[0] == "delete":
            try:
                queue.remove(commands[1])
            except ValueError:
                pass
        elif commands[0] == "deleteFirst":
            queue.popleft()
        elif commands[0] == "deleteLast":
            queue.pop()

    print(" ".join(queue))

main()

Solution (Python):
import sys
from collections import deque

queue = deque()

for _ in range(int(sys.stdin.readline())):
    commands = sys.stdin.readline()[:-1].split(" ")
    if commands[0] == "insert":
        queue.appendleft(commands[1])
    elif commands[0] == "delete":
        try:
            queue.remove(commands[1])
        except ValueError:
            pass
    elif commands[0] == "deleteFirst":
        queue.popleft()
    elif commands[0] == "deleteLast":
        queue.pop()

print(" ".join(queue))

Solution (Python):
from collections import deque
n = int(input())

d = deque()

for _ in range(n):
    com = input()
    if (com[0] == 'i'):
        com ,i = com.split()
        d.appendleft(int(i))
    elif (com[0] == 'd'):
        if (com[6] == ' '):
            com, i = com.split()
            try:
                d.remove(int(i))
            except ValueError:
                pass
        elif (com[6] == 'F'):
            d.popleft()
        elif (com[6] == 'L'):
            d.pop()
ans = map(str, d)
ans = " ".join(ans)
print(ans)

Solution (Python):
from collections import deque
n = int(input())

d = deque()

for _ in range(n):
    com = input()
    if (com[0] == 'i'):
        com ,i = com.split()
        d.appendleft(int(i))
    elif (com[0] == 'd'):
        if (com[6] == ' '):
            com, i = com.split()
            try:
                d.remove(int(i))
            except ValueError:
                pass
        elif (com[6] == 'F'):
            d.popleft()
        elif (com[6] == 'L'):
            d.pop()
ans = map(str, d)
ans = " ".join(ans)
print(ans)