<p>In this article, we will discuss an algorithm to find the nth node from the end of a singly linked list. First, let&#8217;s discuss what is singly liked list.</p>
<h2 id="1" style="border-bottom: 1px solid #eaecef;">Singly Linked List</h2>
<p>It is a linked list, we can traverse only forward direction. We can&#8217;t move to backwards. That means, in a singly linked list, each node in the list has the content and a pointer to the next node in the list. It does not store any pointer to the previous node. To store a single linked list, only the pointer to the first node in that list must be stored. The last node in a single linked list points to nothing.</p>
<p><img class="size-full wp-image-4237" src="https://dineshonjava.com/wp-content/uploads/2018/09/SingleLinkedList.jpg" alt="nth node from the end of a singly linked list " width="960" height="720" /></p>
<h2 id="2" style="border-bottom: 1px solid #eaecef;">The nth node from the end of a singly linked list</h2>
<p>There are many solutions to this problem are available online specifically in GeeksforGeeks. Let&#8217;s discuss one of the solutions to this problem.</p>
<p><strong>Step 1:</strong> First, let&#8217;s take two pointers to suppose pointer1 and pointer2.</p>
<p><strong>Step 2:</strong> Intially initialize these two pointers with the head of the linked list.</p>
<pre>pointer1=head; 
pointer2=head; 
</pre>
<p><strong>Step 3:</strong> Now fix the pointer1 and let&#8217;s pointer2 till the nth node from the head of the singly linked list.</p>
<pre>while(n >; 0){ 
 pointer2 = pointer2->;next; 
 n--; 
} 
</pre>
<p><strong>Step 4:</strong> If pointer2 is the last node of the singly linked then pointer1 is the nth node from the end.</p>
<p><strong>Step 5:</strong> If pointer2 is not the last node of the singly liked list then let&#8217;s start moving forward pointer1 till pointer2 will reach to the last node of this singly linked list.</p>
<pre>while(pointer2->;next != null){ 
 pointer2 = pointer2->;next; 
 pointer1 = pointer1->;next; 
} 
</pre>
<p><strong>Step 6:</strong> Finally, we found pointer1 as the nth node from the end of the singly linked list.</p>
<h2>Code</h2>
<pre>//Pseudo code 
 
 
function(head,n){ 
 pointer1=head 
 pointer2=head 
 
 while(n--){ 
 pointer2=pointer2.next 
 } 
 //pointer2 reached nth node 
 while(pointer2.next is not null){ 
 pointer2=pointer2.next 
 pointer1=pointer1.next 
 } 
 
 return pointer1 
} 
 
</pre>
<p>Hope, you have understood this solution for the above find the nth node from the end of a singly linked list. Please share other solutions if you have. :).</p>
<p>Happy learning with us!!!.</p>
<div align="center"><iframe style="width: 120px; height: 240px;" src="//ws-in.amazon-adsystem.com/widgets/q?ServiceVersion=20070822&;OneJS=1&;Operation=GetAdHtml&;MarketPlace=IN&;source=ac&;ref=tf_til&;ad_type=product_link&;tracking_id=dineshonjav06-21&;marketplace=amazon&;region=IN&;placement=1787127567&;asins=1787127567&;linkId=a1e64f621b5e6128d8cb10e876eb1c48&;show_border=true&;link_opens_in_new_window=true&;price_color=333333&;title_color=0066c0&;bg_color=ffffff" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" align="center"><span data-mce-type="bookmark" style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;" class="mce_SELRES_start"></span><br />
</iframe></div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/find-the-middle-element-in-a-linked-list/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '4236'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…