问题是: Given a singly linked list, determine if it is a palindrome.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
bool isPalindrome(struct ListNode* head) {
if(head==NULL||head->next==NULL) {
return true;
}
static int count;
struct ListNode *p = head;
while(p){
count++;
p=p->next;
}
int i;
int s[count/2];
p=head;
for(i=0;i<count/2;i++){
s[i]=p->val;
p=p->next;
}
i--;
if(count%2==1) {
p=p->next;
}
while(p!=NULL&&s[i]==p->val){
i--;
p=p->next;
}
if(i==-1){
return true;
}
else{
return false;
}
}
Runtime Error Message:
Line 22: member access within null pointer of type 'struct ListNode'
Last executed input:[-129,-129]
但是用 run 的话,用 [-129, -129]
测试是可以过的.为什么呢?
我刚开始看数据结构,不是很懂,求大神指教.谢谢了.
估计是static惹的祸。它会一直保存每一次测试用例的结果,并不会重新开始计数。所以你直接对那个失败的测试用例可以过,而对多个测试用例(也就是commit)过不了