Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. public class LinkedListIterator implements Iterator<T>{
  2.  
  3. ListNode<T> current = head, previous = null;
  4. boolean canRemove = false;
  5.  
  6. public boolean hasNext() {
  7. return current != null;
  8. }
  9.  
  10. public T next() {
  11. if(hasNext()==false){
  12. throw new IllegalStateException("hasNext was false!");
  13. }
  14. canRemove = true;
  15. previous = current;
  16. current = current.getNext();
  17. return previous.getValue();
  18. }
  19.  
  20. public void remove(){
  21. if(canRemove == true){
  22. previous.setNext(current.getNext());
  23. canRemove = false;
  24. }
  25. }
  26.  
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement