1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
class Solution {
List<Integer> list;
public int[] reversePrint(ListNode head) { list = new ArrayList<>(); reverse(head); int n = list.size(); int[] ans = new int[n]; for (int i = 0; i < n; ++i) { ans[i] = list.get(i); } return ans; }
private void reverse(ListNode head) { if (head == null) return; reverse(head.next); list.add(head.val); } }
|