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



NEXT = 2
DATA = 1
PREV = 0
from collections import deque
def dll_processor(operations):
    
    front = None
    end = None
    dll = deque([])
    for o in operations:
        if o[0] == "insert":
            dll.appendleft(o[1])
            
        elif o[0] == "delete":
            if o[1] in dll:
                dll.remove(o[1])
            
        elif o[0] == "deleteFirst":
            dll.popleft()
            
        elif o[0] == "deleteLast":
            dll.pop()
            
        
        
    return dll

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

Solution (Python):



NEXT = 2
DATA = 1
PREV = 0
from collections import deque
def dll_processor(operations):
    
    front = None
    end = None
    dll = deque([])
    for o in operations:
        if o[0] == "insert":
            dll.appendleft(o[1])
            
        elif o[0] == "delete":
            if o[1] in dll:
                dll.remove(o[1])
            
        elif o[0] == "deleteFirst":
            dll.popleft()
            
        else:
            dll.pop()
            
        
        
    return dll

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

Solution (Python):



NEXT = 2
DATA = 1
PREV = 0
from collections import deque
def dll_processor(operations):
    
    front = None
    end = None
    dll = deque([])
    for o in operations:
        if o[0] == "insert":
            dll.appendleft(o[1])
            
        elif o[0] == "delete":
            try:
                dll.remove(o[1])
            except:
                pass
            
        elif o[0] == "deleteFirst":
            dll.popleft()
            
        elif o[0] == "deleteLast":
            dll.pop()
            
        
        
    return dll

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

Solution (Python):



''' ???????????£???????????? '''

from collections import deque

functions = {"insert": lambda obj, x: obj.insert(x),
             "delete": lambda obj, x: obj.delete(x),
             "deleteFirst": lambda obj: obj.deleteFirst(),
             "deleteLast": lambda obj: obj.deleteLast()}


class DoublyLinkedList(object):
    """ Doubly Linked List """

    def __init__(self):
        
        self.list = deque()

    def insert(self, x):
        """ ???????????????x?????????????´?????????\ """
        
        self.list.appendleft(x)

    def delete(self, x):
        """ ??????x??????????????????????´?????????? """
        
        
        
        
        if x in self.list:
            self.list.remove(x)

    def deleteFirst(self):
        """ ?????????????????????????´?????????? """
        
        self.list.popleft()

    def deleteLast(self):
        """ ????????????????°????????´?????????? """
        self.list.pop()


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

    q = deque()
    for i in range(N):
        command = input().split()
        if command[0] == 'insert':
            q.appendleft(command[1])
        elif command[0] == 'delete':
            if command[1] in q:
                q.remove(command[1])
        elif command[0] == 'deleteFirst':
            q.popleft()
        elif command[0] == 'deleteLast':
            q.pop()

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

Solution (Python):


from collections import deque

result = deque()

def operation(command):
    if command[0] == "insert":
        result.appendleft(command[1])
    elif command[0] == "delete":
        if command[1] in result:
            result.remove(command[1])
    elif command[0] == "deleteFirst":
        result.popleft()
    elif command[0] == "deleteLast":
        result.pop()

n = int(input())
for i in range(n):
    command = input().split()
    operation(command)
print(*result)

Solution (Python):
from collections import deque

q = deque()

for i in range(int(input())):
  command_line = input().split(" ")
  command = command_line[0]
  arg = ""
  if len(command_line) > 1: arg = command_line[1]

  if command == "insert":
    q.appendleft(arg)
  elif command == "delete":
    try:
      q.remove(arg)
    except ValueError:
      pass
  elif command == "deleteFirst":
    q.popleft()
  else:
    q.pop()

print(" ".join(q))

Solution (Python):

from collections import deque

n = int(input())
linked_list = deque([])

for i in range(n):
    line = input().split()
    cmd = line[0]

    if len(line) == 2:
        value = line[1]

    if cmd == "insert":
        linked_list.appendleft(value)
    elif cmd == "delete":
        if value in linked_list:
            linked_list.remove(value)
    elif cmd == "deleteFirst":
        linked_list.popleft()
    elif cmd == "deleteLast":
        linked_list.pop()

print(*linked_list)

Solution (Python):

import sys
from collections import deque

n = int(sys.stdin.readline())
linked_list = deque([])

