Drill

ProblemsC# › patterns

Reverse a list end to end

easypatternsTwo pointersArraysC#

A replay view shows the most recent reading first, so the buffer has to come back the other way round.

ReverseReadings(readings: list<int>) → list<int>

C# needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

public List<int> ReverseReadings(List<int> readings) {
    
}

Worked examples

CallResult
ReverseReadings(new List<int> { 1, 2, 3 })new List<int> { 3, 2, 1 }
ReverseReadings(new List<int> { 1, 2, 3, 4 })new List<int> { 4, 3, 2, 1 }
ReverseReadings(new List<int> { 7 })new List<int> { 7 }
ReverseReadings(new List<int> { })new List<int> { }

Hint

Put one index at each end. Swap what they point at, then step them towards each other until they meet.

Reference solution in C#
public List<int> ReverseReadings(List<int> readings) {
    var outList = new List<int>(readings);
    int i = 0, j = outList.Count - 1;
    while (i < j) {
        int swap = outList[i];
        outList[i] = outList[j];
        outList[j] = swap;
        i++;
        j--;
    }
    return outList;
}

The same problem in another language

More patterns problems in C#