输入:head = [1,3,2]
输出:[2,3,1]
class Solution {
public int[] reversePrint(ListNode head) {
Stack<Integer> stack = new Stack<Integer>();
int len = 0;
while (head!=null) {
stack.push(head.val);
head = head.next;
len++;
}
int[] res = new int[len];
int i = 0;
while (!stack.empty()) {
res[i] = stack.pop();
i++;
}
return res;
}
}
// 栈
class Solution {
fun reversePrint(head: ListNode?): IntArray {
var head = head
val stack = Stack<Int>()
var len = 0
while (head != null) {
stack.push(head!!.`val`)
head = head!!.next
len++
}
val res = IntArray(len)
var i = 0
while (!stack.empty()) {
res[i++] = stack.pop()
}
return res
}
}
// 递归
class Solution {
ArrayList<Integer> tmp = new ArrayList<Integer>();
public int[] reversePrint(ListNode head) {
recursion(head);
int[] res = new int[tmp.size()];
for (int i=0;i<res.length;i++) {
res[i] = tmp.get(i);
}
return res;
}
// 递归的从尾到头添加结点值到tmp
// 每访问一个节点的时候,先递归添加它后面的结点,再添加它本身
public void recursion(ListNode head) {
if(head == null)
return;
recursion(head.next);
tmp.add(head.val);
}
}