for i in range(n):
    line = sys.stdin.readline().split()
    cmd = line[0]

    if len(line) == 2:
        value = line[1]

    if cmd == "insert":
        linked_list.appendleft(value)
    elif cmd == "delete":
        if value in linked_list:
            linked_list.remove(value)
    elif cmd == "deleteFirst":
        linked_list.popleft()
    elif cmd == "deleteLast":
        linked_list.pop()

print(*linked_list)

Solution (Python):

import sys
from collections import deque

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

inputs = []

for i in range(n):
    inputs.append(sys.stdin.readline().split())

linked_list = deque([])

for line in inputs:
    cmd = line[0]

    if len(line) == 2:
        value = line[1]

    if cmd == "insert":
        linked_list.appendleft(value)
    elif cmd == "delete":
        if value in linked_list:
            linked_list.remove(value)
    elif cmd == "deleteFirst":
        linked_list.popleft()
    elif cmd == "deleteLast":
        linked_list.pop()

print(*linked_list)

Solution (Python):
from collections import deque

n = int(input())

l = deque()
for _ in range(n):
    command = input().split()
    if command[0] == "insert":
        l.appendleft(command[1])
    elif command[0] == "delete":
        try:
            ind = l.remove(command[1])
        except ValueError:
            pass
    elif command[0] == "deleteFirst":
        l.popleft()
    elif command[0] == "deleteLast":
        l.pop()
        
print (" ".join(l))

Solution (Python):
import sys
from collections import deque
n = int(sys.stdin.readline())
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):
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
n = int(input())
q = deque()
for i in range(n):
    c = input()
    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
order = int(input())
llist = [input().split() for _ in range(order)]
ans = deque([])
count = 0
for lst in llist:
    if lst[0] == 'insert':
        ans.appendleft(lst[1])
    
    elif lst[0] == 'delete':
        if lst[1] in ans:
            ans.remove(lst[1])
   
    elif lst[0] == 'deleteFirst':
        ans.popleft()
    
    elif lst[0] == 'deleteLast':
        ans.pop()
        
    count += 1

print(*ans)

Solution (Python):
from collections import deque
order = int(input())
llist = [input().split() for _ in range(order)]
ans = deque([])
count = 0
for lst in llist:
    if lst[0] == 'insert':
        ans.appendleft(lst[1])
    
    elif lst[0] == 'delete':
        if lst[1] in ans:
            ans.remove(lst[1])
   
    elif lst[0] == 'deleteFirst':
        ans.popleft()
    
    elif lst[0] == 'deleteLast':
        ans.pop()
        
    count += 1

print(*ans)

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

Solution (Python):
from _collections import deque

n = int(input())
data_input = []
dlList = deque()
for i in range(n):
    x = input().split()
    if x[0] == 'insert':
        dlList.appendleft(x[1])
    elif x[0] == 'delete':
        if x[1] in dlList:
            dlList.remove(x[1])
        else :
            pass
    elif x[0] == 'deleteFirst':
        dlList.popleft()
    elif x[0] == 'deleteLast':
        dlList.pop()
    else:
        pass

print(' '.join(dlList))

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

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

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] == "deleteFirst":
        Q.popleft()
    elif command[0] == "deleteLast":
        Q.pop()
    elif command[0] == "delete":
        try:
            Q.remove(command[1])
        except:
            pass
print(" ".join(Q))

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

Solution (Python):
n = input()
lis = []
ans = 0
for i in xrange(n):
    com = raw_input()
    if   com[0] == 'i':
        lis.append(com[7:])
    elif com[6] == ' ':
        try:
            lis.pop(~lis[::-1].index(com[7:]))
        except:
            pass
    elif com[6] == 'F':
        lis.pop()
    else:ans += 1
  
print ' '.join( map(str, reversed(lis[ans:])) )

Solution (Python):

n = input()
lis = []
ans = 0
for i in xrange(n):
    com = raw_input()
    if   com[0] == 'i':
        lis.append(com[7:])
    elif com[6] == ' ':
        try:
            lis.pop(~lis[::-1].index(com[7:]))
        except:
            pass
    elif com[6] == 'F':
        lis.pop()
    else:ans += 1
  
print ' '.join( map(str, reversed(lis[ans:])) )

Solution (Python):

