簡體   English   中英

刪除 LinkedList 的最后一個元素的問題

[英]Problem with deleting the last element of a LinkedList

我正在嘗試在LinkedList中實現delete()方法,這是鏈表數據結構的自定義實現。

如果您刪除第一個元素或中間的一個元素,以下代碼似乎可以正常工作。 但是如果我試圖刪除LinkedList中的最后一個元素,它會拋出NullPointerException

我該如何解決這個問題?

我的代碼:

public class LinkedList implements List {
    Node first;
    Node last;
    int size;
    
    public LinkedList() {
        this.first = null;
        this.last = null;
        this.size = 0;
    }
    
    public void add(double element) {
        Node newNode = new Node(element);
        
        if (first == null) {
            first = newNode;
            last = newNode;
        } else {
            last.next = newNode;
            last = newNode;
        }
        size++;
    }
    
    // more methods (unrelated to the problem)
    
    public void delete(double element) {
        if (first == null) {
            return;
        }
        if (first.value == element) {
            first = first.next;
            return;
        }
        
        Node current = first;
        Node previous = null;
        
        while (current != null && current.next.value != element) {
            previous = current;
            current = current.next;
        }
        
        if (current == null) {
            return;
        } else {
            current.next = current.next.next;
        }
    }

    @Override
    public String toString() { 
        if (first == null) {
            return "[]";
        } else {
            StringBuffer sb = new StringBuffer();
            sb.append("[");
            for (Node current = first; current != last; current = current.next) {
                sb.append(current.value);
                sb.append(",");
            }
            sb.append(last.value);
            sb.append("]");
            return sb.toString();
        }
    }
}
class LinkedListTest {
    
    public static void main(String[] args) {
        
        LinkedList list = new LinkedList();
        
        list.add(5);
        list.add(25);
        list.add(-7);
        list.add(80);
        
        System.out.println(list);
        list.delete(80);
        System.out.println(list);
    }
}

delete()方法的實現有兩個問題:

  • while循環中的條件不正確。 相反,檢查current != null是否需要確保current.next不為null請注意,訪問currentnext屬性是安全的,因為迭代從將current分配給first開始,這被證明是非 null )。

  • 當要刪除的節點是最后一個節點時,您沒有考慮邊緣情況。 在這種情況下,當控制從while循環中跳出時, current.next將指向last節點,這意味着應該重新分配last字段(因為我們要刪除最后一個節點)。

此外,局部變量previous是多余的。

public void delete(double element) {
    if (first == null) {
        return;
    }
    if (first.value == element) {
        first = first.next;
        return;
    }
    
    Node current = first;
    
    while (current.next != null && current.next.value != element) {
        current = current.next;
    }
    
    if (current.next == last) { // reassign last if the Node to be removed is the current last Node
        last = current;
    }
    
    if (current.next != null) { // if the Node with given value was found
        current.next = current.next.next;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM