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):
class D_Linked_List:
    """Doubly Linked List class.

    ?????¢?????´???????????¨??????????§???????p.96-100.
    """
    class Node:
        def __init__(self, key, next = None, prev = None):
            self.next = next
            self.prev = prev
            self.key = key

    def __init__(self):
        self.nil = D_Linked_List.Node(None)
        self.nil.next = self.nil
        self.nil.prev = self.nil

    def insert(self, key):
        node_x = D_Linked_List.Node(key, self.nil.next, self.nil)
        self.nil.next.prev = node_x
        self.nil.next = node_x

    def _listSearch(self, key):
        cur_node = self.nil.next
        while (cur_node != self.nil) and (cur_node.key != key):
            cur_node = cur_node.next
        return cur_node

    def _deleteNode(self, node):
        if node == self.nil:
            return None
        node.prev.next = node.next
        node.next.prev = node.prev

    def deleteFirst(self):
        self._deleteNode(self.nil.next)

    def deleteLast(self):
        self._deleteNode(self.nil.prev)

    def deleteKey(self, key):
        node = self._listSearch(key)
        self._deleteNode(node)

    def show_keys(self):    
        cur_node = self.nil.next
        keys = []
        while cur_node != self.nil:
            keys.append(cur_node.key)
            cur_node = cur_node.next
        print(' '.join(keys))


import sys

d_ll = D_Linked_List()

for i in sys.stdin:
    if 'insert' in i:
        x = i[7:-1]
        d_ll.insert(x)
    elif 'First' in i:
        d_ll.deleteFirst()
    elif 'Last' in i:
        d_ll.deleteLast()
    elif 'delete' in i:
        x = i[7:-1]
        d_ll.deleteKey(x)
    else:   
        pass

d_ll.show_keys()

Solution (Python):

n = input()
l = []
tail = 0
for i in xrange(n):
    cmd = raw_input().split()
    if cmd[0] == "insert":
        l.append(cmd[1])
    elif cmd[0] == "delete":
        try:
            l.pop(len(l) - l[::-1].index(cmd[1]) -1)
        except ValueError:
            pass
    elif cmd[0] == "deleteFirst":
        l.pop()
    elif cmd[0] == "deleteLast":
        tail += 1

print " ".join(l[tail:][::-1])

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

Solution (Python):
class DoublyLinkedList:
    def __init__(self):
        self.nil = self.make_node(None)
        self.nil['next'] = self.nil
        self.nil['prev'] = self.nil

    def make_node(self,v):
        return {'key':v, 'next':None, 'prev':None}

    def dump(self):
        n = self.nil['next']
        while True:
            if n['key'] == None:
                break
            else:
                print str(n['key']),
                n = n['next']
        print


    def insert(self,v):
        n = self.make_node(v)
        n['next'] = self.nil['next']
        self.nil['next']['prev'] = n
        n['prev'] = self.nil
        self.nil['next'] = n

    def delete(self,v):
        self.delete_node(self.list_search(v))

    def delete_node(self,n):
        if n['key'] == None: return
        n['next']['prev'] = n['prev']
        n['prev']['next'] = n['next']

    def list_search(self,v):
        cur = self.nil['next']
        while (cur['key'] != None) and (cur['key'] != v):
            cur = cur['next']
        return cur

    def deleteFirst(self):
        self.delete_node(self.nil['next'])

    def deleteLast(self):
        self.delete_node(self.nil['prev'])

n = int(raw_input())
commands = []
for i in range(n):
    commands.append(raw_input())

l = DoublyLinkedList()
for c in commands:
    a = c[6]
    if a == "F":
        l.deleteFirst()
    elif a == "L":
        l.deleteLast()
    else:
        val = int(c[7:])
        op = c[0]
        if op == "i":
            l.insert(val)
        else:
            l.delete(val)

l.dump()

Solution (Python):

import collections
n = int(input())
command = [input().split() for i in range(n)]
ans = collections.deque()

for i in range(n):
    if command[i][0] == "insert":
        ans.appendleft(command[i][1])
    elif ans == []:
        continue
    elif command[i][0] == "delete":
        try:
            ans.remove(command[i][1])
        except:
            continue
    elif command[i][0] == "deleteFirst":
        ans.popleft()
    else:
        ans.pop()

print (' '.join(ans))

Solution (Python):

import collections
n = int(input())
ans = collections.deque()

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

print (' '.join(ans))

Solution (Python):
from collections import deque
  
n = int(input())
dll = deque()
  
