Sword Finger Offer Interview Question 06.リンクされたリストを端から端まで印刷する

タイトル

https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/

アイデア

リンクされたリストを配列にトラバースし、次にリバースして返す

コード

 /**
     * 
     * @date: 2020-03-10 22:00
     * @param: * @param head:  
     * @return: * @return: int[]
     * @author: wwh 
     * @Description:  2020年03月10日21:50:33  -2020年03月10日22:00:48
     * 面试题06. 从尾到头打印链表
     * https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/
     *  思路:遍历链表到list  然后翻转后返回数组即可
     */       
    public   int[] reversePrint(ListNode head) {
        if(head==null){
            return  new int[0];
        }
        List<Integer> temp= new ArrayList<>();
        while (head!=null){
            temp.add(head.val);
            head = head.next;
        }
        int[] arr = new int[temp.size()];
        for (int i1 = temp.size()-1 ; i1 >=0 ; i1--) {
            arr[temp.size()-1-i1] = temp.get(i1);
        }
        return arr;
    }
元の記事を33件公開しました 賞賛されました37 再生回数110,000回

おすすめ

転載: blog.csdn.net/hagle_wang/article/details/104881230