Accepted java solution in a unique way


  • 0
    L
    public ListNode reverseBetween(ListNode head, int m, int n) {
            ListNode dummy=new ListNode(0), w=dummy, r=dummy;
            dummy.next = head;
            while(m>1){
            	w = w.next;
            	m--;
            }
            while(n>0){
            	r = r.next;
            	n--;
            }
            ListNode piv = w.next, pre = r.next;
            w.next = r;
            while(piv!=r){
            	ListNode temp = piv.next;
            	piv.next = pre;
            	pre = piv;
            	piv = temp;
            }
            piv.next = pre;
            return dummy.next;
        }
    

Log in to reply
 

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