I have a linked list constructed as follows:
LinkedList linked = new LinkedList();
var array = new int[] { 23, 55, 64, 65 };
foreach (
The only way is to check element by element and increase a counter (by "only way", I am saying that other methods like LINQ need to do the same thing internally).
A hand-written extension method would look something like this:
public static class LinkedListExt
{
public static int IndexOf(this LinkedList list, T item)
{
var count = 0;
for (var node = list.First; node != null; node = node.Next, count++)
{
if (item.Equals(node.Value))
return count;
}
return -1;
}
}
But it can easily be done using LINQ as @L.B wrote (yielding the same time complexity).