n = input()
lis = []
ans = 0
for i in xrange(n):
    com = raw_input()
    if   com[0] == 'i':
        lis.append(com[7:])
    elif com[6] == ' ':
        try:
            lis.pop(~lis[::-1].index(com[7:]))
        except:
            pass
    elif com[6] == 'F':
        lis.pop()
    else:ans += 1
  
print ' '.join( map(str, reversed(lis[ans:])) )

Solution (Python):
from collections import deque

l = deque([])
n = int(input())
for i in range(n):
    com = input()
    if com == "deleteFirst":
        a = l.popleft()
    elif com == "deleteLast":
        a = l.pop()
    else:
        m = com.split()
        if m[0] == "insert":
            l.appendleft(m[1])
        else:
            try:
                l.remove(m[1])
            except:
                a = 1
print(" ".join(l))

Solution (Python):
class MyList:
	head = tail = None

	def insert(self, x):
		n = [x, None, None]
		if self.head == None:
			self.head = self.tail = n
		else:
			n[2] = self.head
			self.head[1] = n
			self.head = n

	def delete(self, x):
		n = self.head
		while n and n[0] != x:
			n = n[2]
		if n: 
			self.__delete_node(n)

	def deleteFirst(self):
		self.__delete_node(self.head)

	def deleteLast(self):
		self.__delete_node(self.tail)

	def __delete_node(self, n):
		if n == self.head:
			self.head = n[2]
		if n == self.tail:
			self.tail = n[1]
		if n[1]:
			n[1][2] = n[2]
		if n[2]:
			n[2][1] = n[1]

	def print_all(self):
		n = self.head
		while n:
			print n[0],
			n = n[2]

myList = MyList()
n = int(raw_input())
for _ in xrange(n):
	line = raw_input()
	if line[0]=='i':
		myList.insert(line[7:])
	elif line[6]=='F':
		myList.deleteFirst()
	elif line[6]=='L':
		myList.deleteLast()
	else:
		myList.delete(line[7:])
myList.print_all()

Solution (Python):
from collections import deque

N = int(input())

A = deque()

for i in range(N):
	b= input()
	if b == "deleteFirst":
		A.popleft()
	elif b == "deleteLast":
		A.pop()
	else:
		b, c = b.split()
		c = int(c)
	
		if b == "insert":
			A.appendleft(c)
		elif b == "delete":
			if (c in A):
				A.remove(c)

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

Solution (Python):

import sys
from collections import deque

output_str = deque()
data_cnt =  int(input())  

for i in range(data_cnt):
    line = input().rstrip()
    if line.find(" ") > 0:
        in_command, in_str = line.split(" ")
    else:
        in_command = line
    
    if in_command == "insert":
        output_str.appendleft(in_str)
    elif in_command == "delete":
        if output_str.count(in_str) > 0:
            output_str.remove(in_str)
    elif in_command == "deleteFirst":
        output_str.popleft()
    elif in_command == "deleteLast": 
        output_str.pop()

print(" ".join(output_str))

Solution (Python):

import sys
from collections import deque

output_str = deque()
data_cnt =  int(input())  

for i in range(data_cnt):
    in_command = input().rstrip().split(" ")
    
    if in_command[0] == "insert":
        output_str.appendleft(in_command[1])
    elif in_command[0] == "delete":
        if output_str.count(in_command[1]) > 0:
            output_str.remove(in_command[1])
    elif in_command[0] == "deleteFirst":
        output_str.popleft()
    elif in_command[0] == "deleteLast": 
        output_str.pop()

print(" ".join(output_str))

Solution (Python):

import sys
from collections import deque

output_str = deque()
data_cnt =  int(input())  

for i in range(data_cnt):
    in_command = input().rstrip().split(" ")
    
    if in_command[0] == "insert":
        output_str.appendleft(in_command[1])
    elif in_command[0] == "delete":
        try:
            output_str.remove(in_command[1])
        except:
            pass
    elif in_command[0] == "deleteFirst":
        output_str.popleft()
    elif in_command[0] == "deleteLast": 
        output_str.pop()

print(" ".join(output_str))

Solution (Python):

import sys
from collections import deque

output_str = deque()
data_cnt =  int(input())  

for i in range(data_cnt):
    in_command = sys.stdin.readline().rstrip().split(" ")
    if in_command[0] == "insert":
        output_str.appendleft(in_command[1])
    elif in_command[0] == "delete":
        if output_str.count(in_command[1]) > 0:
            output_str.remove(in_command[1])
    elif in_command[0] == "deleteFirst":
        output_str.popleft()
    elif in_command[0] == "deleteLast": 
        output_str.pop()

