[英]Deleting a Node at a given position in a Doubly Linked List
我試圖刪除給定索引處的節點,但我一直在測試失敗,有時甚至會得到NullPointerException
。 這是我到目前為止所做的:
public void remove(int pos)
{
if (head == last)
{
head = null;
last = null;
}
if (pos == size - 1)
return removeLast();
if (pos == 0)
return removeFirst();
else
{
DoubleListNode<E> current = head;
for (int i = 0; i < pos - 1; i ++)
{
current = current.getNext();
}
current.setNext(current.getNext().getNext());
current.getNext().getNext().setPrev(current);
return current.getData();
}
}
從雙向鏈表中刪除的步驟:
讓要刪除的節點為del。
記住,在這里您必須同時維護上一個和下一個指針,同時從兩側處理null條件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.