Implement reverse_in_place(arr) which reverses the list in place using two pointers and returns the same list object.
Swap the elements at the left and right pointers, then move the pointers toward each other. Do not allocate a new list. Lists with zero or one element are already reversed.
def two_sum_sorted(nums, target):
i, j = 0, len(nums) - 1
while i < j:
s = nums[i] + nums[j]
if s == target:
return (i, j)
elif s < target:
i += 1
else:
j -= 1
return None
def is_palindrome(s):
i, j = 0, len(s) - 1
while i < j:
while i < j and not s[i].isalnum():
i += 1
while i < j and not s[j].isalnum():
j -= 1
if s[i].lower() != s[j].lower():
return False
i += 1
j -= 1
return True
def reverse_in_place(arr):
"""Reverse arr in place with two pointers and return it."""
# TODO: swap arr[i] and arr[j] while moving the pointers inward
pass
The editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.
Implement reverse_in_place(arr) which reverses the list in place using two pointers and returns the same list object.
Swap the elements at the left and right pointers, then move the pointers toward each other. Do not allocate a new list. Lists with zero or one element are already reversed.
Press Run Tests or ββ© to check your solution.