Problem:


Doubly Linked List

Your task is to implement a double linked list.

Write a program which performs the following operations:

insert x: insert an element with key x into the front of the list.

delete x: delete the first element which has the key of x from the list. If there is not such element, you need not do anything.

deleteFirst: delete the first element from the list. 

deleteLast: delete the last element from the list. 

Input

The input is given in the following format:

n

command1 

command2 

...

commandn 

In the first line, the number of operations n is given. In the following n lines, the above mentioned operations are given in the following format:

insert x

delete x

deleteFirst 

deleteLast 

Output

Print all the element (key) in the list after the given operations. Two consequtive keys should be separated by a single space.

Constraints

The number of operations ≤ 2,000,000

The number of delete operations ≤ 20

0 ≤ value of a key ≤ 109

The number of elements in the list does not exceed 106

For a delete, deleteFirst or deleteLast operation, there is at least one element in the list.

Sample Input 1

7
insert 5
insert 2
insert 3
insert 1
delete 3
insert 6
delete 5

Sample Output 1

6 1 2

Sample Input 2

9
insert 5
insert 2
insert 3
insert 1
delete 3
insert 6
delete 5
deleteFirst
deleteLast

Sample Output 2

1

Template in C
-->


Sample input:
7
insert 5
insert 2
insert 3
insert 1
delete 3
insert 6
delete 5


Sample output:
6 1 2

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    doubly_linked_list=[]
    for i in range(n):
        # organize an inputted command
        input_command = input()
        if ' ' in input_command:
            command, key = input_command.split()
        else:
            command, key = input_command, None

        # execute a command
        if command == 'insert':
            doubly_linked_list.insert(0, key)
        elif command == 'delete':
            if key in doubly_linked_list:
                doubly_linked_list.remove(key)
        elif command == 'deleteFirst':
            doubly_linked_list.pop(0)
        elif command == 'deleteLast':
            doubly_linked_list.pop()
        else:
            raise AssertionError
        #print(doubly_linked_list)

    print(' '.join(doubly_linked_list))

    return


main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
num_list = []

for _ in range(n):
    cmd = (input().split())
    if   cmd[0] == 'insert':
        num_list.insert(0,int(cmd[1]))
    elif cmd[0] == 'delete':
        if int(cmd[1]) in num_list:
            idx = num_list.index(int(cmd[1]))
            del(num_list[idx])
    elif cmd[0] == 'deleteFirst':
        del(num_list[0])
    elif cmd[0] == 'deleteLast':
        num_list.pop()

ans_list = [str(i) for i in num_list]
print(' '.join(ans_list))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
num_list = []

for _ in range(n):
    cmd = (input().split())
    if   cmd[0] == 'insert':
        num_list.insert(0,cmd[1])
    elif cmd[0] == 'delete':
        if cmd[1] in num_list:
            idx = num_list.index(cmd[1])
            del(num_list[idx])
    elif cmd[0] == 'deleteFirst':
        del(num_list[0])
    elif cmd[0] == 'deleteLast':
        num_list.pop()

print(' '.join(num_list))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
num_list = []

for _ in range(n):
    cmd = (input().split())
    if   cmd[0] == 'insert':
        num_list.insert(0,cmd[1])
    elif cmd[0] == 'delete':
        if cmd[1] in num_list:
            num_list.remove(cmd[1])
    elif cmd[0] == 'deleteFirst':
        del(num_list[0])
    else:
        num_list.pop()

print(' '.join(num_list))

Submission status: Time Limit Exceeded

Solution code:
import sys
import time
class NodeList:

    class Node:
        def __init__(self, left, x, right):
            self.key = x
            self.right = right
            self.left = left
        
        def __repr__(self):
            return str(self.key) 
        
        def printDetail(self):
            print(str(self.left) + ' ' + str(self) + ' ' + str(self.right))

    def __init__(self):
        self.head = NodeList.Node(None,None,None)
        self.head.right = self.head
        self.head.left = self.head
    
    def insert(self, x):
        newNode = NodeList.Node(None, x, None)
        newNode.right = self.head.right
        newNode.right.left = newNode
        self.head.right = newNode
        newNode.left = self.head


    def print(self):
        point = self.head.right 
        while point != self.head:
            sys.stdout.write(str(point))
            if point.right:
                point = point.right
            if point != self.head:
                sys.stdout.write(' ')
        sys.stdout.write('\n')

    def printAll(self):
        print(self.head)
        point = self.head.right
        while point != self.head:
            point.printDetail()
            point = point.right
            
        sys.stdout.write('\n')

    def delete(self,point):
        point.right.left = point.left
        point.left.right = point.right

    def searchDelete(self, x):
        point = self.head.right
        while point != self.head:
            if point.key == x:
                break
            point = point.right
        self.delete(point)

    def deleteFirst(self):
        self.delete(self.head.right)
    
    def deleteLast(self):
        point = self.head
        while point.right != self.head:
            point = point.right
        #print(point.key)
        #time.sleep(3)
        self.delete(point)



