How can I do the equivalent of the following C++ snippet using C# LinkedList
?
std::list<MyClass*>::reverse_iterator itr(it);
for(; itr != MyList.rend(); ++itr)
As a 1-off, something like:
var el = list.Last;
while (el != null) {
// use el.Value
el = el.Previous;
}
If you are doing it regularly, maybe a similar iterator block to yield all the values:
public static IEnumerable<T> Reverse<T>(this LinkedList<T> list) {
var el = list.Last;
while (el != null) {
yield return el.Value;
el = el.Previous;
}
}
then:
foreach(var val in list.Reverse()) {
// use val
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With