Reverse list Series

92. Reverse list II

Reversed from the position  m  to  n  list. Please use the trip to the scan is complete reversal.

Description:
. 1 ≤  m  ≤  n-  ≤ chain length.

Example:

Input: l-> 2-> 3-> 4-> 5-> NULL, m = 2, n- =. 4 
Output: 1-> 4-> 3-> 2- > 5-> NULL
public class T92 {
    public ListNode reverseBetween(ListNode head, int m, int n) {
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode preNode = dummy;
        //m - 1 步停下,即为 preNode
        for (int i = 0; i < m - 1; i++) {
            preNode = preNode.next;
        }
        ListNode curNode = preNode.next;
        ListNode node = null;
        for (int i = 0; i < n - m + 1; i++) {
            ListNode temp = curNode.next;
            curNode.next = node;
            node = curNode;
            curNode = temp;
        }
        preNode.next.next = curNode;
        preNode.next = node;
        return dummy.next;
    }
}

 

Guess you like

Origin www.cnblogs.com/zzytxl/p/12529096.html