print(" ".join(output_str))

Solution (Python):
from collections import deque

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

Solution (Python):


import sys
import os
from collections import deque


N = int(input())
q = deque()

for i in range(N):
    lst = input().split()
    command = lst[0]
    if command == 'insert':
        v = int(lst[1])
        q.appendleft(v)
    elif command == 'delete':
        v = int(lst[1])
        if v in q:
            q.remove(v)
    elif command == 'deleteFirst':
        q.popleft()
    elif command == 'deleteLast':
        q.pop()
print(*list(q))

Solution (Python):


import sys
import os
from collections import deque

N = int(input())
q = deque()

for i in range(N):
    lst = input().split()
    command = lst[0]
    if command == 'insert':
        v = int(lst[1])
        q.appendleft(v)
    elif command == 'delete':
        v = int(lst[1])
        try:
            q.remove(v)
        except Exception:
            pass
    elif command == 'deleteFirst':
        q.popleft()
    elif command == 'deleteLast':
        q.pop()
print(*list(q))

Solution (Python):


import sys
import os
from collections import deque


N = int(input())
q = deque()

for i in range(N):
    lst = input().split()
    command = lst[0]
    if command == 'insert':
        v = int(lst[1])
        q.appendleft(v)
    elif command == 'delete':
        v = int(lst[1])
        try:
            q.remove(v)
        except Exception:
            pass
    elif command == 'deleteFirst':
        q.popleft()
    elif command == 'deleteLast':
        q.pop()

print(*q)

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] == "deleteFirst":
        Q.popleft()
    elif command[0] == "deleteLast":
        Q.pop()
    elif command[0] == "delete":
        try:
            Q.remove(command[1])
        except:
            pass
print(" ".join(Q))

Solution (Python):


from collections import deque

N = int(input())
q = deque()

for i in range(N):
    lst = input().split()
    command = lst[0]
    if command == 'insert':
        q.appendleft(lst[1])
    elif command == 'delete':
        try:
            q.remove(lst[1])
        except Exception:
            pass
    elif command == 'deleteFirst':
        q.popleft()
    elif command == 'deleteLast':
        q.pop()

print(*q)

Solution (Python):


import sys
import os
from collections import deque


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

for i in range(N):
    lst = sys.stdin.readline().split()
    command = lst[0]
    if command == 'insert':
        q.appendleft(lst[1])
    elif command == 'delete':
        try:
            q.remove(lst[1])
        except Exception:
            pass
    elif command == 'deleteFirst':
        q.popleft()
    elif command == 'deleteLast':
        q.pop()

print(*q)

Solution (Python):


import sys
import os
from collections import deque


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

lines = sys.stdin.readlines()
for s in lines:
    lst = s.split()
    command = lst[0]
    if command == 'insert':
        q.appendleft(lst[1])
    elif command == 'delete':
        try:
            q.remove(lst[1])
        except Exception:
            pass
    elif command == 'deleteFirst':
        q.popleft()
    elif command == 'deleteLast':
        q.pop()

print(*q)

Solution (Python):
from collections import deque
dq = deque()
for mak in range(int(input())) :
    com = input().split()
    if   com[0] == 'insert'      : dq.appendleft(com[1])
    elif com[0] == 'deleteFirst' : dq.popleft()
    elif com[0] == 'deleteLast'  : dq.pop()
    else : 
        if com[1] in dq :
            dq.remove(com[1])
print(' '.join(dq))

Solution (Python):
from collections import deque
n = int(input())
d = deque()
for _ in range(n):
    x = input().split()
    if x[0][0] == 'i':
        d.appendleft(int(x[1]))
    elif x[0] == 'delete':
        try:
            d.remove(int(x[1]))
        except ValueError:
            pass
    elif x[0][6] == 'F':
        d.popleft()
    else:
        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] == "deleteFirst":
        Q.popleft()
    elif command[0] == "deleteLast":
        Q.pop()
    elif command[0] == "delete":
        try:
            Q.remove(command[1])
        except:
            pass
print(" ".join(Q))

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 = input()
d = deque()
for i in range(int(n)):
	command = input()
	if command[0] == 'i':
		d.appendleft(command[7:])
	else:
		if command[6] == " ":
			if  command[7:] in d:
				d.remove(command[7:])
		else:
			if command[6] == 'L':
				d.pop()
			else:
				d.popleft()
