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; } }
http://www.cnblogs.com/makexu/

浙公网安备 33010602011771号