Java – syntax for moving backward in LinkedList?

I know that linkedlists are implemented in a two-way link, so each node has a next and a previous pointer However, I can't find the syntax to access the previous node?

I'm trying to design an experiment to prove that linkedlists is not just a single link list, but I don't know how to do this without moving the link list backward

If possible, please explain to me how to move backward. Thank you very much

Solution

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();
}

This does not prove that it is a two-way linked list - for example, it can be implemented very inefficiently in a single linked list - but that's how I iterate back on the linked list

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>