Inverse list without losing references
In an interview question, I saw this. How inverse the content of this list without loose the references.
List<s开发者_开发百科tring> list = new List<string>() {"AA", "BB", "CC" };
Update:
The question is more like this, you have:
public void ReversedList<T>(IList<T> listToReverse)
{
// To be implemented
}
sample the list has 1,2,3,4 and must return 4,3,2,1 without lose reference.
Do you mean reverse, i.e. change the order to "CC", "BB", "AA"? Just call List<T>.Reverse()
:
list.Reverse();
If that's not what you mean, please edit your question to be more specific. (It's not really clear what you mean by "without loose the references" either.)
Unless I'm missing something, list.Reverse()
would do exactly what you want (reverse the elements in place rather than create a new list with identical items in the reverse order).
精彩评论