算法day3-简单篇-反转链表

算法day3-简单篇-反转链表_第1张图片

采用原地逆置,采用pre来指向逆置,in指针移动,rs指针来指向原链表

    public ListNode reverseList(ListNode head) {
        if(head==null||head.next==null){
            return head;
        }
        ListNode pre = head, in = head.next, rs = in;
        pre.next = null;
        while(rs!=null){
            rs = rs.next;
            in.next = pre;
            pre = in;
            in = rs;
        }
        return pre;
    }

你可能感兴趣的:(Java,Web,链表,算法,数据结构)