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:
class Node:

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


class DoublyLikedList:

    def __init__(self):
        self.root = None
        self.last = None
        self.size = 0

    def insert(self, x):
        if self.root is None:
            self.root = Node(None, None, x)
            self.last = self.root
        else:
            tmp = self.root
            self.root = Node(None, tmp, x)
            tmp.prev = self.root
        self.size += 1

    def delete(self, x):
        cur = self.root
        while cur is not None:
            if cur.val == x:
                break
            cur = cur.next
        if cur is None:
            return
        if cur.prev is None:
            self.deleteFirst()
        elif cur.next is None:
            self.deleteLast()
        else:
            a = cur.prev
            b = cur.next
            a.next = b
            b.prev = a
            self.size -= 1

    def deleteFirst(self):
        if self.size == 1:
            self.root = None
            self.last = None
        else:
            a = self.root
            self.root = a.next
            self.root.prev = None
        self.size -= 1

    def deleteLast(self):
        if self.size == 1:
            self.root = None
            self.last = None
        else:
            a = self.last.prev
            a.next = None
            self.last = a
        self.size -= 1


def main():
    n = int(input())
    l = DoublyLikedList()
    for _ in range(n):
        x = input().split()
        if x[0] == 'insert':
            p = int(x[1])
            l.insert(p)
        elif x[0] == 'delete':
            p = int(x[1])
            l.delete(p)
        elif x[0] == 'deleteFirst':
            l.deleteFirst()
        else:
            l.deleteLast()
    cur = l.root
    while cur is not None:
        if cur.next is not None:
            print(str(cur.val)+" ", end="")
        else:
            print(cur.val)
        cur = cur.next


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
class Node:

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


class DoublyLikedList:

    def __init__(self):
        self.root = None
        self.last = None
        self.size = 0

    def insert(self, x):
        if self.root is None:
            self.root = Node(None, None, x)
            self.last = self.root
        else:
            tmp = self.root
            self.root = Node(None, tmp, x)
            tmp.prev = self.root
        self.size += 1

    def delete(self, x):
        cur = self.root
        while cur is not None:
            if cur.val == x:
                break
            cur = cur.next
        if cur is None:
            return
        if cur.prev is None:
            if self.size == 1:
                self.root = None
                self.last = None
            else:
                a = self.root
                self.root = a.next
                self.root.prev = None
            self.size -= 1
        elif cur.next is None:
            if self.size == 1:
                self.root = None
                self.last = None
            else:
                a = self.last.prev
                a.next = None
                self.last = a
            self.size -= 1
        else:
            a = cur.prev
            b = cur.next
            a.next = b
            b.prev = a
            self.size -= 1

    def deleteFirst(self):
        if self.size == 1:
            self.root = None
            self.last = None
        else:
            a = self.root
            self.root = a.next
            self.root.prev = None
        self.size -= 1

    def deleteLast(self):
        if self.size == 1:
            self.root = None
            self.last = None
        else:
            a = self.last.prev
            a.next = None
            self.last = a
        self.size -= 1


