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

有序IntNode链表插入数字功能异常,代码错误排查求助

有序IntNode链表插入数字的代码错误排查与修正

你的代码存在几处关键问题,导致执行出错或不符合作业要求,具体问题和修正方案如下:

核心错误点

  • 空链表处理逻辑错误:当传入的链表为空时,你试图对null对象调用SetValue,直接触发空引用异常。正确做法是返回一个新创建的节点。
  • 遗漏头部插入场景:如果插入值x比链表第一个节点的值小,当前代码没有处理这种情况,会直接进入循环导致错误。
  • 未做空指针校验:循环中直接访问p.GetNext().GetValue(),但未判断p.GetNext()是否为null,遍历到链表末尾时会崩溃。
  • 插入逻辑错误:你现在是修改已有节点的值,而非插入新节点。作业要求是向链表中添加新元素,需要创建新的IntNode实例并调整指针完成插入。
  • ToString方法不完整:当前ToString仅返回单个节点内容,无法打印整个链表,无法直观验证插入结果。

修正后的完整代码

using System.Text;

public class IntNode
{
    private int value;
    private IntNode next;

    // 构造函数
    public IntNode(int value)
    {
        this.value = value;
        this.next = null;
    }

    public IntNode(int value, IntNode next)
    {
        this.value = value;
        this.next = next;
    }

    // Getter
    public int GetValue()
    {
        return this.value;
    }
    public IntNode GetNext()
    {
        return this.next;
    }

    // Setter
    public void SetValue(int value)
    {
        this.value = value;
    }
    public void SetNext(IntNode next)
    {
        this.next = next;
    }

    public bool HasNext()
    {
        return this.next != null;
    }

    // 重写ToString,遍历整个链表输出完整内容
    public override string ToString()
    {
        StringBuilder sb = new StringBuilder();
        IntNode current = this;
        while (current != null)
        {
            sb.Append(current.value);
            if (current.next != null)
            {
                sb.Append("->");
            }
            current = current.next;
        }
        return sb.ToString();
    }
}

public class LinkedListHelper
{
    public static IntNode InsertIntoSortedList(IntNode list, int x)
    {
        // 处理空链表情况
        if (list == null)
        {
            return new IntNode(x);
        }

        // 处理头部插入:x小于等于第一个节点值
        if (x <= list.GetValue())
        {
            return new IntNode(x, list);
        }

        IntNode p = list;
        // 找到插入位置:p的下一个节点值大于x,或者p是最后一个节点
        while (p.GetNext() != null && x > p.GetNext().GetValue())
        {
            p = p.GetNext();
        }

        // 创建新节点并插入到链表中
        IntNode newNode = new IntNode(x);
        newNode.SetNext(p.GetNext());
        p.SetNext(newNode);

        return list;
    }

    static void Main(string[] args)
    {
        IntNode list = new IntNode(1, new IntNode(4, new IntNode(9, new IntNode(12, new IntNode(72)))));
        Console.WriteLine("插入前链表:" + list.ToString());
        IntNode newList = InsertIntoSortedList(list, 7);
        Console.WriteLine("插入后链表:" + newList.ToString());
    }
}

修正说明

  1. 空链表处理:直接返回新节点,彻底避免空引用异常。
  2. 头部插入:新增判断逻辑,当x小于等于头节点值时,直接创建新节点作为链表头部。
  3. 循环安全校验:先判断p.GetNext()是否为null,再进行值比较,防止遍历到链表末尾时触发空指针。
  4. 正确插入逻辑:创建新节点后,调整前后节点的指针关系,完成新元素的插入,而非覆盖原有节点值。
  5. 完整ToString方法:使用StringBuilder遍历整个链表,输出完整的链表结构,方便直观验证插入结果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 14:15:26