Find nth element from the end of linked list

In this post, we will see how to find nth element from end of the linked list.

Nth node from end of linked list


Algorithm

  • Initialize two pointers, node and reference
  • Iterate reference node up to nth node from head of linked list.
  • Move node and reference pointer both by one node.
  • Once reference pointer reaches end of linked list, node will be nth node from the  end.

Complete java program to find nth element from end of linked list

Output

Linked list:
2 -> 9 -> 4 -> 1 -> 7
========================================
4th node from end of linked list: 9

That’s all about finding nth element from the end of linked list

Leave a Reply

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