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: Memory Limit Exceeded)

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		DLinkedList dLinkedList = new DLinkedList();
		final String insert = "insert";
		final String delete = "delete";
		final String deleteFirst = "deleteFirst";
		final String deleteLast = "deleteLast";
		Scanner sc = new Scanner(System.in);
		int commandNum = Integer.parseInt(sc.nextLine());
		List<Command> commands = new ArrayList<>();
		for(int i = 0; i < commandNum; i++) {
			String commandLine = sc.nextLine();
			Command command;
			if(commandLine.equals(deleteFirst) || commandLine.equals(deleteLast)) {
				command = new Command(commandLine, null);
			} else {
				String[] commandNameKey = commandLine.split(" ");
				command = new Command(commandNameKey[0], commandNameKey[1]);
			}
			commands.add(command);
		}
		sc.close();
		for(Command command: commands) {
			String commandName = command.commandName;
			String commandKey = command.commandKey;
			switch (commandName) {
			case insert:
				dLinkedList.addFirst(commandKey);
				break;

			case delete:
				dLinkedList.delete(commandKey);
				break;

			case deleteFirst:
				dLinkedList.removeFirst();
				break;

			case deleteLast:
				dLinkedList.removeLast();
				break;
			}
		}
		System.out.println(dLinkedList.toString());
	}

}

class Command {
	String commandName;
	String commandKey;
	public Command(String commandName, String commandKey) {
		this.commandName = commandName;
		this.commandKey = commandKey;
	}
}
//Cell???????????????1????´??????¨???
class Cell {
 // ????????????????´??????????????????????????????§
 private Object data;
 // ?¬????????´????????????§
 private Cell next;
 // ??????????´????????????§
 private Cell prev;

 // ?????????????????????
 // next, prev??????????????? null ?????????????????????
 public Cell(Object o) {
     data = o;
 }
 // next??????????????????
 public void setNext(Cell next) {
     this.next = next;
 }
 // next?????????
 public Cell getNext() {
     return next;
 }
 // prev??????????????????
 public void setPrev(Cell prev) {
     this.prev = prev;
 }
 // prev?????????
 public Cell getPrev() {
     return prev;
 }
 // ??????????????????
 public Object getData() {
     return data;
 }
}

class DLinkedList {
    // ??????????????????????????§
    private Cell dummy;

    // ?????????????????????
    public DLinkedList() {
        // ??????????????????????????????next??¨prev????????????????????????????¨??????????
        dummy = new Cell(null);
        dummy.setNext(dummy);
        dummy.setPrev(dummy);
    }

    // ?????????????????????????´??????????
    public void addFirst(Object data) {
        // ???????????????
        Cell c = new Cell(data);
        c.setNext(dummy.getNext());
        c.setPrev(dummy);
        dummy.getNext().setPrev(c);
        dummy.setNext(c);
    }

    // ?????????????????????????´??????????????????????
    public Object removeFirst() {
    		Cell c = dummy.getNext();
        if (c == dummy) {
            return null;
        }
        c.getNext().setPrev(c.getPrev());
        c.getPrev().setNext(c.getNext());
        return c.getData();
    }

    public Object removeLast() {
        Cell c = dummy.getPrev();
        if (c == dummy) {
            return null;
        }
        c.getNext().setPrev(c.getPrev());
        c.getPrev().setNext(c.getNext());
        return c.getData();
    }

    // ?????????????´????????????????????????° null ?????????
    public Object getFirst() {
        Cell c = dummy.getNext();
        if (c == dummy) {
            return null;
        }
        return c.getData();
    }

    // index?????????????´?????????????????°?????¶??????????null????????????
    public Object get(int index) {
        Cell c = dummy.getNext();
        for (int i = 0; i < index; i++) {
            if (c == dummy) {
                return null;
            }
            c = c.getNext();
        }
        return c.getData();
    }

    public void delete(Object data) {
    		Cell deleteCell;
    		for(deleteCell = dummy.getNext(); deleteCell != dummy; deleteCell = deleteCell.getNext()) {
    			if(data.equals(deleteCell.getData())) {
    				deleteCell.getNext().setPrev(deleteCell.getPrev());
    		        deleteCell.getPrev().setNext(deleteCell.getNext());
    		        break;
    			}
    		}
    }

    // ???????????????????????¢??????????????????
    public String toString() {
        // StringBuilder???????????????????????¨????????????
        StringBuilder sb = new StringBuilder();
        for (Cell c = dummy.getNext(); c != dummy; c = c.getNext()) {
            // StringBuilder???append??????????????§??????????????????
            sb.append(c.getData());
            if (c.getNext() != dummy) {
                sb.append(" ");
            }
        }
        // ????????? toString??????????????§String?????????
        return sb.toString();
    }
}