//采用双指针,循环遍历
import java.util.*;
/*
-
public class ListNode {
-
int val;
-
ListNode next = null;
-
} */
public class Solution { public ListNode reverseBetween (ListNode head, int m, int n) { // write code here if (head == null) { return null; } int len = n - m; ListNode right = head; for (int i = 0; i < len; i++) { right = right.next; } ListNode pre = new ListNode(-1); pre.next = head; ListNode preLeft = pre; for (int i = 1; i < m; i++) { preLeft = preLeft.next; right = right.next; } ListNode left = preLeft.next; ListNode aftLeft = left.next; preLeft.next = right; left.next = right.next; while (left != right ) { preLeft = left; left = aftLeft; aftLeft = left.next; left.next = preLeft ; } return pre.next;
}
}