I tried to use a two-pointer algorithm but realized that I need 3 pointers in this case.
var merge = function(nums1, m, nums2, n) {
let i = m - 1; // Pointer for the last initialized element in nums1
let j = n - 1; // Pointer for the last element in nums2
let k = m + n - 1; // Pointer for the last position in nums1
// Iterate backwards and fill nums1 from the end
while (j >= 0) {
if (i >= 0 && nums1[i] > nums2[j]) {
nums1[k] = nums1[i];
i--;
} else {
nums1[k] = nums2[j];
j--;
}
k--;
}
return nums1;
};