Code
Loading editor...
Tap Analyze to see visualization
Click Analyze to visualize
See step-by-step execution, variables, and output
Variables
Run code to see variables
Output
Console output will appear here
Click Analyze to visualize
See step-by-step execution, variables, and output
Run code to see variables
Console output will appear here
Merge from the end to avoid overwriting elements in nums1. Use three pointers: one at the end of valid elements in nums1, one at the end of nums2, and one at the last position of nums1. Place the larger element at the insert position and decrement the corresponding pointer.
Starting from the back ensures we never overwrite unprocessed nums1 elements, since the merged position is always at or beyond the current nums1 read position.
Updated Feb 2026