1. 程式人生 > >LeetCodeBug-member access within null pointer of type 'struct ListNode'

LeetCodeBug-member access within null pointer of type 'struct ListNode'

寫在前面

這個BUG是我在做LeetCode的連結串列一類題目的時候遇到的,覺得還是蠻有代表性的,因為剛開始遇到這個BUG,我完全不知道哪裡有問題,還與正確答案反覆對比,結果發現完全一樣,但我的就報錯,後來發現原來是這個知識點完全沒有接觸過,所以就通過這篇部落格來記錄一下自己犯得錯誤,來提醒自己。

錯誤提示

這裡寫圖片描述

錯誤原因

錯誤的含義:型別為“struct ListNode”的空指標內的成員訪問。

根據:LEETCODE 中的member access within null pointer of type ‘struct ListNode’112. Path Sum ,其實不難理解,這個問題主要是由於測試系統不知道當前的結點與結點的下一個結點是否為NULL,而自己有沒有宣告,所以系統不確定,於是就報錯。這也說明了連結串列中的結點使用的時候比其他資料結構要嚴格一些,一定要時刻保證使用的結點不為NULL,為NULL要做出相應處理。

解決方法

剛開始按照別人的解決方法依葫蘆畫瓢做了一個判別語句:

while(second != NULL && second->next != NULL && first != NULL){
    ...
}

但是依葫蘆畫瓢的都不是特准確,所以還是要好好理解。比如,在我使用:

ListNode *second=head->next;

這個語句語義上沒有問題,但是要確定head與head->next均不為NULL,所以就新增下面的語句即可:

while(head==NULL||head->next==NULL
) return head;

相關推薦

LeetCodeBug-member access within null pointer of type 'struct ListNode'

寫在前面 這個BUG是我在做LeetCode的連結串列一類題目的時候遇到的,覺得還是蠻有代表性的,因為剛開始遇到這個BUG,我完全不知道哪裡有問題,還與正確答案反覆對比,結果發現完全一樣,但我的就報錯,後來發現原來是這個知識點完全沒有接觸過,所以就通過這篇部落

LeetCode最長字首問題reference binding to null pointer of type 'struct value_type'