for i in range(n):
    input_line = input().split()
    if input_line[0] == "insert":
        dll.appendleft(input_line[1])
    elif len(dll) == 0:
        continue
    elif input_line[0] == "delete":
        try:
            dll.remove(input_line[1])
        except:
            pass
    elif input_line[0] == "deleteFirst":
        dll.popleft()
    else:
        dll.pop()
          
print(*dll)

Solution (Python):
from collections import deque
   
n = int(input())
dll = deque()
   
for i in range(n):
    input_line = input().split()
    if input_line[0] == "insert":
        dll.appendleft(input_line[1])
    elif input_line[0] == "delete":
        try:
            dll.remove(input_line[1])
        except:
            pass
    elif input_line[0] == "deleteFirst":
        dll.popleft()
    else:
        dll.pop()
           
print(*dll)

Solution (Python):
from functools import reduce
from collections import deque


def operate(dq, op):
    if op[0] == 'i':
        dq.appendleft(int(op[7:]))
    else:
        k = op[6]
        if k == 'F':
            dq.popleft()
        elif k == 'L':
            dq.pop()
        else:
            try:
                dq.remove(int(op[7:]))
            except ValueError:
                pass
    return dq


n = int(input())
print(*reduce(operate, [input() for _ in range(n)], deque()))

Solution (Python):
class DoublyLinkedList:
    def __init__(self):
        self.nil = self.make_node(None)
        self.nil['next'] = self.nil
        self.nil['prev'] = self.nil
    def make_node(self, k):
        return {'key':k, 'next':None,'prev':None}

    def insert(self, key):
        x = self.make_node(key)
        x['next'] = self.nil['next']
        self.nil['next']['prev'] = x
        self.nil['next'] = x
        x['prev'] = self.nil

    def listSearch(self,key):
        cur = self.nil['next']
        while cur['key'] != None and cur['key'] != key:
            cur = cur['next']
        return cur

    def delete(self, k):
        self.delete_node(self.listSearch(k))

    def delete_node(self,x):
        if x['key'] == None: return
        x['prev']['next'] = x['next']
        x['next']['prev'] = x['prev']

    def deleteFirst(self):
        self.delete_node(self.nil['next'])

    def deleteLast(self):
        self.delete_node(self.nil['prev'])

    def printList(self):
        x = self.nil['next']
        while True:
            if x['key'] == None:
                break
            else:
                print x['key'],
                x = x['next']
        print

def main():
    N = int(raw_input())
    commands = []
    for i in xrange(N):
        commands.append(raw_input())
    d = DoublyLinkedList()
    for c in commands:
        a = c[0]
        if a == 'i':
            d.insert(int(c[7:]))
        else:
            if c[6] == 'F':
                d.deleteFirst()
            elif c[6] == 'L':
                d.deleteLast()
            else:
                d.delete(int(c[7:]))

    d.printList()
    return 0

main()

Solution (Python):
from collections import deque

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

Solution (Python):
A=[]
fp =0
bp =0
 
n=int(raw_input())
while n:
    s = raw_input()
    if s[0] == 'i':
        A.append(int(s[7:]))
        fp += 1
    elif s[6] == ' ':
        try:
            i = A[::-1].index(int(s[7:]))
            if i!=-1:
                del A[-i-1]
                fp -=1
        except:
            pass
    elif s[6] == 'F':
        A.pop()
        fp -=1
    elif s[6] == 'L':
        bp +=1
    n -=1
 
for e in A[bp:fp+1][::-1]:
    print int(e),

Solution (Python):
A=[]
fp =0
bp =0
n=int(raw_input())
while n: 
    cmd = raw_input()
    if cmd[0] == 'i':
        A.append(int(cmd[7:]))
        fp += 1
    elif cmd[6] == ' ':
        try:
            i = A[::-1].index(int(cmd[7:]))
            if i!=-1:
                del A[-i-1]
                fp -=1
        except:
            pass
    elif cmd[6] == 'F':
        A.pop()
        fp -=1
    elif cmd[6] == 'L':
        bp +=1
    n -=1
 
for e in A[bp:fp+1][::-1]:
    print int(e),

Solution (Python):
A=[]
fp =0
bp =0
n=int(raw_input())
while n: 
    cmd = raw_input()
    if cmd[0] == 'i':
        A.append(int(cmd[7:]))
        fp += 1
    elif cmd[6] == ' ':
        try:
            i = A[::-1].index(int(cmd[7:]))
            if i!=-1:
                del A[-i-1]
                fp -=1
        except:
            pass
    elif cmd[6] == 'F':
        A.pop()
        fp -=1
    elif cmd[6] == 'L':
        bp +=1
    n -=1
 
