简体   繁体   English

Java - 从 LinkedList 中删除一个元素,除了 first

[英]Java - Removing an element from LinkedList except first

I'm new to Java.我是 Java 新手。

I have created a method where it will remove elements from LinkedList except the first one.我创建了一个方法,它将从 LinkedList 中删除除第一个之外的元素。 The idea is a boolean will be set to true if a LinkedList's element data (Which is in Integer) matched with parameter.这个想法是,如果 LinkedList 的元素数据(在整数中)与参数匹配,则布尔值将设置为 true。 Once the boolean sets to true, it will remove any element that also matched with initial one.一旦布尔值设置为 true,它将删除任何与初始元素匹配的元素。

Now for the problem.现在的问题。 For example, if I were to remove 5 except the first one from this LinkedList:例如,如果我要从此 LinkedList 中删除除第一个之外的 5 个:

5 5 5 6 5 7 8 9 5 5 5 6 5 7 8 9

I will get result like this:我会得到这样的结果:

5 5 6 7 8 9 5 5 6 7 8 9

As you can see, it didn't remove the 5 on the second position.如您所见,它没有删除第二个位置的 5。 Is there anything wrong with my code?我的代码有什么问题吗?

Here's the code by the way顺便说一下,这是代码

public void append(int data) {
    Node newNode = new Node(data);
    if (head == null) {
        head = new Node(data);
        return;
    }

    Node lastNode = head;
    while (lastNode.next != null) {
        lastNode = lastNode.next;
    }

    lastNode.next = newNode;
    return;
}

public void insert(int data) {
    Node newData = new Node(data);
    newData.next = head;
    head = newData;
}

public void removeExceptFirst(int dataValue) { //The mentioned method
    boolean duplicate = false;
    Node currentNode = head;
    while (currentNode.next != null) {
        int value = currentNode.next.data;
        if (value == dataValue) {
            if (!duplicate) {
                duplicate = true;
                currentNode = currentNode.next;
            } else {
                currentNode.next = currentNode.next.next;
            }
        } else {
        currentNode = currentNode.next;
        }
    }
    return;
}

The problem here is with这里的问题是

if (!duplicate) {
     duplicate = true;
     currentNode = currentNode.next;
} 

you are marking duplicate = true and immediately assigning the "currentNode = currentNode.next;"您正在标记重复 = true 并立即分配“currentNode = currentNode.next;” due to this reference is getting preserve of the next node So由于此引用正在保留下一个节点所以

1. Put the condition outside of the loop to check whether the head element itself is 
   that node, if->yes mark isDuplicate = true and proceed in the loop.
2. Inside the loop check afterward and then assign the next node.

Hope this should work希望这应该有效

You skipped head node.您跳过了头节点。 Try replace尝试更换

    Node currentNode = head;

with

    Node currentNode = new Node();
    currentNode.next = head;

You should update the current node reference as well as head->next should point to the current node after removing the node.删除节点后,您应该更新当前节点引用,并且 head->next 应该指向当前节点。 try the below code:试试下面的代码:

if (!duplicate) {
    duplicate = true;
    currentNode = currentNode.next;
     head.next= currentNode.next;
}else {
    currentNode.next = currentNode.next.next;
    currentNode = currentNode.next;
    head.next = currentNode;  }

` `

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM