I am getting 0 while trying to retreive the value in the nth node. While debugging I saw that in the function current is set to null when . Can someone please suggest how to correct this.
Junilu Lacar wrote:This is unrelated to your question but your deleteLast() method will never delete the last node of a list that has only one node in it.
I have made the change, can you please take a look and let me know if it seems right?
Junilu Lacar wrote:Your getSize() method is recursive and it clobbers the head. This messes up anything else you do with your list after that.
Thanks, I have re-implemented tt]getSize()[/tt] method.
Junilu Lacar wrote:Also, on line 99, you check head. I don't think that's right especially since you're traversing the list using current as a cursor.
Thanks, I have made the changes.
Here is my code :
One option might be to have your return type as Optional<Integer> instead. Then the usage would be something like this:
You'll need to learn how to use java.util.Optional though. you should be able to find some articles out there that show you how. Optional was introduced in Java 8.