1. 程式人生 > 其它 >關於連結串列的長度,head == NULL 與 head.next == NULL的區別

關於連結串列的長度,head == NULL 與 head.next == NULL的區別

首先,求連結串列的長度需要遍歷,下面為遍歷的程式碼:

 //求連結串列的長度 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 ,
head.next == NULL 等同於 :2 == null 所以上面的函式會返回 :5