Very clear Java solution


  • 0
    M
    public ListNode reverseBetween(ListNode head, int m, int n) {
    	ListNode tmp = head;
    	ListNode begin = null;
    	int[] arr = new int[n + 1 - m];
    	for (int i = 1; i < n + 1; i++) {
    		if (i >= m) {
    			arr[i - m] = tmp.val;
    			begin = i == m ? tmp : begin;
    		}
    		tmp = tmp.next;
    	}
    	for (int i = 0; i < arr.length; i++) {
    		begin.val = arr[arr.length - i - 1];
    		begin = begin.next;
    	}
    	return head;
    }
    

Log in to reply
 

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