i know that LinkedLists are implemented in a doubly linked way, so each node has a next and a previous pointer. however, i couldn't find what syntax to use to access the previous nodes? i looked on the java api, and there is a method to iterate through a linkedlist backwards. that to me, implies that there is an easy way to access previous nodes P:.
i am trying to design an experiment to prove that LinkedLists isn't just a singlely linked list but i can't think of how to do so without moving backwards in linkedlists.
please explain to me how to move backwards if it is possible, thank you very much.
LinkedList
has a listIterator(int)
method. So you can use:
// Start at the end...
ListIterator<Foo> iterator = list.listIterator(list.size());
while (iterator.hasPrevious()) {
Foo foo = iterator.previous();
}
That doesn't prove that it's a doubly-linked list - that could be implemented very inefficiently in a single-linked list, for example - but that's how I'd go about iterating over a linked list backwards.
See more on this question at Stackoverflow