SIGN UP MEMBER LOGIN:    
ARTICLE

Find nth element from last in a Linked List

Posted by Gaurav Rawat Articles | C# Language January 27, 2011
One very interesting problem is to find the nth element from the end in a linked list. Now it's very easy to find the nth element from the beginning of the list and can be done in one traverse. So what are the various solutions we can think of.
Reader Level:


One very interesting problem is to find the nth element from the end in a linked list. Now it's very easy to find the nth element from the beginning of the list and can be done in one traverse. So what are the various solutions we can think of.

One of the methods involves traversing the list once to find the length say the length is l.

Now we need to find the nth element from last that means (l-m)th element from head so we need to traverse the list once again until we reach (l-m)th element.

This method is good for small lists where the number of elements to be traversed are not large but if the number is large then there would be a problem since there would be twice the traversal.

Another method is to hold a pointer to a node which is m nodes ahead and traverse the list until the node reaches last so the node which is m nodes behind is the desired node.

Lets define a simple Node class :

image1.gif

The code for this algorithm is below:

image2.gif

There is a one more solution in which we don't have to as much traversing as we had done above. This is a bit complicated but interesting. In it we traverse the list in rounds of n nodes and maintain a pointer to the node which is beginning of the node which is m nodes behind the beginning of current node. e.g. if a list has 10 elements and the elements are 1-2-3-4-5-6-7-8-9-10 and we need to find the 4th element from last i.e. 7 we would proceed the list in a set of 4 elements so that when current reaches 5th node, pointer is at 1st node. When it reaches 9th node, the pointer is at 5th node. After 9th node we get to the last node in 2 turns so mth node will be 5+2 i.e. 7th node. The code to implement this is as follows :

image3.gif

erver'>
Login to add your contents and source code to this article
share this article :
post comment
 

Thanks Sam for other alternatives. Off course there could be many more solutions to the problem. My idea was to show how can this be done in a singly linked list.With a doubly linked list and an array we don't have to write so much code.The whole idea was to show can pointer iteration be shown in C#.

Posted by Gaurav Rawat Jan 27, 2011

When the requirement is to access items by position, the ideal solution of course is to use an array. Another possibility is to use a doubly-linked list; in other words, a list that supports both forward and backward traversal. In situations in which a list must be used that only has forward pointers, it would be possible to create another list or an array that could be re-used.

Posted by Sam Hobbs Jan 27, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Nevron Gauge for SharePoint
Become a Sponsor