/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 */
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param head ListNode类 
 * @param m int整型 
 * @param n int整型 
 * @return ListNode类
 */
struct ListNode* reverseBetween(struct ListNode* head, int m, int n ) {
    // write code here
    if(head==NULL)return NULL;
    if(head->next==NULL)return head;
    if(m==n)return head;
    struct ListNode* befor,*head1,*head2,*head3,*head4,*end;
    int i,j,k;
    befor=head1=head2=head3=head4=end=head;
    for(i=1;end->next!=NULL;i++){
        end=end->next;//结束点
    }
    for(j=1;j<m;j++){
        befor=head1;//头前一个结点
        head1=head1->next;//头
    }
    for(k=1;k<n&&k<i;k++){
        head2=head2->next;//尾
    }
    head3=head1->next;
    head1->next=head2->next;
    if(head3->next!=NULL){
         head4=head3->next;
    }
   else head4=head3;
    while(head1!=head2){
        head3->next=head1;
        head1=head3;
        head3=head4;
        if(head4->next!=NULL){
            head4=head4->next;
        }
    }
    if(m==1){
        return head2;
    }
    else { 
         befor->next=head2;
        return head;}
}