Reverse an Array in Place in Python
Reverse an Array in Place
In this article, I'll share the solution to reverse an array in place along
with some traditional methods that can be used to reverse an array in python.
Now, this problem can be solved using two pointer approach, Think about it.
Take a two-pointers to suppose "i" and
"j" where "i" holds
the starting index of an array and "j" holds the last index of an array then starts swapping two elements at index
"i" and
"j" until you reach the middle.
Let's see this in action:
There are some other methods to reverse an array but instead of
reversing an array at its own place, they'll need some extra space O(n) to
store reversed array.
Thank you so much for reading this article, if you like this question please share this with your friends who love programming. And if you have questions or feedback please drop a comment.
To solve more problems please check out this article "Top 10 Array Interview Questions".
All the Best!
Comments
Post a Comment