辅助栈法:
class Soluction{
public int[] reseversePrint(ListNode head){
Stack<ListNode> stack = new Stack<ListNode>();
ListNode temp = head;
while(temp !=null){
stack.push(temp);
temp = temp.next;
}
int size = stack.size();
int[] print = new int[size];
for(int i = 0;i<size;i++){
print[i] = stack.pop().val;
}
return print;
}
}
递归法:
class Solution {
ArrayList<Integer> tmp = new ArrayList<Integer>();
public int[] reversePrint(ListNode head) {
recur(head);
int[] res = new int[tmp.size()];
for(int i = 0; i < res.length; i++)
res[i] = tmp.get(i);
return res;
}
void recur(ListNode head) {
if(head == null) return;
recur(head.next);
tmp.add(head.val);
}
}
作者:jyd
链接:https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/solution/mian-shi-ti-06-cong-wei-dao-tou-da-yin-lian-biao-d/
来源:力扣(LeetCode)