Java 此方法是否可以删除Linkedlist的工作?

Java 此方法是否可以删除Linkedlist的工作?,java,linked-list,Java,Linked List,我问过我的朋友们这件事,他们说你永远不能把电流带到上一个节点,当我问他们为什么不给我一个明确的理由时,有人能帮我吗 //here is the signature of the method; public void remove (){ Node<T> tmp = null; tmp.next = head; // I want to delete the current by the way!; while (tmp.next != current

我问过我的朋友们这件事,他们说你永远不能把电流带到上一个节点,当我问他们为什么不给我一个明确的理由时,有人能帮我吗

//here is the signature of the method;
public void remove (){
    Node<T> tmp = null;
    tmp.next = head;
    // I want to delete the current by the way!;
    while (tmp.next != current)
        tmp = tmp.next;
    tmp.next = current.next;
    //now am taking the current to the node before it so that it only becomes null if the linkedlist is empty;
    current=tmp;
}
//这是方法的签名;
公共空间删除(){
节点tmp=null;
tmp.next=头部;
//顺便说一下,我想删除当前的!;
while(tmp.next!=当前)
tmp=tmp.next;
tmp.next=current.next;
//现在,我将电流带到它前面的节点,这样只有当linkedlist为空时,它才会变为null;
电流=tmp;
}

如果您希望改变现有的链表并删除元素,那么基本想法是正确的。尽管在while之后缺少大括号,并且在第一次tmp时会发生NullPointException。下一步。

通常会向
remove
方法传递要删除的节点中的数据。你的问题不清楚当前的
应该指向什么。您的代码不支持删除列表的标题或空列表(空标题)

下面是一个可能的实现:

public boolean remove(T value) {
    if (head == null) {
        return false;
    } else if (head.value.equals(value)) {
        head = head.next;
        return true;
    } else {
        Node<T> current = head;
        while (current.next != null) {
            if (current.next.value.equals(value)) {
                current.next = current.next.next;
                return true;
            }
            current = current.next;
        }
        return false;
    }
}
公共布尔删除(T值){
if(head==null){
返回false;
}否则如果(头值等于(值)){
head=head.next;
返回true;
}否则{
节点电流=头;
while(current.next!=null){
if(当前.next.value.equals(值)){
current.next=current.next.next;
返回true;
}
当前=当前。下一步;
}
返回false;
}
}

如果
current
变量最终指向已删除节点之前的节点,则您会遇到一个问题:如果删除列表的开头,会发生什么情况?

什么是“您永远无法将当前节点移到上一个节点”呢?我是否要删除当前节点所指向的节点,如果出于某种原因,current==head,那么这意味着我想移除头部,问题是在我的医生植入术中,如果链表中只有一个节点,他确实将头部指向Null,所以我对自己说,如果我们可以这样做,那么我们也可以这样做