# Linked List reversing. Is my method correct or unacceptable?

**URL:** <https://forum.codewithmosh.com/t/linked-list-reversing-is-my-method-correct-or-unacceptable/19437>\
**Category:** Data Structures\
**Created:** [April 7, 2023, 2:33pm UTC](https://forum.codewithmosh.com/t/linked-list-reversing-is-my-method-correct-or-unacceptable/19437 "2023-04-07T14:33:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nevy](https://avatars.discourse-cdn.com/v4/letter/n/3ec8ea/32.png) [@nevy](https://forum.codewithmosh.com/u/nevy)\
**Post date:** [April 7, 2023, 2:33pm UTC](https://forum.codewithmosh.com/t/linked-list-reversing-is-my-method-correct-or-unacceptable/19437/1 "2023-04-07T14:33:54Z")

</div>

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;  
}  
}

---

<div class="post-metadata">

**Author:** ![UniqueNospaceShort](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/uniquenospaceshort/32/1814_2.png) [@UniqueNospaceShort](https://forum.codewithmosh.com/u/UniqueNospaceShort)\
**Post date:** [April 7, 2023, 11:34pm UTC](https://forum.codewithmosh.com/t/linked-list-reversing-is-my-method-correct-or-unacceptable/19437/2 "2023-04-07T23:34:50Z")

</div>

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.

> **[Array.Reverse Method (System)](https://learn.microsoft.com/en-us/dotnet/api/system.array.reverse?view=net-10.0)**
>
> Reverses the order of the elements in a one-dimensional Array or in a portion of the Array.

> **[Enumerable.Reverse Method (System.Linq)](https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.reverse?view=net-10.0)**
>
> Inverts the order of the elements in a sequence.