node = NodeList()
n = int(input())
for i in range(0,n,1):
    #node.printAll()
    #print()
    query = input().split()
    if query[0] == 'insert':
        node.insert(int(query[1]))
    elif query[0] == 'delete':
        node.searchDelete(int(query[1]))
    elif query[0] == 'deleteFirst':
        node.deleteFirst()
    elif query[0] == 'deleteLast':
        node.deleteLast()

node.print()

Submission status: Time Limit Exceeded

Solution code:
import sys
import time
class NodeList:

    class Node:
        def __init__(self, left, x, right):
            self.key = x
            self.right = right
            self.left = left
        
        def __repr__(self):
            return str(self.key) 
        
        def printDetail(self):
            print(str(self.left) + ' ' + str(self) + ' ' + str(self.right))

    def __init__(self):
        self.head = NodeList.Node(None,None,None)
        self.head.right = self.head
        self.head.left = self.head
    
    def insert(self, x):
        newNode = NodeList.Node(None, x, None)
        newNode.right = self.head.right
        newNode.right.left = newNode
        self.head.right = newNode
        newNode.left = self.head


    def print(self):
        point = self.head.right 
        start = self.head.right
        flag = False
        while point != self.head:
            sys.stdout.write(str(point))
            if point.right:
                point = point.right
            if flag and point == start:
                break
            if point != self.head:
                sys.stdout.write(' ')
            flag = True
        sys.stdout.write('\n')

    def printAll(self):
        print(self.head)
        point = self.head.right
        while point != self.head:
            point.printDetail()
            point = point.right
            
        sys.stdout.write('\n')

    def delete(self,point):
        point.right.left = point.left
        point.left.right = point.right

    def searchDelete(self, x):
        point = self.head.right
        while point != self.head:
            if point.key == x:
                break
            point = point.right
        self.delete(point)

    def deleteFirst(self):
        self.delete(self.head.right)
    
    def deleteLast(self):
        point = self.head
        while point.right != self.head:
            point = point.right
        #print(point.key)
        #time.sleep(3)
        self.delete(point)

from sys import stdin
node = NodeList()
n = int(input())
for i in range(n):
    #print(i)
    query = stdin.readline().strip().split()
    if query[0] == 'insert':
        node.insert(int(query[1]))
    elif query[0] == 'delete':
        node.searchDelete(int(query[1]))
    elif query[0] == 'deleteFirst':
        node.deleteFirst()
    elif query[0] == 'deleteLast':
        node.deleteLast()

node.print()

Submission status: Time Limit Exceeded

Solution code:
import sys
import time
class NodeList:

    class Node:
        def __init__(self, left, x, right):
            self.key = x
            self.right = right
            self.left = left
        
        def __repr__(self):
            return str(self.key) 
        
        def printDetail(self):
            print(str(self.left) + ' ' + str(self) + ' ' + str(self.right))

    def __init__(self):
        self.head = NodeList.Node(None,None,None)
        self.head.right = self.head
        self.head.left = self.head
    
    def insert(self, x):
        newNode = NodeList.Node(None, x, None)
        newNode.right = self.head.right
        newNode.right.left = newNode
        self.head.right = newNode
        newNode.left = self.head


    def print(self):
        point = self.head.right 
        start = self.head.right
        flag = False
        while point != self.head:
            sys.stdout.write(str(point))
            if point.right:
                point = point.right
            if flag and point == start:
                break
            if point != self.head:
                sys.stdout.write(' ')
            flag = True
        sys.stdout.write('\n')

    def printAll(self):
        print(self.head)
        point = self.head.right
        while point != self.head:
            point.printDetail()
            point = point.right
            
        sys.stdout.write('\n')

    def delete(self,point):
        point.right.left = point.left
        point.left.right = point.right

    def searchDelete(self, x):
        point = self.head.right
        while point != self.head:
            if point.key == x:
                break
            point = point.right
        self.delete(point)

    def deleteFirst(self):
        self.delete(self.head.right)
    
    def deleteLast(self):
        point = self.head
        while point.right != self.head:
            point = point.right
        #print(point.key)
        #time.sleep(3)
        self.delete(point)

