鍍金池/ 問(wèn)答/人工智能  C  網(wǎng)絡(luò)安全/ Palindrome Linked List 在Leetcode上run可以過(guò),

Palindrome Linked List 在Leetcode上run可以過(guò),但是submit過(guò)不了

問(wèn)題是: 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] 測(cè)試是可以過(guò)的.為什么呢?
我剛開(kāi)始看數(shù)據(jù)結(jié)構(gòu),不是很懂,求大神指教.謝謝了.

回答
編輯回答
任她鬧

估計(jì)是static惹的禍。它會(huì)一直保存每一次測(cè)試用例的結(jié)果,并不會(huì)重新開(kāi)始計(jì)數(shù)。所以你直接對(duì)那個(gè)失敗的測(cè)試用例可以過(guò),而對(duì)多個(gè)測(cè)試用例(也就是commit)過(guò)不了

2018年8月19日 13:38