import java.util.Stack;
/**
* 输入链表的头结点,从尾到头打印链表
* @author liu
*
*/
public class PrintListReverse {
//非递归方式
public static void printListReverse(ListNode headNode){
Stack<ListNode> stack = new Stack<>();
while(headNode != null){
stack.push(headNode);
headNode = headNode.next;
}
while(!stack.isEmpty()){
System.out.println(stack.pop().data);
}
}
//递归方式
public static void printListReverse1(ListNode headNode){
if(headNode != null){
if(headNode.next != null){
printListReverse1(headNode.next);
}
}
System.out.println(headNode.data);
}
class ListNode{
private int data;
private ListNode next;
public ListNode(int data, ListNode next){
this.data = data;
this.next = next;
}
}
}
09-06
09-06
09-06
09-06
09-06
09-06