from sys import stdin
node = NodeList()
n = int(input())
for i in range(n):
    #print(i)
    query = stdin.readline().split()
    if query[0] == 'insert':
        node.insert(int(query[1]))
    elif query[0] == 'delete':
        node.searchDelete(int(query[1]))
    elif query[0] == 'deleteFirst':
        node.deleteFirst()
    elif query[0] == 'deleteLast':
        node.deleteLast()

node.print()

Submission status: Time Limit Exceeded

Solution code:
import sys
import time
class NodeList:

    class Node:
        def __init__(self, left, x, right):
            self.key = x
            self.right = right
            self.left = left
        
        def __repr__(self):
            return str(self.key) 
        
        def printDetail(self):
            print(str(self.left) + ' ' + str(self) + ' ' + str(self.right))

    def __init__(self):
        self.head = NodeList.Node(None,None,None)
        self.head.right = self.head
        self.head.left = self.head
    
    def insert(self, x):
        newNode = NodeList.Node(None, x, None)
        newNode.right = self.head.right
        newNode.right.left = newNode
        self.head.right = newNode
        newNode.left = self.head


    def print(self):
        point = self.head.right 
        start = self.head.right
        flag = False
        while point != self.head:
            sys.stdout.write(str(point))
            if point.right:
                point = point.right
            if flag and point == start:
                break
            if point != self.head:
                sys.stdout.write(' ')
            flag = True
        sys.stdout.write('\n')

    def printAll(self):
        print(self.head)
        point = self.head.right
        while point != self.head:
            point.printDetail()
            point = point.right
            
        sys.stdout.write('\n')

    def delete(self,point):
        point.right.left = point.left
        point.left.right = point.right

    def searchDelete(self, x):
        point = self.head.right
        while point != self.head:
            if point.key == x:
                break
            point = point.right
        self.delete(point)

    def deleteFirst(self):
        self.delete(self.head.right)
    
    def deleteLast(self):
        point = self.head
        while point.right != self.head:
            point = point.right
        #print(point.key)
        #time.sleep(3)
        self.delete(point)

from sys import stdin
node = NodeList()
n = int(input())
for i in range(n):
    #print(i)
    query = stdin.readline().split()
    if query[0] == 'insert':
        node.insert(int(query[1]))
    elif query[0] == 'delete':
        node.searchDelete(int(query[1]))
    elif query[0] == 'deleteFirst':
        node.deleteFirst()
    elif query[0] == 'deleteLast':
        node.deleteLast()

node.print()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = [list(map(str,input().split())) for _ in range(n)]
B = []

for i in range(n):
    if A[i][0] == 'insert':
        B.insert(0,int(A[i][1]))
    elif A[i][0] == 'delete':
        if int(A[i][1]) in B:
            B.remove(int(A[i][1]))
    elif A[i][0] == 'deleteFirst':
        del B[0]
    elif A[i][0] == 'deleteLast':
        del B[-1]
  

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
B = []

for i in range(n):
    A = list(map(str,input().split()))
    if A[0] == 'insert':
        B.insert(0,int(A[1]))
    elif A[0] == 'delete':
        if int(A[1]) in B:
            B.remove(int(A[1]))
    elif A[0] == 'deleteFirst':
        del B[0]
    elif A[0] == 'deleteLast':
        del B[-1]
  

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

Submission status: Time Limit Exceeded

