試圖編寫一個方法,該方法從單向鏈表中刪除值的所有實例,但似乎不起作用。單鏈接列表:正在刪除
我試圖適應的頭部是否包含的價值,但我不知道這是否是這樣做的正確方法:
public void remove (int value)
{
if (head.value == value)
{
head = head.next;
count--;
}
IntegerNode temp=head;
while (temp !=null)
{
if (temp.next != null)
{
if (temp.next.value == value)
{
temp.next = temp.next.next;
count--;
}
}
temp=temp.next;
}
}
有什麼明顯的毛病我碼?
什麼問題exacly,爲什麼你正在使用count-- –
的統計顯示多少個值在列表中。 – user3283585
把if語句放在while循環之後 –