反转链表
反转链表
https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=196&&tqId=37111&rp=1&ru=/activity/oj&qru=/ta/job-code-total/question-ranking
第一种:递归
public ListNode ReverseList(ListNode head) {
        if(head==null ||head.next==null) return head;
        ListNode sec=ReverseList(head.next);
        head.next.next=head;
        head.next=null;
        return sec;
    }第二种:循环(头插法)
public ListNode ReverseList(ListNode head) {
        if(head==null ||head.next==null) return head;
        ListNode pre=new ListNode(0);
        pre.next=null;
        while(head!=null){
            ListNode temp =head.next;
            head.next=pre.next;
            pre.next=head;
            head=temp;
        }
        return pre.next;
    }
 智元机器人成长空间 174人发布
智元机器人成长空间 174人发布