0
点赞
收藏
分享

微信扫一扫

JAVA:剑指_Offer_06_从尾到头打印链表

高子歌 2022-03-12 阅读 71
package 剑指offer_初级;

public class 剑指_Offer_06_从尾到头打印链表 {
    public class ListNode {
      int val;
      ListNode next;
      ListNode(int x) { val = x; }
    }
    class Solution {
        public int[] reversePrint(ListNode head) {
            int a[]=new int[0];
            if(head==null){
                return a;
            }
            int length=1;
            ListNode node=head;
            while(head.next!=null){
                length++;
                head=head.next;
            }
            int[] arr=new int[length];
            while(node.next!=null){
                arr[length-1]=node.val;
                length--;
                node=node.next;
            }
            arr[0]=node.val;
            return arr;
        }
    }
}
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
举报

相关推荐

0 条评论