Find the Nth node/element from a LinkedList in java

You have given a LinkedList (not java.util.LinkedList), and a number N.

Write a program to find/print the value at the Nth node from a LinkedList in Java.

Solution:

1)      Initialize length to zero

2)      Traverse through the list

  1. If length is equal to the passed index, return current element
  2. Increment length
  3. Assign current element to next element of current.

3)      If user passed last index of a list

  1. Check current node is points to null and length is equal to the passed index and return that node.
 Output:
 

Posted in data-structures and tagged , , .

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">