
class Solution { public: ListNode* swapPairs(ListNode* head) { if(head==NULL||head->next==NULL) //为什么这里去掉后面对 head->next 的判断提交会报错啊 return head; ListNode* temp=head->next; head->next=swapPairs(head->next->next); temp->next=head; return temp; } }; 错误如下:
Runtime Error Message: Line 16: Char 29: runtime error: member access within null pointer of type 'struct ListNode' (solution.cpp) 判断head是空的话,直接return不就行了吗,感觉不用再判断head->next了吧
1 jyyx 2020-01-11 13:38:35 +08:00 head->next=swapPairs(head->next->next); |
2 jyyx 2020-01-11 13:40:34 +08:00 head->next=swapPairs(head->next->next); 如果没有上面的保证 null- >next, 这里肯定不行啊 |
3 geminikingfall 2020-01-11 18:22:36 +08:00 你画个图就知道了,递归最后肯定是最后一个非空节点 |
4 alexsunxl 2020-01-11 21:57:16 +08:00 运行的测试集是可以看到的。 但提交阶段,会有隐藏的测试集, 是很多边界条件的测试。 没考虑好经常都会这样的。 |