java - 迭代方法: Delete Linked List node using only one reference variable

标签 java linked-list iteration

已解决:代码反射(reflect)解决方案

我一直在开发自定义链接列表,并且需要仅使用对列表的一个引用来删除具有给定键的节点。

我已经成功地使用两个引用(前一个节点,当前节点)来完成此操作,但对于如何仅使用一个引用来实现这一点感到有点困惑。

我的代码适用于除了删除头节点以及尝试删除“88”或不存在“100”的节点时不在列表中的节点(我得到空指针异常)之外的情况。

Here is my test data from the list:
0) 88
1) 2
2) 1
3) 8
4) 11

// Iterative method to delete a node with a given integer key
// Only uses ONE reference variable to traverse the list.
private void delete (int key, Node x) {

    // Check if list is empty
    if (isEmpty()) {
        System.out.println("Cannot delete; the list is empty.");
    }

    // Check if we're deleting the root node
    if (key == head.getKey()) {

        // Now the first in the list is where head was pointing
        removeFromHead();
    }

    // General case: while the next node exists, check its key
    for (x = head; x.getNext() != null; x = x.getNext()) {

        // If the next key is what we are looking for, we need to remove it
        if (key == x.getNext().getKey()) {

            // x skips over the node to be deleted.
            x.putNext(x.getNext().getNext());
        }
    } // End for
}

最佳答案

试试这个:

public Value delete (int key) {

    //check if list is empty
    if (head == null)
        //the key does not exist. return null to let the method caller know
        return null; 

    //check if we're deleting the root node
    if (key == head.getKey()) {
        //set the value of what we're deleting
        Value val = head.getNode().getValue();
        //now the first in the list is where head was pointing
        head = head.getNext();
        //there is now one less item in your list. update the size
        total--;
        //return what we're deleting
        return val;
    }

    // general case: while the next node exists, check its key
    for (Node x = head; x.getNext() != null; x = x.getNext()) {

        //check if the next node's key matches
        if (key == x.getNext().getKey()) {

            //set value of what we're deleting
            Value val = x.getNext().getNode().getValue();

            //x now points to where the node we are deleting points
            x.setNext(x.getNext().getNext());

            //there is now one less item in the list. update the size
            total--;

            //return what we're deleting
            return val; 
        }
    }


    //if we didn't find the key above, it doesn't exist. return null to let the
    // method caller know.
    return null; 
}

这是给 LinkedList<code><Value></code> 。总体思路已经有了,但您必须根据您设置一切的方式进行调整。

关于java - 迭代方法: Delete Linked List node using only one reference variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28266087/

相关文章:

java - 通过 JSpinner 动态调整 JTable 的大小

c - 单链路链路交换节点崩溃

css - 通过 SASS 迭代 Prop 类

c++ - 链表函数cpp中的指针作用域

java - 如何在 LinkedList 节点中保留两条信息?

python - 迭代搜索循环列表

swift - 在 Swift 中,什么是 'progression' ?

java - 提取方括号之间的数字并以逗号分隔

Java spring 文件下载代理与休息调用

java - 使用 Java 绘制 SVG 椭圆弧