Only O(length1 * length2) space is required since length3 == length1 + length2 is invariant.