链表逆置,提示说发生了段错误,请问怎么改

img


反转一个单链表,返回新链表的头结点,要求时间复杂度为O(n),空间复杂度为O(1).
想问

img

带固定头结点的链表吗?
这个函数没啥问题,是不是前面生成链表的代码有问题啊


/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* reverseList(struct ListNode* head){
    struct ListNode* cur = head;
    struct ListNode* p = NULL;
    while(cur)
    {   
        struct ListNode* pre = cur->next;
        cur->next = p;
        p = cur;
        cur = pre;
    }
    return p;



}