The following code will iterate over LinkedList efficiently in reverse order:
LinkedList<string> list = new LinkedList<string> (new[] {"cat", "dog", "frog", "antelope", "gazelle"}); LinkedListNode<string> item = list.Last; do { Console.WriteLine(item.Value); item = item.Previous; } while (item != null); Console.ReadKey();
The key here is that LinkedList only contains reference to instances of the First and Last LinkedListNode of the list. Each instance of LinkedListNode contains a link to the next and previous element in the list (or null at each end of the list), as well as the Value property. This means that iterating from the first or last LinkedListNode is simple, but random access requires iterating from the first or last in the list.
If you need to make input in a path, use LinkedList.AddBefore or AddAfter to insert a new LinkedListNode.
spender
source share