gpt4 book ai didi

java - 从头部移除链表

转载 作者:行者123 更新时间:2023-12-04 21:27:49 25 4
gpt4 key购买 nike

<分区>

在只有一个节点情况的链表中,我尝试使用 reoveFromHead() 将其删除;我在 toString() 方法中得到一个空指针。

public void removeFromHead(){
if(head==null)
return;

else{
head=head.next;
}
}

public String toString() {
String result = " ";
ListNode a=head;

while (a.next!=null){
result +=" "+a.item;
a=a.next;
}
result+=" "+a.item;
return result;
}

谁能指出错误在哪里?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com