02.输入一个链表,从尾到头打印链表每个节点的值

/**
*    public class ListNode {
*        int val;
*        ListNode next = null;
*
*        ListNode(int val) {
*            this.val = val;
*        }
*    }
*
*/
import java.util.ArrayList;
public class Solution {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        if(listNode == null) return list;
     //倒序输出,可以先将链表反序,再添加 ListNode pre , now ,temp; pre
= listNode; now = pre.next; pre.next = null; while(now != null){ temp = now.next; now.next = pre; pre = now; now = temp; } while(pre != null){ list.add(pre.val); pre = pre.next; } return list; } }

 

posted @ 2016-08-02 11:17  桃源仙居  阅读(233)  评论(0)    收藏  举报