/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ publicclassSolution{ public ListNode reverseBetween(ListNode head, int m, int n){ if(head == null) returnnull; int tempA = -1; ListNode pre = null; ListNode begin = null; ListNode end = null; ListNode before = null;
ListNode fakeHead = new ListNode(0); fakeHead.next = head; ListNode p = fakeHead;
while(p != null){ tempA++; if(tempA < m-1){ p = p.next; } if((tempA+1) == m) { pre = p; before = p; p = p.next; } if(tempA == m) { begin = p; } if(m <= tempA && tempA <= n){ ListNode next = p.next; p.next = pre; pre = p; if(tempA == n){ end = p; } p = next; } if(tempA == n){ begin.next = p;