zoukankan      html  css  js  c++  java
  • 【剑指offer】逆序输出链表

    输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。
    *考察栈的使用
    *使用循环输出Stack中内容的时候,不能使用for(int i; i<stack.size();i++)因为stack.size()在数据出栈操作后会变化。
    /**
    *    public class ListNode {
    *        int val;
    *        ListNode next = null;
    *
    *        ListNode(int val) {
    *            this.val = val;
    *        }
    *    }
    *
    */
    import java.util.ArrayList;
    import java.util.Stack;
    public class Solution {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
            ArrayList<Integer> result = new ArrayList<Integer>();
            Stack<Integer> stack = new Stack<Integer>();
            while(listNode!=null){
                stack.push(listNode.val);
                listNode = listNode.next;
            }
           while(stack.size()!=0){
                result.add(stack.pop());
            }
                
            return result;
        }
    }
  • 相关阅读:
    c++ *.h和*.cpp在编译中的作用
    test
    DOM Tree
    SecureCRT
    趣味盒1
    数据结构笔试:前缀表达式|后缀表达式
    Shell 文件包含
    Shell 输入/输出重定向
    Shell 函数
    Shell 流程控制
  • 原文地址:https://www.cnblogs.com/singular/p/10015251.html
Copyright © 2011-2022 走看看