public void reverse(){
var current = first;
int array = new int[size];
int index = 0;
while (current != null){
array[index++] = current.value;
current = current.next;
}
index–;
current = first;
while (current != null){
current.value = array[index–];
current = current.next;
}
}
Hi,
Is the code complete?
Where are the first
, size
defined?
Just because of that it is unclear what you are trying to do with it (at least for first
). The var
does not help though I believe it is an int
.
I don’t know what language it is but if it was C# I wouldn’t do this way.
You should better use a for
loop for you know the size of the array.
Also C# has a hat operator for arrays that designated elements backwards.
Do you really need these first
, current
variables?
Not so sure.
So to me at first glance I would say not correct. You may also want to pass the array as a parameter to make it reusable.
FYI
There are simpler ways to reverse an array outside of doing it for the sake of exercice.
Array.Reverse(); And Linq has a Reverse operation too.