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.


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.

Posted in data-structures and tagged , , .

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.