class Solution { public String convert(String s, int numRows) { if (numRows == 1) return s; List rows = new ArrayList<>(); int currentRow = 0; boolean direction = false; for (char c : s.toCharArray()) { if (rows.size() == currentRow) { rows.add(new StringBuilder()); } rows.get(currentRow).append(c); if (currentRow == 0 || currentRow == numRows - 1) { direction = !direction; } currentRow += direction ? 1 : -1; } StringBuilder result = new StringBuilder(); for (StringBuilder row : rows) { result.append(row); } return result.toString(); } }