public class LinkedList { private Node head, tail; public LinkedList() { head = new Node<>(null, null, null); tail = new Node<>(null, head, null); head.setNext(tail); } private class Node { private Node next, prev; private E e; public Node(Node nxt, Node prv, E elem) { next = nxt; prev = prv; e = elem; } public Node getNext() {return next;} public void setNext(Node nxt) { next = nxt; } public Node getPrev() {return prev;} public void setPrev(Node prv) { prev = prv; } public E getElement() { return e; } public void setElement(E elem) { e = elem; } } private class Iterator implements java.util.Iterator { Node curr; public Iterator(Node c) { curr = c; } public boolean hasNext() { if(curr.getNext() != tail) return true; return false; } public E next() { curr = curr.getNext(); return curr.getElement(); } public void remove() { curr.getPrev().setNext(curr.getNext()); curr.getNext().setPrev(curr.getPrev()); curr = null; } } Iterator iterator() { return new Iterator(head); } // append in the end public void add(E elem) { Node plus = new Node(tail, tail.getPrev(), elem); tail.getPrev().setNext(plus); tail.setPrev(plus); } public boolean contains(E elem) { Iterator it = iterator(); while(it.hasNext()) { E e = it.next(); if( e.equals(elem) ) return true; } return false; } public int indexOf(E elem) { Iterator it = iterator(); int index = -1; while(it.hasNext()) { E e = it.next(); index++; if( e.equals(elem) ) return index; } return -1; } }