在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

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

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

回答
編輯回答
任她鬧

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

2018年8月19日 13:38