【问题标题】:How do I remove the only node in a linked list in java?如何在java中删除链表中的唯一节点?
【发布时间】:2014-07-01 07:05:25
【问题描述】:

我有一个 while 循环,它应该一直运行,直到从我的链表中删除某个值。(remainingPoints) 问题是,如果该节点是链表中的唯一节点,我的编译器会给我一个错误。编辑:我在java中使用预先编写的链表类。

    while (remainingPoints.contains(endPoint)) {

        //loop through each index of the start points adjacency array and update the path estimates.
        for (int i = 0; i < maxIndex; i++) {
            //if the path estimate is greater than the distance found from the next Point to the
            //i'th point, update the pathEstimate for that point and update the parent of the point.
            if ((pathEstimates[i] != 0 && adjMatrix[next][i] != 0) && (adjMatrix[next][i]+pathEstimates[next] < pathEstimates[i])) {
                pathEstimates[i] = adjMatrix[next][i] + pathEstimates[next];
                parents[i] = next;
            }
        }

        //reset next. 
        next = -1;

        //This will be the intersection that has the shortest path from the last tested 
        //intersection (that is not 0).
        for (int i = 0; i < maxIndex; i++) {
            if (pathEstimates[i] != 0 && remainingPoints.contains(i)) {
                if (next == -1)
                    next = i;
                else if (pathEstimates[i] < next)
                    next = i;
            }
        }

        //Inelegent solution goes here in place of the line of code below:
        remainingPoints.remove(next);

    }

我尝试的一个不优雅的解决方案是在这个 if 语句中添加一个包含 -1 的无用节点,以便可以删除包含 next 的节点,从而使 while 循环的下一次迭代为 false,但这增加了一个更奇怪的问题:

    if (remainingPoints.size() == 1)
            remainingPoints.add(-1);
    System.out.println(next);
    System.out.println(remainingPoints.remove(next));

通过这个尝试的解决方案,循环无限运行。打印的下一个值是 1(这是下一个的正确和预期值),但不知何故,剩余点的值.remove(next) 是-1。我也测试了其他值,remainingPoints.remove(next) 的值始终是我使用 if 语句添加的值。这表明 remove 方法正在删除我添加的值,这可以解释无限循环,但为什么会发生这种情况?

如果有人能解释如何在java中简单地删除链表中的唯一节点,那将不胜感激!谁能解释上述错误,谁也能得到奖励。

顺便说一句,这是我关于堆栈溢出的第一篇文章,所以如果我有任何发布错误或对任何堆栈溢出礼节一无所知,请告诉我!

【问题讨论】:

  • 为什么不只检查 head->next 是否为 null,如果是,则检查 head 中的值是否是您正在搜索的值,删除它并返回 null 或者执行什么都没有?
  • 我刚开始用 java 编码并且是一个初级程序员所以也许这是一个愚蠢的问题但是你将如何删除节点并返回 null?如果我在 C 中编码,但我在 java 中使用预先编写的链表类,这对我来说将是有用的建议,而且我没有看到任何删除链表中唯一节点的方法。
  • 哦,你没有在你的问题中提到这一点,只需将头部的内容设置为 null 就是这样。类似 list.remove(0)
  • 在 Java 中你不能删除。您只需删除对对象的引用,然后 GC 就可以释放它。
  • @damienhawks 你说得对,我应该提到这一点,我编辑了这个问题。哇,谢谢你完全解决了我的问题!虽然我不得不承认我对如何做有点困惑。为什么我能够以这种方式删除引用的节点,而不是通过删除包含我想要的值的节点?我现在看到有两种不同的带有参数的删除方法。一个取一个要搜索的对象,另一个取一个索引。如果我的链表只是一个 Integer 对象的列表,编译器怎么知道我引用的是哪个 remove 方法?

标签: java linked-list singly-linked-list


【解决方案1】:

检查head->next是否为null,如果是则检查head中的值是否是您正在搜索的值,将其删除并返回null,否则什么也不做

您可以使用以下语法将其删除:-

list.remove(0);

考虑到该列表是您的 LinkedList,我还建议您阅读 Jenkov's tutorials 上的列表和集合,以帮助您掌握这一点。当我开始使用 Java 时,他们帮助了我很多。我已经把它们联系起来了。请随时提出问题,欢迎堆栈溢出!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多