标签:示例 print int help return for node 输入 oid
输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。
示例 1:
输入:head = [1,3,2]
输出:[2,3,1]
限制:
0 <= 链表长度 <= 10000
class Solution {
ArrayList<Integer> arr = new ArrayList<>();
public int[] reversePrint(ListNode head) {
helper(head);
int[] ans = new int[arr.size()];
for(int i = 0; i < arr.size(); i++){
ans[i] = arr.get(i);
}
return ans;
}
public void helper(ListNode head){
if(head == null) return;
helper(head.next);
arr.add(head.val);
}
}
标签:示例 print int help return for node 输入 oid
原文地址:https://www.cnblogs.com/whisperbb/p/12885461.html