Palindrome Linked List 在Leetcode上run可以过,但是submit过不了

问题是: 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] 测试是可以过的.为什么呢?
我刚开始看数据结构,不是很懂,求大神指教.谢谢了.

阅读 3.6k
1 个回答

估计是static惹的祸。它会一直保存每一次测试用例的结果,并不会重新开始计数。所以你直接对那个失败的测试用例可以过,而对多个测试用例(也就是commit)过不了

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题