One pass java solution


  • 0
    J
        public ListNode reverseBetween(ListNode head, int m, int n) {
        if(head==null || head.next==null || m==n) return head;
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode pre = dummy;
        ListNode start =head;
        ListNode cur = dummy;
        ListNode next = head;
        
        ListNode q = null;//next.next;
        int count =1;
        while(count<=n){
             q = next.next;
            if(m==count){
                pre = cur;
                start = next;
            }else if(count>m){
                next.next = cur;
            }
             cur = next;
             next = q;
            count++;
        }
        pre.next = cur;
        start.next = next;
        return dummy.next;
    }

Log in to reply
 

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