for _ in range(len(d)-1):
	print(d.popleft(), end=" ")
print(d.pop())

Solution (Python):
from collections import deque

class MyList(deque):
    def __init__(self):
        self.__init__ = deque
        self.d = {'deleteFirst':self.delF, 'deleteLast':self.delL, 'insert':self.insX, 'delete':self.delX}
    def cmd(self,command):
        if ' ' in command:
            ope, num = command.split()
            num = int(num)
            self.d[ope](num)
        else:
            self.d[command]()
            
    def delF(self):
        self.popleft()

    def delL(self):
        self.pop()

    def insX(self,num):
        self.appendleft(num)

    def delX(self,num):
        if num in self:
            self.remove(num)

if __name__ == '__main__' :
    l = MyList()
    n = input()
    
    for i in range(n):
        l.cmd(raw_input())

    q = deque(l)
    for i in range(len(l)-1):
        print q.popleft(),
    print l[-1]

Solution (Python):


import collections
s=int(input())
deq =collections.deque()

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

Solution (Python):
import sys
from collections import deque

def doubly_lined_list():
	n = int(input())
	Q = deque()

	for i in range(n):
		line = sys.stdin.readline()
		command = line.split()[0]

		if command == 'insert':
			Q.appendleft(line.split()[1])
		elif command == 'delete':
			try:
				Q.remove(line.split()[1])
			except:
				pass
		elif command == 'deleteFirst':
			Q.popleft()
		elif command == 'deleteLast':
			Q.pop()

	print(' '.join(Q))

if __name__ == '__main__':
	doubly_lined_list()

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

for i in range(n):
    cmdline = input().split()

    if cmdline[0] == "insert":
        L.appendleft(cmdline[1])
    elif cmdline[0] == "delete":
        try:
            L.remove(cmdline[1])
        except ValueError: pass
    elif cmdline[0] == "deleteFirst":
        L.popleft()
    elif cmdline[0] == "deleteLast":
        L.pop()

print(" ".join(L))

Solution (Python):
import sys
res = dict()
dll = [None] * 2000000
left = 0
right = 0
n = int(input())
for inpt in sys.stdin.read().splitlines():
    i = inpt.split()
    if i[0] == "insert":
        x = int(i[1])
        dll[left] = x
        if(x in res):
            res[x].add(left)
        else:
            res[x] = set([left])
        left += 1
    if i[0] == "delete":
        x = int(i[1])
        if(x in res):
            ind = max(res[x])
            dll[ind] = None
            res[x].remove(ind)
            if(len(res[x]) == 0):
                del res[x]
            if(ind == (left - 1)):
                left = ind
            if(ind == right):
                right += 1
    if i[0] == "deleteFirst":
        left -= 1
        x = dll[left]
        res[x].remove(left)
        if(len(res[x]) == 0):
            del res[x]
        dll[left] = None
    if i[0] == "deleteLast":
        right += 1
    
ret = []
for x in dll[right:left]:
    if(x is not None):
        ret.append(str(x))
ret.reverse()
print(" ".join(ret))

Solution (Python):

import collections
q = collections.deque()
cmds={"insert":lambda cmd: q.appendleft(cmd[1]),
      "delete":lambda cmd: q.remove(cmd[1]) if (q.count(cmd[1]) > 0) else "none",
      "deleteFirst":lambda cmd: q.popleft(),
      "deleteLast": lambda cmd: q.pop()
     }

n=int(input())
for i in range(n):
    cmd=input().split()
    cmds[cmd[0]](cmd)
print(*q)

Solution (Python):

import collections
import sys
q = collections.deque()
n=int(input())
_input = sys.stdin.readlines()
cmds={"insert":lambda cmd: q.appendleft(cmd[1]),
      "delete":lambda cmd: q.remove(cmd[1]) if (q.count(cmd[1]) > 0) else "none",
      "deleteFirst":lambda cmd: q.popleft(),
      "deleteLast": lambda cmd: q.pop()
     }

for i in range(n):
    cmd=_input[i].split()
    cmds[cmd[0]](cmd)
print(*q)

Solution (Python):

from collections import deque
n = int(input())
deque = deque()

for i in range(n):
    inp = list(input().split())
    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)