Search code examples
javalinked-listnodes

how to get a node by its position in the list in java? (not the value, the whole node)


I have a linked list and a position of a node, and I need to get the node itself from the list (not only its value). Is there a func in java that does that? if not, can you send a piece of code that does that?

Thanks.


Solution

  • No. Java’s LinkedList doesn’t allow you to work directly on the nodes.

    If you need an O(1) “pointer”, you can use a ListIterator: list.listIterator()

    If your linked list is a custom implementation, I’m 99% sure it’s your homework and you should do it yourself.