6
\$\begingroup\$

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?

\$\endgroup\$
1
  • 5
    \$\begingroup\$ Title should really be "Reverse numbers in a list" :) \$\endgroup\$ Commented Mar 10, 2020 at 18:42

3 Answers 3

14
\$\begingroup\$

You can improve the efficiency by reversing the numbers as integers instead of converting to strings and back.

Modifying the list and returning it is redundant since the list is passed by reference and your changes will persist to the original list.

Simplified your code could look like this:

public static void ReverseIntegerList_Array(List<int> intList)
{
    if(intList == null)
    {
        return;
    }
    int limit = intList.Count;
    for(int i = 0; i < limit;++i)
    {
        intList[i] = ReverseNum(intList[i]);
    }
}

public static int ReverseNum(int num)
{
    int retVal = 0;
    while(num > 0)
    {
        retVal = (retVal * 10) + num % 10;
        num /= 10;
    }
    return retVal;
} 
\$\endgroup\$
6
  • \$\begingroup\$ Thanks for the solution. For this solution, the time complexity is o(n2)? \$\endgroup\$ Commented Mar 10, 2020 at 9:01
  • 1
    \$\begingroup\$ What do you think the old time and space complexity was and what the name one is? (Also how do you think conversion from int to characters is actually accomplished? Hint: It looks pretty much identical to your loop) \$\endgroup\$ Commented Mar 10, 2020 at 18:27
  • \$\begingroup\$ @Voo - Converting to and from a string requires extra time and space \$\endgroup\$ Commented Mar 10, 2020 at 20:21
  • 1
    \$\begingroup\$ @tinstaafl That's not what's meant with space and time complexity. You should read the wikipedia article on the topic since this is a much too large topic to explain in a comment. \$\endgroup\$ Commented Mar 10, 2020 at 20:23
  • \$\begingroup\$ @tinstaafl You don't need the ReverseNum, just move it inside the for loop, this would make it nicer. \$\endgroup\$ Commented Mar 10, 2020 at 20:24
5
\$\begingroup\$

You can convert int to string, then reverse, back to string and to int. This is two lines solution:

public static void ReverseIntegerList_Array(List<int> integers)
{
    for (int i = 0; i < integers.Count; i++)
        integers[i] = int.Parse(new string(integers[i].ToString().Reverse().ToArray()));
}
\$\endgroup\$
2
\$\begingroup\$

As we are working in C#, below is an example of an object-oriented approach, in contrast to the static/procedural approach.

UPDATE: In the original answer I neglected to create a Number class to handle the reversing of the value. I have added it.

And here's the output:
output

public class App_ReverseNumbers
{
    public void Run()
    {
        var list = new List<int> { 123, 456, 789 };

        var numbers = new Numbers(list);
        Console.WriteLine(numbers.ToString());

        var reversed = numbers.AsReversed();
        Console.WriteLine(reversed.ToString());
    }        
}

public class Numbers
{
    public List<int> List;

    public Numbers(List<int> list) => List = list;

    public Numbers AsReversed() => 
        new Numbers(List.Select(i => new Number(i).Reverse()).ToList());

    public override string ToString() => string.Join(", ", List);
}

public class Number
{
    public int Value { get; private set; }

    public Number(int value) => Value = value;

    public int Reverse() => int.Parse(new string(Value.ToString().Reverse().ToArray()));
}
\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.