链表内指定区间反转的未知错误


struct ListNode* reverseBetween(struct ListNode* head, int m, int n ) {
    // write code here
    if(head==NULL)return head;
    if(head->next==NULL) return head;
    int rp=2;
    struct ListNode*ahead=head,*last;
    struct ListNode*p,*nhead,*nlast;
    while(rp head=head->next;
        last=head;
    }
    head=head->next;
    nlast=nhead=head;
    head=head->next;
    for(int i=0;ival=head->val;
        p->next=nhead;
        nhead=p;
        head=head->next;
    }
    
    nlast->next=head;
    last->next=nhead;
    return ahead;

}

牛客网BM2
程序段错误,不知道哪错
思路:先找到m端点,将m-n区间内链表节点头插法插入新创建的链表,再将三段链表链接起来

这代码看着就害怕,所有指针都不检查是否为空的啊,门户大开