获取单链表中节点的个数,不算头节点: public static int getLength(HeroNode head){ if(head.next == null){//空节点 return 0; } int length = 0; //辅助变量 HeroNode cur = head.next; int sum = 0; while(cur!=null).
摘要由CSDN通过智能技术生成
获取单链表中节点的个数,不算头节点:这个题比较简单,只需要遍历一遍然后计数就可以了
public static int getLength(HeroNode head){
if(head.next == null){//空节点
return 0;
}
int length = 0;
//辅助变量
HeroNode cur = head.next;
int sum = 0;
while(cur!=null){
sum++;
cur = cur.next;
}
return sum;
}
获取单链表中节点的个数,不算头节点: public static int getLength(HeroNode head){ if(head.next == null){//空节点 return 0; } int length = 0; //辅助变量 HeroNode cur = head.next; int sum = 0; while(cur!=null).