使用递归反转链表数组

 

 

 

调用 insteadList  反转数组

    public Node<U> reverseList(Node<U> head) {
        if(head == null ) return null;
        Node<U> new_head = reverseList(head.next);
        if(new_head != null) {
            new_head.next = head;
        }else {
            this.first = head;
        }
        return head;
    }
    public void insteadList() {
        Node temp = first; //使不至于被销毁,反转后temp是最后一个
        Node<U> uNode = reverseList(first);
        temp.next =null; //反转后temp是最后一个,它的next应为空,否则遍历懂的
        temp = null;
        System.out.println("反转完成");
    }

 

 

  

 

posted @ 2021-09-17 08:58  小庄的blog  阅读(61)  评论(0编辑  收藏  举报