• Post Reply Bookmark Topic Watch Topic
  • New Topic

Get Nth node in a Linked List  RSS feed

 
Greenhorn
Posts: 23
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello , I am trying to learn datastructures and algorithms .I was trying to implement a Linked List and write a fucntion to get the nth node in the linked list. Following is my code:


Main Class:


Output :


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.



 
dhrubo bhattacharjee
Greenhorn
Posts: 23
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry forgot to add the Node class:

 
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you trying to get the 11th node in the list that is size 11?
Generally, the method is implemented as get(int index) where index is from 0 through size-1, therefore would only go up to an index of 10 in your case.
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your getSize() method is recursive and it clobbers the head. This messes up anything else you do with your list after that.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, your deleteFirst() method will throw a RuntimeException if called when the list is empty. Unlike your deleteLast() method, your deleteFirst() method will delete the head node if it is the only node in the list.
 
dhrubo bhattacharjee
Greenhorn
Posts: 23
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 :


Main Method:


Output:



 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your getNthNode() method only works in the happy path cases, where the specified N is valid.  If N is greater than the size of a non-empty list, then your method will always return 0.  That's fine if 0 is not a valid value for Node.data. However, if it is, then how do you distinguish 0 as valid node data from 0 as a flag to signal that there was no Nth node?

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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!