Solution code:
import sys
import time
class NodeList:

    class Node:
        def __init__(self, left, x, right):
            self.key = x
            self.right = right
            self.left = left
        
        def __repr__(self):
            return str(self.key) 
        
        def printDetail(self):
            print(str(self.left) + ' ' + str(self) + ' ' + str(self.right))

    def __init__(self):
        self.head = NodeList.Node(None,None,None)
        self.head.right = self.head
        self.head.left = self.head
    
    def insert(self, x):
        newNode = NodeList.Node(None, x, None)
        newNode.right = self.head.right
        newNode.right.left = newNode
        self.head.right = newNode
        newNode.left = self.head


    def print(self):
        point = self.head.right 
        start = self.head.right
        flag = False
        while point != self.head:
            sys.stdout.write(str(point))
            if point.right:
                point = point.right
            if flag and point == start:
                break
            if point != self.head:
                sys.stdout.write(' ')
            flag = True
        sys.stdout.write('\n')

    def printAll(self):
        print(self.head)
        point = self.head.right
        while point != self.head:
            point.printDetail()
            point = point.right
            
        sys.stdout.write('\n')

    def delete(self,point):
        point.right.left = point.left
        point.left.right = point.right

    def searchDelete(self, x):
        point = self.head.right
        while point != self.head:
            if point.key == x:
                break
            point = point.right
        self.delete(point)

    def deleteFirst(self):
        self.delete(self.head.right)
    
    def deleteLast(self):
        # point = self.head
        # while point.right != self.head:
        #     point = point.right
        # #print(point.key)
        # #time.sleep(3)
        # self.delete(point)
        self.delete(self.head.left)

from sys import stdin
node = NodeList()
n = int(input())
for i in range(n):
    #print(i)
    query = stdin.readline().split()
    if query[0] == 'insert':
        node.insert(int(query[1]))
    elif query[0] == 'delete':
        node.searchDelete(int(query[1]))
    elif query[0] == 'deleteFirst':
        node.deleteFirst()
    elif query[0] == 'deleteLast':
        node.deleteLast()

node.print()

Submission status: Time Limit Exceeded

Solution code:
import sys
import time
class NodeList:

    class Node:
        def __init__(self, left, x, right):
            self.key = x
            self.right = right
            self.left = left
        
        def __repr__(self):
            return str(self.key) 
        
        def printDetail(self):
            print(str(self.left) + ' ' + str(self) + ' ' + str(self.right))

    def __init__(self):
        self.head = NodeList.Node(None,None,None)
        self.head.right = self.head
        self.head.left = self.head
    
    def insert(self, x):
        newNode = NodeList.Node(None, x, None)
        newNode.right = self.head.right
        newNode.right.left = newNode
        self.head.right = newNode
        newNode.left = self.head


    def print(self):
        point = self.head.right 
        start = self.head.right
        flag = False
        while point != self.head:
            sys.stdout.write(str(point))
            if point.right:
                point = point.right
            if flag and point == start:
                break
            if point != self.head:
                sys.stdout.write(' ')
            flag = True
        sys.stdout.write('\n')

    def printAll(self):
        print(self.head)
        point = self.head.right
        while point != self.head:
            point.printDetail()
            point = point.right
            
        sys.stdout.write('\n')

    def delete(self,point):
        point.right.left = point.left
        point.left.right = point.right

    def searchDelete(self, x):
        point = self.head.right
        while point != self.head:
            if point.key == x:
                break
            point = point.right
        self.delete(point)

    def deleteFirst(self):
        self.delete(self.head.right)
    
    def deleteLast(self):
        self.delete(self.head.left)

from sys import stdin
node = NodeList()
n = int(sys.stdin.readline())
for i in range(n):
    #print(i)
    query = stdin.readline().split()
    if query[0] == 'insert':
        node.insert(int(query[1]))
    elif query[0] == 'delete':
        node.searchDelete(int(query[1]))
    elif query[0] == 'deleteFirst':
        node.deleteFirst()
    elif query[0] == 'deleteLast':
        node.deleteLast()

node.print()

Submission status: Time Limit Exceeded

Solution code:
class Node:
	def __init__(self, key):
		self._key = key
		self._prev = None 
		self._next = None

def list_search(head, key):
	cur = head._next
	while cur._key != None and cur._key != key:
		cur = cur._next 
	return cur 

def initiation():
	head = Node(None)
	head._prev = head 
	head._next = head
	return head  

def delete_node(node):
	if node._key == None:
		return 
	node._prev._next = node._next 
	node._next._prev = node._prev 
	node._prev = node._next = node._key = None

def deleteFirst(head):
	delete_node(head._next)

def deleteLast(head):
	delete_node(head._prev)

def delete(head, key):
	delete_node(list_search(head, key))

def insert(head, x):
	node = Node(x)
	node._next = head._next 
	head._next._prev = node 
	node._prev = head 
	head._next = node 



