Java 删除单链表中给定位置和头部后的节点

Java 删除单链表中给定位置和头部后的节点,java,Java,我是否遗漏了以下代码中的任何内容。代码是关于从给定头和位置的linkedlist中删除节点。我的程序没有通过所有测试用例 Node Delete(Node head, int position) { // Node temp=head; int count=1; if(position==0){ head=head.next; return head; } if(position==1){ head=head.

我是否遗漏了以下代码中的任何内容。代码是关于从给定头和位置的linkedlist中删除节点。我的程序没有通过所有测试用例

Node Delete(Node head, int position) {

   // Node temp=head;
    int count=1;
    if(position==0){
       head=head.next;
       return head; 
    }
    if(position==1){
        head=head.next.next;
        return head;
    }
    if(position>1){
        Node temp=head;
        while(count<position){
            count++;
            temp=temp.next;
        }
        temp.next=temp.next.next;
    }
    return head;
}
节点删除(节点头,int位置){
//节点温度=头;
整数计数=1;
如果(位置==0){
head=head.next;
回流头;
}
如果(位置==1){
head=head.next.next;
回流头;
}
如果(位置>1){
节点温度=头;
while(count
public静态节点删除(节点头,int位置){
节点=头部;
节点prevNode=null;
int指数=0;
if(head==null&&position==0){
回流头;
}
if(head!=null&&position==0){
head=null;
head=node.next;
}
如果(位置>0){

虽然(Index您的代码应该满足哪些测试用例?试着查看Java代码。如果返回的节点应该是linkedList的新头,那么您所做的错误是删除该位置上的每个节点,而不仅仅是删除该位置上的节点。
position==0
是您应该重新执行的唯一情况修改后的<代码>标题>代码>您的代码不满足什么样的测试用例?请考虑在OP的代码中添加一个注释,与OP的代码使用前一个节点和当前节点两个变量相比,这样当在给定位置删除节点时,将下一个节点指定为当前节点,并检查给定POS时的条件。ition大于零,然后使用while循环并迭代,直到找不到给定的位置节点,然后分配:-prevNode.next=node.next;node=null;
public static Node Delete(Node head, int position) {
           Node node = head;
           Node prevNode = null;
           int index = 0;
        if (head == null && position == 0){
             return head;
           }
         if (head != null && position == 0){
               head = null;
               head = node.next;

           }
           if (position > 0){
           while(index<position){
               prevNode = node;
               node = node.next;
               index = index + 1;
           }
           prevNode.next = node.next;
           node = null;
           }

         return head;
    }