在刷LeedCode的求最長字首問題時遇到如下問題: 直覺上就是訪問陣列越界,但是一直找不出問題。出問題的程式碼如下: class Solution { public: string longestCommonPrefix(vector<string&g

LeetCode最長字首問題reference binding to null pointer of type 'struct value_type'

在刷LeedCode的求最長字首問題時遇到如下問題: 直覺上就是訪問陣列越界,但是一直找不出問題。出問題的程式碼如下: class Solution { public: string longestCommonPrefix(vector<string>&

LeetCode中問題1.reference binding to null pointer of type 'const value_type'

問題1:reference binding to null pointer of type 'const value_type' 在LeetCode做題的過程中,遇到"reference binding to null pointer of type ‘value_type

【轉】leetcode錯誤提示:member access within misaligned address 0x000000000031 for type 'struct ListNode'

錯誤提示: member access within misaligned address 0x000000000031 for type 'struct ListNode', which requires 8 byte alignment 原因分析: 在連結串列中

void type && void pointer(void *,pointer to void type) && Null pointer

void type void pointer(void *,pointer to void type) 1 ANSI C和C ++支援void指標或void *作為通用指標型別。 指向void的指標可

Dapper:The member of type SeoTKD cannot be used as a parameter Value

這次看看新的異常: 這個的根本是因為模型欄位和資料庫不匹配導致的(好像之前是可以的) 資料庫中並沒有SeoInfo這個屬性(EF裡面導航屬性用的還是比較多的,這次換Dapper,這個肯定是少不了的,於是就折中了下,看看能不能讓Dapper忽略這個屬性) 解決方法:設定不可寫特性,writ

解決 'Could not convert variant of type (NULL) into type (String)'

使用Delphi6的朋友開發資料庫的時候可能遇到下面的情況: 使用TADOTable開啟表的時候當表為空時出現文章標題的錯誤資訊很是煩人。 當表有資料就不會出現這個錯誤了,經過多方磨難終於找到解決方法:(delphi想說愛你不容易,最近找到很多Bug) 解決方法: 1、在Ob

debug---null Pointer Exception--一步步查找(2)

image party 添加 -- 空指針異常 pointer entity oca except 添加PartyLocationRepository後,再次在Ubuntu中編譯項目,再次報空指針異常。 直接在createDto處打斷點,然後debug每個表達式的值,找出

CSS3::nth-child與:nth-of-type區別

css3選擇器 block lan strong title 選擇 示例 紅色 arc 一、:nth-child 1.1 說明 :nth-child(n)選擇器匹配屬於其父元素的第N個子元素,不論元素的類型。n可以是數字、關鍵詞或公式。 註意:如果第N個子元素

CSS3選擇器:nth-child與:nth-of-type區別

html css css3 一、:nth-child 1.1 說明 :nth-child(n)選擇器匹配屬於其父元素的第N個子元素,不論元素的類型。n可以是數字、關鍵詞或公式。 註意:如果第N個子元素與選擇的元素類型不同則樣

偽類選擇器:first-child和:nth-child()和:first-of-type

head true http doctype 段落 元素 選擇 ont span x:first-child和x:nth-child(1)功能一樣,首先選中的是x元素,並且x元素必須是它父元素的第一個子元素,選擇器才成立,否則不能選中。其中x也可以是選擇器。由此看出nth-

nth-child,nth-last-child,only-child,nth-of-type,nth-last-of-type,only-of-type,first-of-type,last-of-type,first-child,last-child偽類區別和用法

type nth 只有一個 not 同時 選擇器 -type span 進行 我將這坨偽類分成三組,第一組:nth-child,nth-last-child,only-child第二組:nth-of-type,nth-last-of-type,第三組:first-of-tp

swagger ui js 錯誤:Failed to execute 'serializeToString' on 'XMLSerializer': parameter 1 is not of type 'Node'.

排除法 解決 swa set ring param execute 錯誤 exec 經過排查,引發此錯誤的原因是,表中有一個字段名稱為“NodeName”,應該是在前臺xml解析時引發沖突所致。我的解決辦法是: 修改列名,修改映射。 如下: [Column("NodeNa

You (root) are not allowed to access to (crontab) because of pam configuration

作業 cnblogs led 賬號密碼 pad .so normal access new 巡檢發現一臺Linux服務器上的作業沒有如期發送郵件,登錄服務器檢查後發現作業並沒有執行,於是檢查一下crontab的設置。結果發現如下錯誤: [[email pr

Android java.lang.NoSuchFieldError: No static field xxx of type I in class Lcom/XX/R$id; or its superclasses

activity oid 返回 反射 分享 -c lar 進行 是否 項目開發快到尾聲,突然發現之前一個模塊莫名其妙的奔潰了,我的內心也是奔潰的。以前一直都是好好的,也沒去動過它,為啥會出現這樣的問題呢? 下面我會根據自己的理解來看待問題 android是怎麽根據id查找

調用tensorflow中的concat方法時Expected int32, got list containing Tensors of type '_Message' instead.

lin list 代碼 con comm decode tmp class got grid = tf.concat(0, [x_t_flat, y_t_flat, ones])#報錯語句 grid = tf.concat( [x_t_flat, y_t_flat, one

170616、解決 java.lang.IllegalArgumentException: No converter found for return value of type: class java.util.ArrayList

pen group string image exception bean val 轉換 技術 報錯截圖: 原因:搭建項目的時候,springmvc默認是沒有對象轉換成json的轉換器的,需要手動添加jackson依賴。 解決步驟: 1、添加jackson依賴到pom

8.21 :odd??:nth-of-type??

css3 原因 har 實現 csharp 時間 js代碼 效果 logs 今天為了實現隔行變色,我在css裏寫: .note:odd{ background-color: #eee; } 有一個頁面有效果,另一個頁面沒效果,怎麽也找不到原因。。。各種嘗試各種清緩存

CSS常用選擇器記錄-:nth-of-type(n)

center pan col eight 記錄 logs round css 子元素 :nth-of-type(n) 選擇器匹配屬於父元素的特定類型的第 N 個子元素的每個元素. n 可以是數字、關鍵詞或公式。 .step_icon3 li.item{ wid