for k in A[bp:fp+1][::-1]:
    print int(k),

Solution (Python):
A=[]
fp =0
bp =0
n=int(raw_input())
while n: 
    cmd = raw_input()
    if cmd[0] == 'i':
        A.append(int(cmd[7:]))
        fp += 1
    elif cmd[6] == ' ':
        try:
            i = A[::-1].index(int(cmd[7:]))
            if i!=-1:
                del A[-i-1]
                fp -=1
        except:
            pass
    elif cmd[6] == 'F':
        A.pop()
        fp -=1
    elif cmd[6] == 'L':
        bp +=1
    n -=1
 
for k in A[bp:fp+1][::-1]:
    print int(k),

Solution (Python):
A=[]
fp =0
bp =0
n=int(raw_input())
while n: 
    cmd = raw_input()
    if cmd[0] == 'i':
        A.append(int(cmd[7:]))
        fp += 1
    elif cmd[6] == ' ':
        try:
            i = A[::-1].index(int(cmd[7:]))
            if i!=-1:
                del A[-i-1]
                fp -=1
        except:
            pass
    elif cmd[6] == 'F':
        A.pop()
        fp -=1
    elif cmd[6] == 'L':
        bp +=1
    n -=1
 
for k in A[bp:fp+1][::-1]:
    print int(k),

Solution (Python):
import sys

class DoublyLinkedList:
    def __init__(self, value = -1):
        self.value = value
        self.prev = self
        self.next = self

    def insert(self, value):
        newNode = DoublyLinkedList(value)
        newNode.next = self.next
        newNode.prev = self
        self.next.prev = newNode
        self.next = newNode

    def delete(self, value):
        node = self.next
        while node.value != -1:
            if node.value == value:
                node.next.prev = node.prev
                node.prev.next = node.next
                break
            node = node.next

    def deleteFirst(self):
        if self.next.value != -1:
            self.next = self.next.next
            self.next.prev = self

    def deleteLast(self):
        if self.prev.value != -1:
            self.prev = self.prev.prev
            self.prev.next = self

    def printAll(self):
        node = self.next
        while node.next.value != -1:
            print node.value,
            node = node.next
        print node.value

if __name__ == "__main__":
    dll = DoublyLinkedList()
    lines = sys.stdin.readlines()
    lines.pop(0)
    for line in lines:
        if line[0] == "i":
            dll.insert(int(line[7:]))
        elif line[6] == "F":
            dll.deleteFirst()
        elif line[6] == "L":
            dll.deleteLast()
        else:
            dll.delete(int(line[7:]))
    dll.printAll()

Solution (Python):
from collections import deque
def insert(x,a):
    a.appendleft(x)
def delete(x,a):
    if x in a:
        a.remove(x)
    else:
        return
def deletefirst(a):
    a.popleft()
def deletelast(a):
    a.pop()
n=input()
a=deque()
for i in xrange(n):
    s=raw_input().split()
    if s[0]=="insert":
        insert(s[1],a)
    elif s[0]=="delete":
        delete(s[1],a)
    elif s[0]=="deleteFirst":
        deletefirst(a)
    else:
        deletelast(a)
print(" ".join(a))

Solution (Python):
import sys
A=[]
fp =0
bp =0

n=int(raw_input())
while n:
    s = raw_input()

    if s[0] == 'i':
        A.append(int(s[7:]))
        fp += 1
    elif s[6] == ' ':
        try:
            i = A[::-1].index(int(s[7:]))
            if i!=-1:
                del A[-i-1]
                fp -=1
        except:
            pass
    elif s[6] == 'F':
        A.pop()
        fp -=1
    elif s[6] == 'L':
        bp +=1
    n -=1

for e in A[bp:fp+1][::-1]:
    print int(e),

Solution (Python):

A=[]
fp =0
bp =0

n=int(raw_input())
while n:
    s = raw_input()

    if s[0] == 'i':
        A.append(int(s[7:]))
        fp += 1
    elif s[6] == ' ':
        try:
            i = A[::-1].index(int(s[7:]))
            if i!=-1:
                del A[-i-1]
                fp -=1
        except:
            pass
    elif s[6] == 'F':
        A.pop()
        fp -=1
    elif s[6] == 'L':
        bp +=1
    n -=1

