:3
Everything is barely weeks. Everything is days. We have minutes to live.
- about me
- Github: @r1bb1t-h0l3
Everything is barely weeks. Everything is days. We have minutes to live.
Posted
Reversing a list is a common operation, especially in algorithmic problem-solving and data manipulation. Even though Python provides simple, built-in ways to reverse a list, since I am specifically trying to improve my algorithmic thinking and logical reasoning skills, I am focussing on purely algorithmic reversal. Specifically I am doing this as prep to solving a DMOJ problem I am working on which I will share a bit later.
So here are the three different ways I found to reverse a list in Python without using any built-in methods like .reverse()
or slicing([::-1])
. They all make sense to me, so hopefully as I get a little more practise implementing them the logic will start to feel second nature. My favourite is the last one with modulo and floor division, as the second one still feels a bit like “python magic” with tuple unpacking being used.
One of the simplest ways to reverse a list is to build a new list by iterating over the original list from the end to the beginning. This approach is straightforward and doesn’t modify the original list.
# Original list
numbers = [1, 2, 3, 4, 5]
# Create an empty list to hold the reversed elements
reversed_numbers = []
# Iterate from the end of the original list to the beginning
for i in range(len(numbers) - 1, -1, -1):
reversed_numbers.append(numbers[i])
print(reversed_numbers) # Output: [5, 4, 3, 2, 1]
How It Works:
We use a loop to move from the last element(len(numbers) - 1)
down to the first (0)
.
Each element is appended to the reversed_numbers list, building the reversed list step-by-step.
Pros and Cons:
Pros: This approach doesn’t modify the original list, which can be useful if you need to keep the original data intact. Cons: It requires additional space (a new list), which can be inefficient for very large lists.Use Case: Use this method when you need a reversed copy of the list, leaving the original list unmodified.
If you want to reverse the list in place (modifying the original list) without using additional memory, a two-pointer technique is highly efficient. This method involves swapping elements from the two ends of the list, gradually moving toward the center. I do like the two-pointer approach as well.
Code Example:
# Original list
numbers = [1, 2, 3, 4, 5]
# Initialize pointers
start = 0
end = len(numbers) - 1
# Swap elements until the pointers meet in the middle
while start < end:
# Swap elements at start and end
numbers[start], numbers[end] = numbers[end], numbers[start]
# Move pointers toward the center
start += 1
end -= 1
print(numbers) # Output: [5, 4, 3, 2, 1]
How It Works:
We start with two pointers:start
at the beginning and end
at the end of the list.
Each iteration swaps the elements at start
and end
, then moves both pointers inward until they meet in the middle.
Pros and Cons:
Pros: This approach doesn’t require extra space, making it memory efficient. Cons: It modifies the original list, so it’s not suitable if you need the original order preserved.Use Case: Use this method when you want an in-place reversal of the list to save memory, especially with large lists.
When reversing a list of digits stored as an integer (like 12345), it’s possible to reverse it manually without converting it to a string. This method is useful when working with numbers directly.
Code Example:
# Original number
num = 12345
reversed_num = 0
while num > 0:
# Extract the last digit
last_digit = num % 10
# Shift reversed_num left and add the last digit
reversed_num = reversed_num * 10 + last_digit
# Remove the last digit from num
num //= 10
print(reversed_num) # Output: 54321
How It Works:
We extract the last digit fromnum
using modulo (num % 10)
.
We multiply reversed_num
by 10 (shifting digits left) and add the extracted digit.
We divide num
by 10 (integer division) to remove the last digit.
This process continues until num
becomes 0.
Pros and Cons:
Pros: This method works entirely on integers, so there’s no need to convert between types. Cons: Limited to numbers; won’t work with lists of other types.Use Case: Use this method when working with integers where you need to reverse the digits.
So as far as I can surmise these are the three main algorithmic methods to reverse a number list in place. For my purposes only methods 1 and 2 are valid because I need to reverse a string of letters. I will post the challenge I am working on next!