输入个链表的头结点,从尾到头反过来打印出每个结点的值
public static void printListInverselyUsingIteration(ListNode root) {
Stack<ListNode> stack = new Stack<>();
while (root != null) {
stack.push(root);
root = root.nxt;
}
ListNode tmp;
while (!stack.isEmpty()) {
tmp = stack.pop();
System.out.print(tmp.val + " ");
}
}

本文介绍了一种使用迭代和栈数据结构逆序打印链表节点值的方法。通过将链表节点压入栈中,再逐个弹出并打印节点值,实现从尾到头的逆序打印。
1328

被折叠的 条评论
为什么被折叠?