if __name__ == "__main__":
	num_command = int(input())
	head = initiation()
	for i in range(num_command):
		command = input().split()
		order = command[0]
		if order == 'insert':
			value = command[1]
			insert(head, value)
		elif order == 'delete':
			value = command[1]
			delete(head, value)
		elif order == 'deleteFirst':
			deleteFirst(head)
		else:
			deleteLast(head)
	cur = head._next 
	while cur._next._key != None:
		print(str(cur._key), end = ' ')
		cur = cur._next
	print(str(cur._key))

Submission status: Time Limit Exceeded

Solution code:
class Node:
	def __init__(self, key):
		self._key = key
		self._prev = None 
		self._next = None

def list_search(head, key):
	cur = head._next
	while cur._key != None and cur._key != key:
		cur = cur._next 
	return cur 

def initiation():
	head = Node(None)
	head._prev = head 
	head._next = head
	return head  

def delete_node(node):
	if node._key == None:
		return 
	node._prev._next = node._next 
	node._next._prev = node._prev 
	node._prev = node._next = node._key = None

def deleteFirst(head):
	delete_node(head._next)

def deleteLast(head):
	delete_node(head._prev)

def delete(head, key):
	delete_node(list_search(head, key))

def insert(head, x):
	node = Node(x)
	node._next = head._next 
	head._next._prev = node 
	node._prev = head 
	head._next = node 



if __name__ == "__main__":
	num_command = int(input())
	head = initiation()
	for i in range(num_command):
		command = input().split()
		order = command[0]
		if order == 'insert':
			value = command[1]
			insert(head, value)
		elif order == 'delete':
			value = command[1]
			delete(head, value)
		elif order == 'deleteFirst':
			deleteFirst(head)
		else:
			deleteLast(head)
	cur = head._next 
	while (cur._next)._key != None:
		print(str(cur._key), end = ' ')
		cur = cur._next
	print(str(cur._key))

Submission status: Time Limit Exceeded

Solution code:
class Node:
	def __init__(self, key):
		self._key = key
		self._prev = None 
		self._next = None

def list_search(head, key):
	cur = head._next
	while cur._key != None and cur._key != key:
		cur = cur._next 
	return cur 

def initiation():
	head = Node(None)
	head._prev = head 
	head._next = head
	return head  

def delete_node(node):
	if node._key == None:
		return 
	node._prev._next = node._next 
	node._next._prev = node._prev 
	node._prev = node._next = node._key = None

def deleteFirst(head):
	delete_node(head._next)

def deleteLast(head):
	delete_node(head._prev)

def delete(head, key):
	delete_node(list_search(head, key))

def insert(head, x):
	node = Node(x)
	node._next = head._next 
	head._next._prev = node 
	node._prev = head 
	head._next = node 



if __name__ == "__main__":
	num_command = int(input())
	head = initiation()
	for i in range(num_command):
		command = input().split()
		order = command[0]
		if order[0] == 'i':
			value = command[1]
			insert(head, value)
		else:
			if len(order) > 6:
				if order[6] == 'F':
					deleteFirst(head)
				else:
					deleteLast(head)
			else:
				value = command[1]
				delete(head, value)
	cur = head._next 
	while (cur._next)._key != None:
		print(str(cur._key), end = ' ')
		cur = cur._next
	print(str(cur._key))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

n = int(input())
d = deque()
for _i in range(n):
    line = input().split()
    order = line[0]
    if order in ('insert', 'delete'):
        key = int(line[1])
    if order == 'insert':
        d.appendleft(key)
    elif order == 'delete':
        try:
            d.remove(key)
        except ValueError:
            pass
    elif order == 'deleteFirst':
        d.popleft()
    elif order == 'deleteLast':
        d.pop()
    else:
        raise ValueError('Invalid order: {order}')

i = 0
while True:
    try:
        if i == 0:
            print(d.popleft(), end='')
        else:
            print(f' {d.popleft()}', end='')
        i += 1
    except IndexError:
        break
print()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
cmds = [input().split(' ') for _ in range(n)]

dl_list = []
for cmd in cmds:
    try:
        if cmd[0] == 'insert':
            dl_list.insert(0, int(cmd[1]))
        elif cmd[0] == 'delete':
            dl_list.remove(int(cmd[1]))
        elif cmd[0] == 'deleteFirst':
            del dl_list[0]
        elif cmd[0] == 'deleteLast':
            del dl_list[-1]
    except ValueError:
        continue

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
cmds = [input().split(' ') for _ in range(n)]

