Solution Review: Return the Nth Node From The End
This review provides a detailed analysis on how to return the "nth" node from the end.
We'll cover the following...
Solution 1: Double iteration
Press + to interact
main.cs
LinkedList.cs
using System;namespace chapter_3{class Challenge_10{static void Main(string[] args){LinkedList list = new LinkedList(); //creating listfor (int j = 0; j <= 7; j++){list.InsertAtHead(j); //insertng data in listlist.PrintList();}int num = 5;int nth = list.FindNth(num); // calling findNth function of the listConsole.WriteLine(num + "th element from end of the list : " + nth);return;}}}
In this approach, the main goal is to figure out the index of the node you need to reach. The algorithm follows these simple steps:
- Calculate the length of the linked list.
- Check if
n
is within the length. - Find