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

Solution in JAVA (judge verdict: Time Limit Exceeded)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public Main() {
    }

    public static void main(String[] args ) {
    		Main main = new Main();
    		main.exec();
    }

    public void exec() {
        BufferedReader bf = null;
        try{
            bf = new BufferedReader(new InputStreamReader(System.in));
            exec_body(bf);
        } catch (IOException e) {
			e.printStackTrace();
		}
        finally{
            if( bf != null ) {
                try {
					bf.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
            }
        }
    }

    public static boolean isEmpty( String str ) {
    		return str == null || str.length() == 0;
    }

    public void exec_body( BufferedReader bf ) throws IOException {
    		String line = bf.readLine();
    		IchiLinkedList<Integer> list = new IchiLinkedList<>();
        while( !isEmpty((line = bf.readLine())) ) {
        		String[] lines = line.split(" ");
        		String command = lines[0];
        		Integer val = Integer.decode(lines[1]);
        		if( "insert".equals(command) ) {
        			list.insert(val);
        		} else if ( "delete".equals(command) ) {
        			list.delete(val);
        		} else if ( "deleteFirst".equals(command) ) {
        			list.deleteFirst();
        		} else if ( "deleteLast".equals(command)) {
        			list.deleteLast();
        		}
        }

        boolean isFirst  = true;
        StringBuilder bd = new  StringBuilder();
        IchiLinkedListElement<Integer> element = list.getFirst();
        while( element != null ) {
        		if ( !isFirst ) {
        			bd.append(" ");
        		}
        		bd.append(element._val);
        		isFirst = false;
        }

        System.out.println(bd.toString());
    }

    public class IchiLinkedList<T> {
    		private IchiLinkedListElement<T> _first = null;
    		private IchiLinkedListElement<T> _last = null;

    		public IchiLinkedListElement<T> getFirst() {
    			return _first;
    		}

    		public IchiLinkedListElement<T> getLast() {
    			return _last;
    		}

    		public void insert( T element ) {
    			if ( element == null ) {
    				return;
    			}

      		// _endの操作
    			IchiLinkedListElement<T> listElement = new IchiLinkedListElement<T>(element);
    			listElement.setPrev(_last);
    			if( _last != null ) {
    				_last.setNext(listElement);
    			}
    			_last = listElement;

    			// _startを埋める
    			if( _first == null ) {
    				_first = listElement;
    			}
    		}

    		public void delete( T element ) {
    			IchiLinkedListElement<T> curElement = _first;
    			IchiLinkedListElement<T> sameElement = null;
    			while( curElement != null ) {
    				if( curElement._val.equals(element) ) {
    					sameElement = curElement;
    					break;
    				}
    			}
    			if( sameElement == null ) {
    				return;
    			}

    			IchiLinkedListElement<T> prev = sameElement._prev;
    			IchiLinkedListElement<T> next = sameElement._next;
    			if( prev != null ) {
    				prev.setNext(next);
    			} else {
    				_first = next;
    			}
    			if( next != null ) {
    				next.setPrev(prev);
    			} else {
    				_last = prev;
    			}
    		}

    		public void deleteFirst() {
    			if( _first == null ) {
    				throw new RuntimeException("Cannot delete because of empty.");
    			}
    			IchiLinkedListElement<T> next = _first._next;
    			_first = next;
    			if( next != null ) {
    				next._prev = null;
    			} else {
    				_last = next;
    			}
    		}

    		public void deleteLast() {
    			if( _first == null ) {
    				throw new RuntimeException("Cannot delete because of empty.");
    			}

    			IchiLinkedListElement<T> prev = _last._prev;
    			_last = prev;
    			if( prev != null ) {
    				prev._next = null;
    			} else {
    				_first = prev;
    			}
    		}
    }

    public class IchiLinkedListElement<T> {
    		private T _val = null;
    		private IchiLinkedListElement<T> _prev = null;
    		private IchiLinkedListElement<T> _next = null;

    		public IchiLinkedListElement(T val ) {
    			_val = val;
    		}

    		public void setPrev( IchiLinkedListElement<T> prev ) {
    			_prev = prev;
    		}

    		public void setNext( IchiLinkedListElement<T> next ) {
    			_next = next;
    		}

    		public T getVal() {
    			return _val;
    		}

    		public IchiLinkedListElement<T> getPrev() {
    			return _prev;
    		}

    		public IchiLinkedListElement<T> getNext() {
    			return _next;
    		}
    }
}