for e in A[bp:fp+1][::-1]:
    print int(e),

Solution (Python):


from collections import deque

n = int(raw_input())
que = deque()
for i in range(n):
    com = str(raw_input())
    if com == "deleteFirst":
        del que[0]
    elif com == "deleteLast":
        del que[-1]
    else:
        c, p = com.split()
        if c == "insert":
            que.appendleft(p)
        else:
            try: que.remove(p)
            except: pass
print " ".join(map(str, que))

Solution (Python):
n = input()
lis = []
bot = 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:
        bot += 1

print ' '.join( map(str, reversed(lis[bot:])) )

Solution (Python):
from collections import deque
n = int(input())
dl = deque()
for _ in range(n):
    cmd = input()
    if ' ' in cmd:
        op,v = cmd.split()
    else:
        op = cmd
    if   op == 'insert'     : dl.appendleft(v) 
    elif op == 'delete'     :
        try: dl.remove(v)
        except ValueError: pass
    elif op == 'deleteFirst': dl.popleft() 
    elif op == 'deleteLast' : dl.pop() 
print(*dl)

Solution (Python):
from collections import deque
n = int(input())
dl = deque()
for _ in range(n):
    op = input().split()
    if   op[0] == 'insert'     : dl.appendleft(op[1]) 
    elif op[0] == 'delete'     :
        if op[1] in dl: dl.remove(op[1])
    elif op[0] == 'deleteFirst': dl.popleft() 
    elif op[0] == 'deleteLast' : dl.pop() 
print(*dl)

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

sample_input = list(range(3))
sample_input[0] = '''7
insert 5
insert 2
insert 3
insert 1
delete 3
insert 6
delete 5'''
sample_input[1] = '''9
insert 5
insert 2
insert 3
insert 1
delete 3
insert 6
delete 5
deleteFirst
deleteLast'''
sample_input[2] = '''8
insert 1000000000
insert 999999999
deleteLast
insert 1234566890
insert 5
deleteFirst
insert 7
delete 5'''
give_sample_input = None
if give_sample_input is not None:
    sample_input_list = sample_input[give_sample_input].split('\n')
    def input():
        return sample_input_list.pop(0)
        

from collections import deque

num_of_command = int(input())
doubly_list = deque()
for n in range(num_of_command):
    str_command = input()
    if str_command == 'deleteFirst':
        doubly_list.popleft()
        continue
    elif str_command == 'deleteLast':
        doubly_list.pop()
        continue
    command_name = str_command.split()[0]
    command_obj = int(str_command.split()[1])
    if command_name == 'insert':
        doubly_list.appendleft(command_obj)
    elif command_name == 'delete':
        try:
            doubly_list.remove(command_obj)
        except ValueError:
            continue

output = ''
for item in doubly_list:
    output += str(item) + ' '
output = output.rstrip()
print(output)

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

for i in range(n):
    line = input().split()
    if len(line) == 2:
        command, ele = line
    else:
        command = line[0]

    if command == 'insert':
        d.appendleft(ele)
    elif command == 'delete':
        if ele in d:
            d.remove(ele)
    elif command == 'deleteFirst':
        d.popleft()
    elif command == 'deleteLast':
        d.pop()

print(' '.join(list(d)))

Solution (Python):



from collections import deque

def main():

    n = int(input())
    dll = deque()

    for _ in range(n):
        operation = input().split(' ')
        if operation[0] in ["insert", "delete"]:
            key = operation[1]
            if operation[0] == "insert":
                dll.appendleft(key)
            else:
                try:
                    dll.remove(key)
                except:
                    pass

        else:
            if operation[0][6] == 'F':
                dll.popleft()

            else:
                dll.pop()

    print(' '.join(dll))

if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque

n = int(input())
q = deque()

for i in range(n):
    command = input()
    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):
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):


import sys
from collections import deque

n = int(input())

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

a = deque()

for task in map(lambda x: [x] if x in task_name else x.split(), sys.stdin.readlines()):
    
    if len(task) == 1:
        if task[0] == "deleteFirst":
            a.popleft()
        else:
            a.pop()

    
    else:
        if task[0] == "insert":
            a.appendleft(task[1])
        elif task[1] in a:
            a.remove(task[1])

print(*a)

Solution (Python):
from collections import deque

q = deque()

n = int(input().strip())