def main():
    n = int(input())
    l = DoublyLikedList()
    for _ in range(n):
        x = input().split()
        if x[0] == 'insert':
            p = int(x[1])
            l.insert(p)
        elif x[0] == 'delete':
            p = int(x[1])
            l.delete(p)
        elif x[0] == 'deleteFirst':
            l.deleteFirst()
        else:
            l.deleteLast()
    cur = l.root
    while cur is not None:
        if cur.next is not None:
            print(str(cur.val)+" ", end="")
        else:
            print(cur.val)
        cur = cur.next


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 17 17:31:28 2018
aoj alds 1-3C
@author: maezawa
"""
from collections import deque 

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


for i in range(n):
    s = input().split()
    com = s[0]
    if com == 'insert':
        d.appendleft(int(s[1]))
    elif com == 'delete':
        try:
            d.remove(int(s[1]))
        except:
            continue
    elif com == 'deleteFirst':
        d.popleft()
    elif com == 'deleteLast':
        d.pop()

print(*d)

Submission status: Time Limit Exceeded

Solution code:
import sys

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

class DoublyLinkedList:
	def __init__(self):
		self.head = None
		self.tail = None
		self.size = 0
	
	def get_size(self):
		return self.size
	
	def is_empty(self):
		return self.size == 0
	
	def appendToHead(self, value):
		node = Node(value)
		if self.head == None and self.tail == None:
			self.head = self.tail = node
		else:
			node.next = self.head
			self.head.prev = node
			self.head = node
		self.size += 1
	
	def removeFirst(self):
		if self.is_empty(): return
		if self.size == 1:
			self.head = self.tail = None
		else:
			next = self.head.next
			next.prev = None
			self.head = next
			if next.next == None:
				self.tail = next
		self.size -= 1		
		
	def removeLast(self):
		if self.is_empty(): return 
		if self.size == 1:
			self.head = self.tail = None
		else:
			prev = self.tail.prev
			prev.next = None
			self.tail = prev
			if prev.prev == None:
				self.head = prev
		self.size -= 1

	def remove(self, value):
		if self.is_empty(): return
		if self.head.value == value:
			self.removeFirst()
		else:
			node = self.head.next
			while node != None:
				if node.value == value:
					prev = node.prev
					next = node.next
					prev.next = next
					if next != None:
						next.prev = prev
					else:
						self.tail = prev
					self.size -= 1
					break
				else:
					node = node.next
		
	def print(self):
			node = self.head
			while node != None:
				sys.stdout.write(str(node.value))
				node = node.next
				if node != None:
					sys.stdout.write(' ')
			print()
	
n = int(input())
dll = DoublyLinkedList()
for i in range(n):
	cmd = str(input())
	if cmd == 'deleteFirst':
		dll.removeFirst()
	elif cmd == 'deleteLast':
		dll.removeLast()
	else:
		cmd, value = cmd.split()
		if cmd == 'insert':
			dll.appendToHead(int(value))
		elif cmd == 'delete':
			dll.remove(int(value))
		else:
			print('Unknown command:' + cmd)
	
dll.print()

Submission status: Time Limit Exceeded

Solution code:
import sys

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

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

	def get_size(self):
		return self.size

	def is_empty(self):
		return self.size == 0

	def insert(self, value):
		node = Node(value)
		if self.is_empty():
			self.head = self.tail = node
		else:
			self.head.prev = node
			node.next = self.head
			self.head = node
		self.size += 1

	def deleteFirst(self):
		if self.is_empty():
			return
		if self.size == 1:
			self.head = self.tail = None
		else:
			self.head = self.head.next
			self.head.prev = None
		self.size -= 1
	
	def deleteLast(self):
		if self.is_empty():
			return
		if self.size == 1:
			self.head = self.tail = None
		else:
			self.tail = self.tail.prev
			self.tail.next = None
		self.size -= 1

	def delete(self, value):
		if self.is_empty():
			return
		if self.head.value == value:
			self.deleteFirst()
		else:
			node1 = self.head.next
			node2 = self.tail
			node3 = None
			while node1 != None:
				if node1.value == value:
					prev = node1.prev
					next = node1.next
					prev.next = next
					if next != None:
						next.prev = prev
					else:
						self.tail = prev
					self.size -= 1
					break
				elif node1 == node2:
					if node3 == None:
						break;
					else:
						node1 = node3
				else:
					node1 = node1.next
					if node2.value == value:
						node3 = node2
					node2 = node2.prev
		return

	def print(self):
		node = self.head
		while node != None:
			sys.stdout.write(str(node.value))
			node = node.next
			if node != None:
				sys.stdout.write(' ')
		print()

n = int(input())
dll = DoublyLinkedList()
for i in range(n):
	cmd = str(input())
	if cmd == 'deleteFirst':
		dll.deleteFirst()
	elif cmd == 'deleteLast':
		dll.deleteLast()
	else:
		cmd, value = cmd.split()
		if cmd == 'insert':
			dll.insert(int(value))
		else:
			dll.delete(int(value))
dll.print()

Submission status: Time Limit Exceeded

Solution code:
import sys

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

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

	def get_size(self):
		return self.size

	def is_empty(self):
		return self.size == 0

	def insert(self, value):
		node = Node(value)
		if self.is_empty():
			self.head = self.tail = node
		else:
			self.head.prev = node
			node.next = self.head
			self.head = node
		self.size += 1

	def deleteFirst(self):
		if self.is_empty():
			return
		if self.size == 1:
			self.head = self.tail = None
		else:
			self.head = self.head.next
			self.head.prev = None
		self.size -= 1
	
	def deleteLast(self):
		if self.is_empty():
			return
		if self.size == 1:
			self.head = self.tail = None
		else:
			self.tail = self.tail.prev
			self.tail.next = None
		self.size -= 1

	def delete(self, value):
		if self.is_empty():
			return
		if self.head.value == value:
			self.deleteFirst()
		else:
			node1 = self.head.next
			node2 = self.tail
			node3 = None
			while node1 != None:
				if node1.value == value:
					prev = node1.prev
					next = node1.next
					prev.next = next
					if next != None:
						next.prev = prev
					else:
						self.tail = prev
					self.size -= 1
					break
				elif node1 == node2:
					if node3 == None:
						break;
					else:
						node1 = node3
				else:
					node1 = node1.next
					if node2.value == value:
						node3 = node2
					node2 = node2.prev
		return

	def print(self):
		node = self.head
		while node != None:
			sys.stdout.write(str(node.value))
			node = node.next
			if node != None:
				sys.stdout.write(' ')
		print()

n = int(input())
dll = DoublyLinkedList()
for n in range(n):
	cmd = sys.stdin.readline().strip().split()
	if cmd[0] == 'deleteFirst':
		dll.deleteFirst()
	elif cmd[0] == 'deleteLast':
		dll.deleteLast()
	else:
		if cmd[0] == 'insert':
			dll.insert(int(cmd[1]))
		else:
			dll.delete(int(cmd[1]))
dll.print()

Submission status: Time Limit Exceeded

Solution code:
import sys

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

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

	def get_size(self):
		return self.size

	def is_empty(self):
		return self.size == 0

	def insert(self, value):
		node = Node(value)
		if self.is_empty():
			self.head = self.tail = node
		else:
			self.head.prev = node
			node.next = self.head
			self.head = node
		self.size += 1

	def deleteFirst(self):
		if self.is_empty():
			return
		if self.size == 1:
			self.head = self.tail = None
		else:
			self.head = self.head.next
			self.head.prev = None
		self.size -= 1
	
	def deleteLast(self):
		if self.is_empty():
			return
		if self.size == 1:
			self.head = self.tail = None
		else:
			self.tail = self.tail.prev
			self.tail.next = None
		self.size -= 1

	def delete(self, value):
		if self.is_empty():
			return
		if self.head.value == value:
			self.deleteFirst()
		else:
			node1 = self.head.next
			node2 = self.tail
			node3 = None
			while node1 != None:
				if node1.value == value:
					prev = node1.prev
					next = node1.next
					prev.next = next
					if next != None:
						next.prev = prev
					else:
						self.tail = prev
					self.size -= 1
					break
				elif node1 == node2:
					if node3 == None:
						break;
					else:
						node1 = node3
				else:
					node1 = node1.next
					if node2.value == value:
						node3 = node2
					node2 = node2.prev
		return

	def print(self):
		node = self.head
		while node != None:
			sys.stdout.write(str(node.value))
			node = node.next
			if node != None:
				sys.stdout.write(' ')
		print()

n = int(input())
dll = DoublyLinkedList()
for _ in range(n):
	cmd = sys.stdin.readline().strip().split()
	if cmd[0] == 'deleteFirst':
		dll.deleteFirst()
	elif cmd[0] == 'deleteLast':
		dll.deleteLast()
	else:
		if cmd[0] == 'insert':
			dll.insert(int(cmd[1]))
		else:
			dll.delete(int(cmd[1]))
dll.print()

Submission status: Time Limit Exceeded

Solution code:
import sys

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

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

	def get_size(self):
		return self.size

	def is_empty(self):
		return self.size == 0

	def insert(self, value):
		node = Node(value)
		if self.is_empty():
			self.head = self.tail = node
		else:
			self.head.prev = node
			node.next = self.head
			self.head = node
		self.size += 1

	def deleteFirst(self):
		if self.is_empty():
			return
		if self.size == 1:
			self.head = self.tail = None
		else:
			self.head = self.head.next
			self.head.prev = None
		self.size -= 1
	
	def deleteLast(self):
		if self.is_empty():
			return
		if self.size == 1:
			self.head = self.tail = None
		else:
			self.tail = self.tail.prev
			self.tail.next = None
		self.size -= 1

	def delete(self, value):
		if self.is_empty():
			return
		if self.head.value == value:
			self.deleteFirst()
		else:
			node1 = self.head.next
			node2 = self.tail
			node3 = None
			while node1 != None:
				if node1.value == value:
					prev = node1.prev
					next = node1.next
					prev.next = next
					if next != None:
						next.prev = prev
					else:
						self.tail = prev
					self.size -= 1
					break
				elif node1 == node2:
					if node3 == None:
						break;
					else:
						node1 = node3
				else:
					node1 = node1.next
					if node2.value == value:
						node3 = node2
					node2 = node2.prev
		return

	def print(self):
		node = self.head
		while node != None:
			sys.stdout.write(str(node.value))
			node = node.next
			if node != None:
				sys.stdout.write(' ')
		print()

	def get_content(self):
		ret = []
		it = self.head

		while it is not None:
			ret.append(str(it.value))
			it = it.next

		return ' '.join(ret)

n = int(input())
dll = DoublyLinkedList()
for _ in range(n):
	cmd = sys.stdin.readline().strip().split()
	if cmd[0] == 'deleteFirst':
		dll.deleteFirst()
	elif cmd[0] == 'deleteLast':
		dll.deleteLast()
	else:
		if cmd[0] == 'insert':
			dll.insert(int(cmd[1]))
		else:
			dll.delete(int(cmd[1]))
print(dll.get_content())

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
lis = []
for n in range(N):
    com = input().split()
    if com[0] == "insert":
        lis.insert(0,int(com[1]))
    elif com[0] == "delete":
        try:
            lis.remove(int(com[1]))
        except:
            pass
    elif com[0] == "deleteFirst":
        del lis[0]
    else:
        lis.pop()
#    print(*lis)
print(*lis)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
class Node():
    key = 0
    def __init__( self,  key ):
        self.key = key
        self.next = -1
        self.pre = -1

    def insert( self, othernode ): # othernodeにはinsert前の先頭のノードを入れる
        self.next = othernode
        othernode.pre = self
        return self

    def delete( self, x ): #先頭ノードで実行
        FirstNode = self
        if self.key == x:
            if self.pre == -1:
                self.next.pre = -1
                FirstNode = self.next
            else :
                self.pre.next = self.next
                self.next.pre = self.pre
        else:
            if self.next != -1:
                self.next.delete( x )
        return FirstNode

    def printNodes( self ): #先頭ノードで実行
        if self.next.next != -1:
            print( str( self.key ), end=" " )
            self.next.printNodes()
        elif self.next != -1:
            print( str( self.key ), end="\n" )

    def deleteFirst( self ):
        self.next.pre = -1
        return self.next #新しい先頭ノードを返す

    def deleteLast( self ):
        if self.next != -1:
            self.next.deleteLast()
        else :
            self.pre.next = -1


def main():
    n = int( input() )
    commands = []
    vals = []
    for i in range(n):
        tmp = input().split()
        tmp.append( 0 )
        commands.append( tmp[0] )
        vals.append( int(tmp[1]) )

    FirstNode = Node( None )
    for i in range(n):
        if commands[i] == "insert":
            TmpNode = Node( vals[i] )
            FirstNode = TmpNode.insert( FirstNode )
        elif commands[i] == "delete":
            FirstNode = FirstNode.delete( vals[i] )
        elif commands[i] == "deleteFirst":
            FirstNode = FirstNode.deleteFirst()
        elif commands[i] == "deleteLast":
            FirstNode.deleteLast()
        else :
            print("command error!")

    FirstNode.printNodes()

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
class Node():
    key = 0
    def __init__( self,  key ):
        self.key = key
        self.next = -1
        self.pre = -1

    def insert( self, othernode ): # othernodeにはinsert前の先頭のノードを入れる
        self.next = othernode
        othernode.pre = self
        return self

    def delete( self, x ): #先頭ノードで実行
        FirstNode = self
        if self.key == x:
            if self.pre == -1:
                self.next.pre = -1
                FirstNode = self.next
            else :
                self.pre.next = self.next
                self.next.pre = self.pre
        else:
            if self.next != -1:
                self.next.delete( x )
        return FirstNode

    def printNodes( self ): #先頭ノードで実行
        if self.next.next != -1:
            print( str( self.key ), end=" " )
            self.next.printNodes()
        elif self.next != -1:
            print( str( self.key ), end="\n" )

    def deleteFirst( self ):
        self.next.pre = -1
        return self.next #新しい先頭ノードを返す

    def deleteLast( self ):
        if self.next != -1:
            self.next.deleteLast()
        else :
            self.pre.next = -1


def main():
    n = int( input() )
    FirstNode = Node( None )
    for i in range(n):
        tmp = input().split()

        if tmp[0] == "insert":
            TmpNode = Node( tmp[1] )
            FirstNode = TmpNode.insert( FirstNode )
        elif tmp[0] == "delete":
            FirstNode = FirstNode.delete( tmp[1] )
        elif tmp[0] == "deleteFirst":
            FirstNode = FirstNode.deleteFirst()
        elif tmp[0] == "deleteLast":
            FirstNode.deleteLast()
        else :
            print("command error!")

    FirstNode.printNodes()

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
xlist = []
for i in range(n):
    command = list(map(str,input().split()))
    if (command[0] == 'insert'):
        x = command[1]
        if (len(xlist) == 0):
            xlist.append(int(x))
        else:
            xlist.append(int(x))
            for i in range(1,len(xlist)):
                k = len(xlist) - i 
                j = k-1
                xlist[j], xlist[k] = xlist[k], xlist[j]
        #print(xlist)
    elif (command[0] == 'delete'):
        x = command[1]
        if (int(x) in xlist) :
            xlist.remove(int(x))
        #print(xlist)
    elif (command[0] == 'deleteFirst'):
        if (len(xlist) > 0):
            del xlist[0]
        #print(xlist)
    elif (command[0] == 'deleteLast'):
        if (len(xlist) > 0):
            del xlist[-1]
        #print(xlist)
n = len(xlist)
for i in range(n):
    if (i == n-1):
        print('{}'.format(xlist[i]), end = '')
    else:
        print('{} '.format(xlist[i]), end = '')
print()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
xlist = []
for i in range(n):
    command = list(map(str,input().split()))
    if (command[0] == 'insert'):
        x = command[1]
        if (len(xlist) == 0):
            xlist.append(int(x))
        else:
            xlist = [int(x)] + xlist
        #print(xlist)
    elif (command[0] == 'delete'):
        x = command[1]
        if (int(x) in xlist) :
            xlist.remove(int(x))
        #print(xlist)
    elif (command[0] == 'deleteFirst'):
        if (len(xlist) > 0):
            del xlist[0]
        #print(xlist)
    elif (command[0] == 'deleteLast'):
        if (len(xlist) > 0):
            del xlist[-1]
        #print(xlist)
n = len(xlist)
for i in range(n):
    if (i == n-1):
        print('{}'.format(xlist[i]), end = '')
    else:
        print('{} '.format(xlist[i]), end = '')
print()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
xlist = []
for i in range(n):
    command = list(map(str,input().split()))
    if (command[0] == 'insert'):
        x = command[1]
        if (len(xlist) == 0):
            xlist.append(int(x))
        else:
            xlist = [int(x)] + xlist
        #print(xlist)
    elif (command[0] == 'delete'):
        x = command[1]
        if (int(x) in xlist) :
            xlist.remove(int(x))
        #print(xlist)
    elif (command[0] == 'deleteFirst'):
        if (len(xlist) > 0):
            xlist.pop(0)
        #print(xlist)
    elif (command[0] == 'deleteLast'):
        if (len(xlist) > 0):
            xlist.pop(-1)
        #print(xlist)
n = len(xlist)
for i in range(n):
    if (i == n-1):
        print('{}'.format(xlist[i]), end = '')
    else:
        print('{} '.format(xlist[i]), end = '')
print()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
n = int(input())
xdeque = deque()
for i in range(n):
    command = list(map(str,input().split()))
    if (command[0] == 'insert'):
        x = command[1]
        xdeque.appendleft(int(x))
        #print(xlist)
    elif (command[0] == 'delete'):
        x = command[1]
        if (int(x) in xdeque) :
            xdeque.remove(int(x))
        #print(xlist)
    elif (command[0] == 'deleteFirst'):
        xdeque.popleft()
        #print(xlist)
    elif (command[0] == 'deleteLast'):
        xdeque.pop()
        #print(xlist)
print(*xdeque)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque   ##deque 라는 데이터형 편리한듯. print도그렇고 append,pop속도도 빠르고
n = int(input())
xdeque = deque()
for i in range(n):
    command = list(map(str,input().split()))
    if (command[0] == 'insert'):
        x = command[1]
        xdeque.appendleft(int(x))
    elif (command[0] == 'delete'):
        x = command[1]
        try:
            xdeque.remove(int(x))
        except ValueError:
            pass
    elif (command[0] == 'deleteFirst'):
        xdeque.popleft()
    else:
        xdeque.pop()
print(*xdeque)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque   ##deque 라는 데이터형 편리한듯. print도그렇고 append,pop속도도 빠르고
n = int(input())
xdeque = deque()
for i in range(n):
    command = input().split()
    if (command[0][0] == 'i'):
        x = command[1]
        xdeque.appendleft(int(x))
    elif (command[0] == 'delete'):
        x = command[1]
        try:
            xdeque.remove(int(x))
        except ValueError:
            pass
    elif (command[0][6] == 'F'):
        xdeque.popleft()
    else:
        xdeque.pop()
print(*xdeque)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

n = int(input())
key = []

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

    if command[0] == 'insert':
        key.insert(0, command[1])
    elif command[0] == 'delete' and command[1] in key:
        key.remove(command[1])
    elif command[0] == 'deleteFirst':
        del key[0]
    elif command[0] == 'deleteLast':
        del key[-1]

for i in range(len(key)):
    if i == len(key) - 1:
        print('{0}'.format(key[i]))
    else:
        print('{0}'.format(key[i]), end=' ')

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
from collections import deque

n = int(input())
key = deque()

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

    if command[0] == 'insert':
        key.insert(0, command[1])
    elif command[0] == 'delete' and command[1] in key:
        key.remove(command[1])
    elif command[0] == 'deleteFirst':
        del key[0]
    elif command[0] == 'deleteLast':
        del key[-1]

for i in range(len(key)):
    if i == len(key) - 1:
        print('{0}'.format(key[i]))
    else:
        print('{0}'.format(key[i]), end=' ')

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
from collections import deque

n = int(input())
key = deque()

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

    if command[0] == 'insert':
        key.insert(0, command[1])
    elif command[0] == 'delete' and command[1] in key:
        key.remove(command[1])
    elif command[0] == 'deleteFirst':
        key.popleft()
    elif command[0] == 'deleteLast':
        key.pop()

for i in range(len(key)):
    if i == len(key) - 1:
        print('{0}'.format(key[i]))
    else:
        print('{0}'.format(key[i]), end=' ')

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
from collections import deque

n = int(input())
key = deque()

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

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

for i in range(len(key)):
    if i == len(key) - 1:
        print('{0}'.format(key[i]))
    else:
        print('{0}'.format(key[i]), end=' ')

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
from collections import deque

n = int(input())
key = deque()

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

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

print(*key)

Submission status: Time Limit Exceeded

Solution code:
l = []
n = int(input())
for _ in range (n):
    cmd = input().split(' ')
    if cmd[0]=='insert':
        l.insert(0,int(cmd[1]))
    if cmd[0]=='delete':
        if int(cmd[1]) in l:
            l.remove(int(cmd[1]))
    if cmd[0]=='deleteFirst':
        l.pop(0)
    if cmd[0]=='deleteLast':
        l.pop(len(l)-1)
print(*l)

Submission status: Time Limit Exceeded

Solution code:
class LinkedList(object):
    
    def __init__(self):
        self.nodes = []

    def insert(self, node):
        self.nodes.insert(0, node)

    def delete(self, node):
        if node in self.nodes:
            self.nodes.remove(node)

    def deleteFirst(self):
        self.nodes.pop(0)

    def deleteLast(self):
        self.nodes.pop()

def run():
    n = int(input())
    linked_list = LinkedList()
    for _ in range(n):
        order = [x.strip() for x in input().split()]
        if order[0] == 'insert':
            linked_list.insert(int(order[1]))
        elif order[0] == 'delete':
            linked_list.delete(int(order[1]))
        elif order[0] == 'deleteFirst':
            linked_list.deleteFirst()
        elif order[0] == 'deleteLast':
            linked_list.deleteLast()
    print(' '.join([str(n) for n in linked_list.nodes]))
    
if __name__ == '__main__':
    run()