Reverse a list in C#
This post will discuss how to reverse a list in C#.
1. Using Enumerable.Reverse() method
To create a reversed copy of the original list, we can use the Enumerable.Reverse() method. It just creates a new sequence with elements in the reverse order without modifying the underlying list. The following code example reverses a list using the Reverse() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; using System.Linq; using System.Collections.Generic; public class Example { public static void Main() { List<int> list = new List<int> { 1, 2, 3, 4, 5 }; List<int> reverse = Enumerable.Reverse(list).ToList(); Console.WriteLine(String.Join(',', reverse)); } } /* Output: 5,4,3,2,1 */ |
2. Using List<T>.Reverse() method
To reverse the order of the elements within the specified list, we can use the List<T>.Reverse() method. List<T>.Reverse() uses an in-place algorithm. That means that the conversion occurs without using any auxiliary list by overwriting the existing elements of the specified list.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System; using System.Collections.Generic; public class Example { public static void Main() { List<int> list = new List<int> { 1, 2, 3, 4, 5 }; list.Reverse(); Console.WriteLine(String.Join(',', list)); } } /* Output: 5,4,3,2,1 */ |
3. Using List<T>.RemoveAt() method
Another approach to in-place reverse a list is to reorder the elements present in the list using a for-loop, which removes an element from the end of the list and insert it into the very beginning, one at a time.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
using System; using System.Collections.Generic; public class Example { public static void Main() { List<int> list = new List<int> { 2, 4, 6, 8 }; for (int i = 0, j = list.Count - 1; i < j; i++) { int temp = list[j]; list.RemoveAt(j); list.Insert(i, temp); } Console.WriteLine(String.Join(',', list)); } } /* Output: 8,6,4,2 */ |
4. Using Recursion
The following code example demonstrates how to use recursion to in-place reverse a list.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
using System; using System.Collections.Generic; public class Example { public static void reverseList<T>(List<T> list) { // base case: the list is empty, or only one element is left if (list == null || list.Count <= 1) { return; } // remove the first element T value = list[0]; list.RemoveAt(0); // recur for remaining items reverseList(list); // insert the top element back after recurse for remaining items list.Add(value); } public static void Main() { List<int> list = new List<int> { 2, 4, 6, 8 }; reverseList(list); Console.WriteLine(String.Join(',', list)); } } /* Output: 8,6,4,2 */ |
5. Naive Solution
We can also write our own custom routine to reverse the list in-place.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
using System; using System.Collections.Generic; public class Example { public static void Main() { List<int> list = new List<int> { 1, 2, 3, 4, 5 }; int n = list.Count; for (int i = 0; i < n/2; i++) { int temp = list[i]; list[i] = list[n-i-1]; list[n-i-1] = temp; } Console.WriteLine(String.Join(',', list)); } } /* Output: 5,4,3,2,1 */ |
That’s all about reversing a list in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)