下面展示下 错误代码
。
// 错误是在这里head->next==NULL
struct ListNode* oddEvenList(struct ListNode* head){
if(head->next==NULL){
return head;
}
为什么会提示runtime error: member access within null pointer of type ‘struct ListNode’?
明明我没有访问空指针,为什么说我访问空指针?
当我把
head->next==NULL改成
head == NULL
就解决问题了
原因应该是运行时,先访问了head,判断了head是否为空,若head为空,则head->next就是访问了空指针,所以改成head==NULL,就避免了这种可能。