for _ in range(n):
    cmd = input()
    if cmd == "deleteFirst":
        q.popleft()
    elif cmd == "deleteLast":
        q.pop()
    elif cmd.startswith("insert "):
        v = cmd[7:]
        q.appendleft(v)
    elif cmd.startswith("delete "):
        v = cmd[7:]
        try:
            q.remove(v)
        except ValueError:
            pass
print(" ".join(q))

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

ans = deque()

for i in range(n):
    c = sys.stdin.readline()[:-1]
    if c[0] == 'i':
        ans.appendleft(c[7:])
    elif c[6] == ' ':
        try:
            ans.remove(c[7:])
        except:
            pass
    elif c[6] == 'F':
        ans.popleft()
    else:
        ans.pop()

print(' '.join(ans))

Solution (Python):
from collections import deque
n = int(input())
dl = deque()
for _ in range(n):
    cmd = input()
    if ' ' in cmd:
        op,v = cmd.split()
    else:
        op = cmd
    if   op == 'insert'     : dl.appendleft(v) 
    elif op == 'delete'     :
        try: dl.remove(v)
        except ValueError: pass
    elif op == 'deleteFirst': dl.popleft() 
    elif op == 'deleteLast' : dl.pop() 
print(*dl)

Solution (Python):
from collections import deque
n = int(input())
li = deque()
for i in range(n):
    command = input().split()
    if command[0] == 'insert':
        li.appendleft(command[1])
    elif command[0] == 'delete':
        if command[1] in li:
            li.remove(command[1])
    elif command[0] == 'deleteFirst':
        li.popleft()
    else:
        li.pop()
print(*li)

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

Solution (Python):
from collections import deque

n = int(input())
d = deque()
cmd = ['com', 0]
for i in range(n):
    cmd = input().split()
    if cmd[0] == 'insert':
        d.appendleft(int(cmd[1]))
    elif cmd[0] == 'delete':
        try:
            d.remove(int(cmd[1]))
        except ValueError:
            pass
    elif cmd[0] == 'deleteFirst':
        d.popleft()
    elif cmd[0] == 'deleteLast':
        d.pop()
print(*list(d))

Solution (Python):


import sys
from collections import deque


class Solution:
    @staticmethod
    def doubly_linked_list():
        
        array_length = int(input())
        task_deque = deque()
        _input = sys.stdin.readlines()

        for i in range(array_length):
            l_arg = _input[i].split()
            action = _input[i].split()[0]
            if len(l_arg) > 1:
                value = _input[i].split()[-1]
            

            if action == 'insert':
                task_deque.appendleft(value)
            elif action == 'deleteFirst':
                task_deque.popleft()
            elif action == 'deleteLast':
                task_deque.pop()
            elif action == 'delete':
                try:
                    task_deque.remove(value)
                except Exception as e:
                    pass
        print(" ".join(task_deque))


if __name__ == '__main__':
    solution = Solution()
    solution.doubly_linked_list()

Solution (Python):


import sys
from collections import deque


class Solution:
    @staticmethod
    def doubly_linked_list():
        
        array_length = int(input())
        task_deque = deque()
        _input = sys.stdin.readlines()

        for i in range(array_length):
            l_arg = _input[i].split()
            action = _input[i].split()[0]
            if len(l_arg) > 1:
                value = _input[i].split()[-1]
            

            if action == 'insert':
                task_deque.appendleft(value)
            elif action == 'deleteFirst':
                task_deque.popleft()
            elif action == 'deleteLast':
                task_deque.pop()
            elif action == 'delete':
                try:
                    task_deque.remove(value)
                except Exception as e:
                    pass
        print(" ".join(task_deque))


if __name__ == '__main__':
    solution = Solution()
    solution.doubly_linked_list()

Solution (Python):


import sys
from collections import deque


class Solution:
    @staticmethod
    def doubly_linked_list():
        
        array_length = int(input())
        task_deque = deque()
        _input = sys.stdin.readlines()

        for i in range(array_length):
            l_arg = _input[i].split()
            action = l_arg[0]
            if len(l_arg) > 1:
                value = l_arg[-1]
            

            if action == 'insert':
                task_deque.appendleft(value)
            elif action == 'deleteFirst':
                task_deque.popleft()
            elif action == 'deleteLast':
                task_deque.pop()
            elif action == 'delete':
                try:
                    task_deque.remove(value)
                except Exception as e:
                    pass
        print(" ".join(task_deque))


if __name__ == '__main__':
    solution = Solution()
    solution.doubly_linked_list()

Solution (Python):


import sys
from collections import deque


