關於連結串列的長度,head == NULL 與 head.next == NULL的區別
阿新 • • 發佈:2021-08-09
首先,求連結串列的長度需要遍歷,下面為遍歷的程式碼:
//求連結串列的長度 privateintlength(ListNodehead){ intlen=0; while(head!=null){ len++; head=head.next; } returnlen; } head == NULL 與 head.next == NULL的區別:head == NULL意思是頭指標是否為空,head.next == NULL意思是頭指標的下一個節點是否為空 舉個例子說明,例如連結串列有5個節點,[1,2,3,4,5],如果head指向1,則head->next指向2,則: head == NULL 等同於 :1 == null ,