1 public static ListNode reverseBetween(ListNode head, int m, int n) { 2 ListNode pre=head,current=head,mPre = new ListNode(0),mNode = new ListNode(0),nNode = new ListNode(0),temp; 3 mPre.next=head; 4 int i=1; 5 while(i<=n) 6 { 7 if(i==m-1) 8 mPre=current; 9 if(i==m) 10 mNode=current; 11 if(i==n) 12 nNode=current; 13 if(m<i&&i<=n) 14 { 15 temp=current; 16 current=current.next; 17 temp.next=pre; 18 pre=temp; 19 } 20 else{ 21 pre=current; 22 current=current.next; 23 } 24 i++; 25 } 26 mPre.next=nNode; 27 mNode.next=current; 28 if(m==1) 29 return nNode; 30 else 31 return head; 32 33 }
时间: 2024-10-26 14:36:59