import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * } */ public class Solution { /** * * @param head ListNode类 * @param m int整型 * @param n int整型 * @return ListNode类 */ public ListNode reverseBetween (ListNode head, int m, int n) { // write code here if(head.next==null){ return head; } if(m==n){ return head; } ListNode start =head; int size=0; while(start!=null){ start=start.next; size++; } start=head; ListNode end1 =head; for(int i=1;i<m-1;i++){ start=start.next; } for(int i=1;i<n;i++){ end1=end1.next; } ListNode start1=start.next; ListNode end2=end1.next; if(m==1){ start1=head; } ListNode cur =start1.next; ListNode curNext=cur.next; ListNode h=start1; h.next=null; end1.next=null; while(cur!=null){ curNext=cur.next; cur.next=h; h=cur; cur=curNext; } if(m==1){ if(n==size){ return end1; }else{ start1.next=end2; return end1; } } if(n==size){ start.next=end1; return head; } start.next=end1; start1.next=end2; return head; } }