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;
        }
    }
  • 相关阅读:
    kill process
    USB development guide
    MMC device
    memtester
    printf()格式化输出详解
    C语言动态内存分配
    归并排序C语言
    c 文件操作
    数据包分析
    C语言文件操作函数大全
  • 原文地址:https://www.cnblogs.com/singular/p/10015251.html
Copyright © 2011-2022 走看看