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

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();
    		int lineCnt = Integer.parseInt(line);
    		IchiLinkedList<Integer> list = new IchiLinkedList<>();
    		while( lineCnt > 0 ) {
    			lineCnt--;
    			line=bf.readLine();
        		String[] lines = line.split(" ");
        		String command = lines[0];
        		Integer val = ( lines.length >= 2 ) ? Integer.decode(lines[1]) : null;
        		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;
        		element = element.getNext();
        }

        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;
    			}

    			IchiLinkedListElement<T> insertElement = new IchiLinkedListElement<T>(element);
    			if( _first == null ) {
    				_first = insertElement;
    				_last = _first;
    			} else {
        			insertElement._next = _first;
        			_first._prev = insertElement;
        			_first = insertElement;
    			}
    		}

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

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

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

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

    			if( _first == _last ) {
    				_first = null;
    				_last = null;
    			} else {
    				_last = _last._prev;
    				_last._next = null;
    			}
    		}
    }

    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;
    		}
    }
}