/** * An Iterator that supports traversing a SimpleLinkedList. * Normally, this would be integrated into the SimpleLinkedList * class by adding the iterator() method to the SimpleLinkedList class: * * @Override * public Iterator iterator() { * return new SimpleLinkedListIterator(this.head); * } * * @author Sean Barker */ public class SimpleLinkedListIterator implements Iterator { // next Node in the traversal private Node nextNode; public SimpleLinkedListIterator(Node headNode) { nextNode = headNode; } @Override public T next() { T val = nextNode.data; nextNode = nextNode.next; // advance the traversal return val; } @Override public boolean hasNext() { return nextNode != null; } }