Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LinkedListIterator implements Iterator<T>{
- ListNode<T> current = head, previous = null;
- boolean canRemove = false;
- public boolean hasNext() {
- return current != null;
- }
- public T next() {
- if(hasNext()==false){
- throw new IllegalStateException("hasNext was false!");
- }
- canRemove = true;
- previous = current;
- current = current.getNext();
- return previous.getValue();
- }
- public void remove(){
- if(canRemove == true){
- previous.setNext(current.getNext());
- canRemove = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement