I have this method which reverses a list of numbers. For example, if the given input is { 1232, 3455, 6071 }, it will return {2321, 5543, 1706}
public static List<int> ReverseIntegerList_Array(List<int> intList)
{
var newIntList = new List<int>();
foreach (var numbers in intList)
{
var charNums = numbers.ToString();
char[] arrNums = new char[charNums.Length];
for (int x = 0; x < charNums.Length;x++)
{
arrNums[x] = charNums[charNums.Length-1-x];
}
newIntList.Add(Convert.ToInt32(string.Join("",arrNums)));
}
return newIntList;
}
Can I reduce the space complexity further by not using an extra list object to store the result and return the result by overwriting the existing list object (defined in the method parameter)?
Found the way already.
public static List<int> ReverseIntegerList_Array3(List<int> intList)
{
// var newIntList = new List<int>();
for (int i = 0; i < intList.Count; i++)
{
var charNums = intList[i].ToString();
intList[i] = 0; // Clear it.
char[] arrNums = new char[charNums.Length];
for (int x = 0; x < charNums.Length; x++)
{
arrNums[x] = charNums[charNums.Length - 1 - x];
}
intList[i] = Convert.ToInt32(string.Join("", arrNums));
}
return intList;
}
Is this solution okay in regards to space complexity?
