[英]Need help. This code removes everything except at index 0
下面的代碼用於從單向鏈表中刪除一個對象,索引為 0 除外。我需要更改什么?
Node<E> current = head;
int currentIndex = 0;
while (current.next != null) {
if ((currentIndex + 1) == index) {
Node<E> newNext = current.next.next;
current.setNext(newNext);
size--;
return true;
}
current = current.next;
currentIndex++;
}
對於索引 0,條件(currentIndex + 1 == index)
將為假。在這種情況下,您可以檢查索引是否等於 0 並移動頭:
if (index == 0) {
head = head.next;
size--;
return true;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.