dl_list = []
insert = dl_list.insert
remove = dl_list.remove
for cmd in cmds:
    try:
        if cmd[0] == 'insert':
            insert(0, int(cmd[1]))
        elif cmd[0] == 'delete':
            remove(int(cmd[1]))
        elif cmd[0] == 'deleteFirst':
            del dl_list[0]
        elif cmd[0] == 'deleteLast':
            del dl_list[-1]
    except ValueError:
        continue
print(' '.join(map(str, dl_list)))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
cmds = [input().split(' ') for _ in range(n)]

dl_list = []
insert = dl_list.insert
remove = dl_list.remove
for cmd in cmds:
    if cmd[0] == 'insert':
        insert(0, int(cmd[1]))
    elif cmd[0] == 'delete':
        if int(cmd[1]) in dl_list:
            remove(int(cmd[1]))
    elif cmd[0] == 'deleteFirst':
        del dl_list[0]
    elif cmd[0] == 'deleteLast':
        del dl_list[-1]
print(' '.join(map(str, dl_list)))

Submission status: Time Limit Exceeded

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

n = int(input())
cmds = [input().rstrip('\r\n').split(' ') for _ in range(n)]

dl_list = []
insert = dl_list.insert
remove = dl_list.remove
for cmd in cmds:
    if cmd[0] == 'insert':
        insert(0, int(cmd[1]))
    elif cmd[0] == 'delete':
        if int(cmd[1]) in dl_list:
            remove(int(cmd[1]))
    elif cmd[0] == 'deleteFirst':
        del dl_list[0]
    elif cmd[0] == 'deleteLast':
        del dl_list[-1]
print(' '.join(map(str, dl_list)))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = []
for i in range(n):
    s = input()
    # print(s)
    if s == "deleteFirst":
        l.pop()
    elif s == "deleteLast":
        l.pop(0)
    else:
        c, d = s.split()
        if c == 'insert':
            l.append(d)
        elif c == "delete":
            if d in l:
                l = l[::-1]
                l.remove(d)
                l = l[::-1]
    # print(l)

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

Submission status: Time Limit Exceeded

Solution code:
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_3_C&lang=jp
class LinkedList():
    def __init__(self):
        self.head = None
    
    def insert(self, x):
        if self.head is None:
            self.head = LinkedListElement(x)
            self.head.next = self.head
            self.head.prev = self.head
            return
        newelm = LinkedListElement(x)
        former_tail = self.head.prev
        former_head = self.head

        former_tail.next = newelm
        former_head.prev = newelm
        
        newelm.next = former_head
        newelm.prev = former_tail
        self.head = newelm

    def deleteLast(self):
        if self.head is None:
            return
        if self.head is self.head.next:
            self.head = None
            return
        
        former_last = self.head.prev
        former_second_last = self.head.prev.prev
        self.head.prev = former_second_last
        former_second_last.next = self.head
    
    def deleteFirst(self):
        if self.head is None:
            return
        if self.head is self.head.next:
            self.head = None
            return

        tail = self.head.prev
        former_second = self.head.next
        tail.next = former_second
        former_second.prev = tail
        self.head = former_second
    
    def delete(self, x):
        if self.head is None:
            return
        if self.head.value == x:
            self.deleteFirst()
            return
        if self.head is self.head.next:
            return
        
        ptr = self.head
        while True:
            if ptr.value == x:
                ptr_prev = ptr.prev
                ptr_next = ptr.next
                ptr_prev.next = ptr_next
                ptr_next.prev = ptr_prev
                break
            ptr = ptr.next
            if ptr is self.head:
                break
    def puts(self):
        if self.head is None:
            print("")
            return
        ptr = self.head
        while True:
            print(ptr.value, end="")
            ptr = ptr.next
            if ptr is self.head:
                print("")
                break
            else:
                print(" ", end="")



class LinkedListElement():

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


if __name__ == "__main__":
    n = int(input())
    li = LinkedList()
    for i in range(n):
        cmd = input().split()
        if cmd[0] == "insert":
            li.insert(cmd[1])
        elif cmd[0] == "delete":
            li.delete(cmd[1])
        elif cmd[0] == "deleteFirst":
            li.deleteFirst()
        elif cmd[0] == "deleteLast":
            li.deleteLast()
    
    li.puts()