Two Heads for Two Arrays
Each pointer walks a different array. A first while advances both while they are valid and decides what to take; two trailing while loops drain whichever array still has leftovers. This 3-loop template handles merges and comparisons cleanly, and the fill direction (forward or backward) is chosen to avoid clobbering data still being read.
Three While Loops
88. Merge Sorted Array
Easy·
Three-While Merge from the Back
O(M+N)O(1)
FIG. 88 MERGE SORTED ARRAY● INTERACTIVE
visualization loads as you reach it
1768. Merge Strings Alternately
Easy·
Parallel Walk
O(M+N)O(M + N)
FIG. 1768 MERGE STRINGS ALTERNATELY● INTERACTIVE
visualization loads as you reach it
165. Compare Version Numbers
Medium·
Parallel Revision Scan
O(M+N)O(1)
FIG. 165 COMPARE VERSION NUMBERS● INTERACTIVE
visualization loads as you reach it
Miscellaneous
392. Is Subsequence
Easy·
Greedy Match
O(N)O(1)
FIG. 392 IS SUBSEQUENCE● INTERACTIVE
visualization loads as you reach it
844. Backspace String Compare
Easy·
Reverse Scan with Skip Counters
O(M+N)O(1)
FIG. 844 BACKSPACE STRING COMPARE● INTERACTIVE
visualization loads as you reach it
242. Valid Anagram
Easy·
3 Approaches · 2 patterns
Sort Both, Walk in Lockstep
O(N + 2N log N)O(sort + 2N)
Compare Two Frequency Counters
O(2N + k)O(2k)
Sliding Window↗ Sliding Window
O(N)O(1)
FIG. 242 VALID ANAGRAM SORTED LOCKSTEP● INTERACTIVE
visualization loads as you reach it