剑指offer 反向打印链表 java代码

题目描述
输入一个链表,从尾到头打印链表每个节点的值。
这里采用栈进行辅助反向打印`

public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {

        Stack<Integer> stack = new Stack<>();
        while (listNode != null) {
            stack.push(listNode.val);

            listNode = listNode.next;

        }
        ArrayList<Integer> arrayList = new ArrayList<>();
        while (!stack.isEmpty())
            arrayList.add(stack.pop());
        return arrayList;
    }

“`

猜你喜欢

转载自blog.csdn.net/chance00/article/details/80216209