class Solution:
    @staticmethod
    def doubly_linked_list():
        
        array_length = int(input())
        task_deque = deque()
        _input = sys.stdin.readlines()

        for i in range(array_length):
            l_arg = _input[i].split()
            action = l_arg[0]
            if len(l_arg) > 1:
                value = l_arg[-1]
            

            if action == 'insert':
                task_deque.appendleft(value)
            elif action == 'deleteFirst':
                task_deque.popleft()
            elif action == 'deleteLast':
                task_deque.pop()
            else :
                try:
                    task_deque.remove(value)
                except Exception as e:
                    pass
        print(" ".join(task_deque))


if __name__ == '__main__':
    solution = Solution()
    solution.doubly_linked_list()

Solution (Python):
import sys
from collections import deque

d = deque()
n = int(input())
lines = sys.stdin.readlines()
for i in range(n):
    ins = lines[i].split()
    command = ins[0]
    if command == "insert":
        d.appendleft(ins[1])
    elif command == "delete":
        try:
            d.remove(ins[1])
        except:
            pass
    elif command == "deleteFirst":
        d.popleft()
    elif command == "deleteLast":
        d.pop()

print(" ".join(d))

Solution (Python):


import sys
from collections import deque


class Solution:
    @staticmethod
    def doubly_linked_list():
        
        array_length = int(input())
        task_deque = deque()
        _input = sys.stdin.readlines()

        for i in range(array_length):
            l_arg = _input[i].split()
            action = l_arg[0]
            if len(l_arg) > 1:
                value = l_arg[-1]
            

            if action == 'insert':
                task_deque.appendleft(value)
            elif action == 'deleteFirst':
                task_deque.popleft()
            elif action == 'deleteLast':
                task_deque.pop()
            else:
                try:
                    task_deque.remove(value)
                except Exception as e:
                    pass
        print(" ".join(task_deque))


if __name__ == '__main__':
    solution = Solution()
    solution.doubly_linked_list()

Solution (Python):


from collections import deque

def parse_cmd(deque,input_cmd):
	if input_cmd[0] == 'insert':
		deque.appendleft(input_cmd[1])
	elif input_cmd[0] == 'delete':
		try:
			deque.remove(input_cmd[1])
		except:
			pass
	elif input_cmd[0] == 'deleteFirst':
		deque.popleft()
	elif input_cmd[0] == 'deleteLast':
		deque.pop()
		
def main():
	que = deque()
	n = int(raw_input())
	cmd = [raw_input().split() for _ in range(n)]
	
	for i in cmd:
		parse_cmd(que,i)
	
	print " ".join(que)




if __name__ == '__main__':
	main()

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

Solution (Python):
from collections import deque

def process_command(dll, commands):
    for cmd in commands:
        
        if cmd.startswith('insert') or cmd.startswith('delete '):
            t = cmd.split(' ')
            cmd = t[0]
            num_str = t[1]
        if cmd == 'insert':
            dll.appendleft(int(num_str))
        elif cmd == 'delete':
            if int(num_str) in dll:
                temp = []
                result = dll.popleft()
                while result != int(num_str):
                    temp.append(result)
                    result = dll.popleft()
                temp = temp[::-1]
                dll.extendleft(temp)
        elif cmd == 'deleteFirst':
            dll.popleft()
        elif cmd == 'deleteLast':
            dll.pop()
        
        


if __name__ == '__main__':
    
    commands = []
    num = int(input())
    for i in range(num):
        commands.append(input())

    
    dll = deque()
    process_command(dll, commands)

    
    
    print('{0}'.format(' '.join(map(str, dll))))

Solution (Python):
from collections import deque

def process_command(dll, commands):
    
    for cmd in commands:
        
        
        
        try:
            cmd, num_str = cmd.split()
        except ValueError:
            pass
        
        
        
        
        if cmd == 'insert':
            dll.appendleft(int(num_str))
        elif cmd == 'delete':
            
            if int(num_str) in dll:  
                temp = []
                result = dll.popleft()
                while result != int(num_str):
                    temp.append(result)
                    result = dll.popleft()
                temp = temp[::-1]
                dll.extendleft(temp)
        elif cmd == 'deleteFirst':
            dll.popleft()
        elif cmd == 'deleteLast':
            dll.pop()
        
        


if __name__ == '__main__':
    
    commands = []
    num = int(input())
    commands = [input() for i in range(num)]

    
    dll = deque()
    process_command(dll, commands)

    
    print('{0}'.format(' '.join(map(str, dll))))