思路
使用栈,先遍历链表,把每个节点加入到Stack,然后再pop()依次打印出来。
public List<Node> reversePrintList(Node node){
List<Node> list=new ArrayList<>();
if(node==null)
return list;
list.add(node);
Stack<Node> stack=new Stack<>();
while(node!=null){
stack.push(node);
node=node.next;
}
while(!stack.isEmpty()){
list.add((stack.pop());
}
return list;
}
使用递归:
public List<Node> reversePrintList(Node node){
List<Node> list=new ArrayList<>();
if(node.next!=null){
reversePrintList(node.next);
}
list.add(node);
}