I have a linked list and I need to make method that returns an iterator at a given point in the list. I currently have an iterator that starts at the head:
p
Without seeing your implementation, the trivial way to do this is:
public Iterator iterator(int x) {
if (x < 0 || this.size() < x) {
throw new IndexOutOfBoundsException();
}
Iterator it = new ListIterator();
for (; x > 0; --x) {
it.next(); // ignore the first x values
}
return it;
}
Otherwise, you could traverse the list to the xth node, but there's no reason you can't do it this way.