Eric Yin Eric Yin - 1 month ago 5
C# Question

Easiest way to Rotate a List in c#

Lists say I have a list

List<int> {1,2,3,4,5}


Rotate means:

=> {2,3,4,5,1} => {3,4,5,1,2} => {4,5,1,2,3}


Maybe rotate is not the best word for this, but hope you understand what I means

My question, whats the easiest way (in short code, c# 4 Linq ready), and will not be hit by performance (reasonable performance)

Thanks.

Answer

You could implement it as a queue. Dequeue and Enqueue the same value.

**I wasn't sure about performance in converting a List to a Queue, but people upvoted my comment, so I'm posting this as an answer.