剑指offer——面试题5:从尾到头打印链表

package offer;

import java.util.Stack;

/**
 * 输入一个链表的头结点,从尾到头反过来打印出每一个节点的值
 */
public class Problem05 {

	public static void main(String[] args) {
		LinkList list = new LinkList();
		list.head = new Node(1);
		list.head.next = new Node(2);
		list.head.next.next = new Node(3);
		Stack<Node> stack = new Stack<>();//重点:利用栈的特性
		Node current = list.head;
		while (current != null) {
			stack.push(current);
			current = current.next;
		}
		while (!stack.isEmpty()) {
			System.out.print("-->" + stack.pop().data);
		}
	}

}

class LinkList {
	Node head;
}

class Node {
	public double data;
	public Node next;

	public Node(double data) {
		this.data = data;
	}
}
相关文章
相关标签/搜索