You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中双向链表指定索引节点删除的实现问题求助

双向链表指定索引删除节点的问题分析与修复

先看你提供的代码:

Node类代码

public class Node {
    Student student;
    Node next;
    Node previous;

    public Node(Student student) {
        this.student = student;
        this.next = null;
        this.previous = null;
    }

    @Override
    public String toString() {
        return student.toString();
    }
}

DoublyLinkedList类代码

public class DoublyLinkedList <T extends Comparable<T>> {
    protected Node head;
    protected Node tail;
    int size=0;

    public DoublyLinkedList() {
        this.head = null;
        this.tail = null;
    }

    public Node append(Student student) {
        Node append = new Node(student);
        if (head == null) {
            head = append;
            tail = append;
        }
        else {
            append.previous = tail;
            tail.next = append;
            tail = tail.next;
        }
        size++;
        return append;
    }

    public void delete(int location) throws IllegalArgumentException {
        Node current = head;
        int counter = 1;
        int i;
        // Exception error for cases in which there are no nodes
        if (head == null || location < 0)
            throw new IllegalArgumentException("Sorry but the DLL is NULL, please add nodes");
        // Cases in which the person wants to delete the head at index 0
        if (location == 0) {
            /* If the node that is next is not null we make that the head
             */
            if (head.next != null) {
                head.next.previous = head;
            }
        }
        for (i = 1; head != null && i < location; i++) {
            head = head.next;
        }
        if (head == null)
            head = head.next.previous;
    }
}

现有代码的核心问题

  1. 异常判断不完整且信息错误

    • 只判断了空链表和负索引,但没处理location >= size的越界情况
    • 负索引的异常信息不准确,应该区分空链表和非法索引场景
  2. 删除头节点逻辑错误

    • head.next.previous = head;这行逻辑完全错误,正确操作是把新头节点的previous置为null,同时更新head指针;还要处理链表只剩一个节点的情况(删除后head和tail都要置为null)
  3. 遍历逻辑严重错误

    • 用head变量直接遍历,会破坏链表的头指针,导致后续所有操作失效
    • 循环条件和遍历逻辑混乱,没有正确定位到目标节点
    • 最后if (head == null)的代码会触发空指针异常,因为head为null时head.next根本不存在
  4. 未处理删除尾节点的情况

    • 删除最后一个节点时,需要更新tail指针为前一个节点,同时把新tail的next置为null
  5. 删除后未更新size

    • 删除节点后size变量没有递减,会导致节点计数和实际数量不符

修复后的delete方法实现

public void delete(int location) throws IllegalArgumentException {
    // 先处理所有非法输入
    if (head == null) {
        throw new IllegalArgumentException("链表为空,无法删除节点");
    }
    if (location < 0 || location >= size) {
        throw new IllegalArgumentException("索引非法,有效范围是0到" + (size - 1));
    }

    Node current = head;
    // 情况1:删除头节点
    if (location == 0) {
        head = head.next;
        // 删除后还有节点,重置新头的previous
        if (head != null) {
            head.previous = null;
        } else {
            // 删除后链表为空,tail也要置空
            tail = null;
        }
        size--;
        return;
    }

    // 情况2:删除尾节点
    if (location == size - 1) {
        current = tail;
        tail = tail.previous;
        tail.next = null;
        size--;
        return;
    }

    // 情况3:删除中间节点
    // 遍历到目标节点
    for (int i = 0; i < location; i++) {
        current = current.next;
    }
    // 重定向前后节点的引用
    current.previous.next = current.next;
    current.next.previous = current.previous;
    size--;
}

修复说明

  • 先统一拦截所有非法输入,避免后续逻辑出问题
  • 分三种场景处理删除:头节点、尾节点、中间节点,逻辑更清晰
  • 用current节点遍历,不会破坏原有的head和tail指针
  • 每次删除后更新size变量,保证计数准确
  • 处理了删除后链表为空的边界情况

内容的提问来源于stack exchange,提问作者user20445910

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 21:25:24