using System.Collections.Generic;
using System.Diagnostics;
public class LinkedListTraversal
public static void Main()
LinkedList<int> linkedList = new LinkedList<int>();
for (int i = 0; i < 10; i++ )
Console.WriteLine("Expected Value:6, Actual Value:" + LinkedListTraversal.FindNthFromLast(linkedList));
public static int FindNthFromLast(LinkedList<int> linkedList, int n = 5)
LinkedListNode<int> currentNode = linkedList.First;
LinkedListNode<int> targetNode = currentNode;
for (int i = 0; i < n - 1; i++)
currentNode = currentNode.Next;
throw new System.ArgumentException("There are less entries in the linked list than the nth element");
while (currentNode.Next != null)
targetNode = targetNode.Next;
currentNode = currentNode.Next;