visual walkthrough
Merge Sorted Array
Filling from the end avoids overwriting data; a standard in-place merge.
The idea
nums1 already has spare room at its end. If you fill it from the back with the largest values first, you never overwrite a value you still need.
Compare the last unplaced value of each list, drop the bigger one into the last free slot, and walk backwards.
Complexity
| approach | time | space |
|---|---|---|
| Merge into a new array | O(m + n) | O(m + n) |
| Merge from the back | O(m + n) | O(1) |