https://www.nowcoder.com/practice/d0267f7f55b3412ba93bd35cfa8e8035
    思路:遍历链表将每个节点的值存入数组中,然后将数组反转返回。
    学习一下如何定义链表!

    1. public class print_linked_list_from_end_to_end {
    2. public static class ListNode {
    3. int val;
    4. ListNode next = null;
    5. ListNode(int val) {
    6. this.val = val;
    7. }
    8. }
    9. public static void main(String[] args) {
    10. ListNode node1 = new ListNode(1);
    11. ListNode node2 = new ListNode(2);
    12. ListNode node3 = new ListNode(3);
    13. node1.next = node2;
    14. node2.next = node3;
    15. ArrayList arrayList = printListFromTailToHead(node1);
    16. System.out.println(arrayList);
    17. }
    18. public static ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
    19. ArrayList arrayList = new ArrayList();
    20. while (listNode != null) {
    21. arrayList.add(listNode.val);
    22. listNode = listNode.next;
    23. }
    24. Collections.reverse(arrayList);
    25. return arrayList;
    26. }
    27. }