Share my 0ms java solution


  • 0
    C
    public ListNode reverseBetween(ListNode head, int m, int n) {
        ListNode dummy = new ListNode(0), dummyRear, p = dummy;
        dummy.next = head;
        for(int i=1; i<m; p=p.next, i++);
        dummyRear = p;
        p = p.next;
        ListNode swapRear = p, pre = null, next = p.next;
        for(int i=m; i<=n; i++){
            p.next = pre;
            pre = p;
            p=next;
            next = next==null? null: next.next;
        }
        dummyRear.next = pre;
        swapRear.next = p;
        return dummy.next;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.