单链表逆转,用三个指针实现,但是在链表为空时,显示段错误,为什么,该怎么改呢?
struct ListNode *reverse( struct ListNode *head )
{
struct ListNode *p ,*q, *h;
h=head;
q=p=head->next;
h->next=NULL;
if(head!=NULL)
{
while(p != NULL) {
q=q->next;
p->next = h;
h=p;
p = q;
}
return h;
}
else
return head;
}