ZigZag conversion solvedwith O(1) memory and O(n) time