如何在Java中递归删除节点链接列表?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (189)

我正在学习数据结构并试图理解Java中的链接列表。我的问题是我在递归地删除给定索引处的节点时遇到麻烦。我的目标是获得O(log n)而不是使用循环并最终得到O(n)

public class LinkedList {
    Node head;
    int index=0;
    Node temp;
    Node prev;
    public LinkedList(Node head){
        this.head=head;
        temp=head;
        prev=null;
    }
    public int length(){
        int counter=0;
        Node n= head.next;
        while(n!=null){
            counter=counter+1;
            n=n.next;
        }
        return counter;
    }
    public void push(Node newNode){
        newNode.next=head;
        head=newNode;
    }
    public void add(Node prevNode, int value){
        if(prevNode==null){
            System.out.println("The given previous node can not be null!");
            return;
        }
        Node newNode= new Node(value,null);
        newNode.next=prevNode.next;
        prevNode.next=newNode;
    }
    public void add(int index, int value){
        length();
        if((index<0)||(index>length())){
            System.out.println("Array out of bound!");
            return;
        }
        if(index==0){
            push(new Node(value,null));
            return;
        }
        Node newNode= new Node(value,null);
        Node prevNode=head;
            for(int i=1;i<index;i++){
            prevNode=prevNode.next;
        }
        newNode.next=prevNode.next;
        prevNode.next=newNode;
    }
    public void delete(){
        head=head.next;
    }
    public void delete(int index){
        if((index<0)||(index>length())){
            System.out.println("Array out of bound!");
            return;
        }
        if(index==0){
            delete();
        return;}
        if(head.next==null||head==null){
            head=null;
        return;}
        if(this.index!=index){
            this.index++;
            prev=temp;
            temp=temp.next;
            delete(index);
        }if(this.index==index){
            prev=temp.next;
        }
    }
    public void search(int value){
        if(head!=null){
        if(value!=head.value){
            head=head.next;
            index=index+1;
            search(value);
        }else if(value==head.value){
            System.out.println("The value \""+value+"\" was found in index: "+index);}}}
    public void display(){
        Node n= head;
        System.out.print("{");
        while(n!=null){
            System.out.print(" ("+n.value+") ");
            n=n.next;
        }System.out.print("}");
        System.out.println("\n------------------------------");
    }
    public static void main(String[]args){
        LinkedList ll= new LinkedList(new Node(2,null));
        ll.push(new Node(5,null));
        ll.push(new Node(6,null));
        ll.push(new Node(13,null));
        ll.push(new Node(1,null));
        ll.display();
        ll.add(ll.head.next,8);
        ll.display();
        ll.add(0, 0);
        ll.display();
        ll.add(6, 4);
        ll.display();
        System.out.println(ll.length());
        ll.search(13);
        ll.delete(2);
        ll.display();
    }
}

因此,当我试图删除索引2处的条目时,它会删除该索引之前的所有数字,但不会删除该索引 - 因此它会删除[0]和[1]但不会删除[2]。

提问于
用户回答回答于

不确定它的复杂性是O(N)还是O(Log N)

 public void delete(int index){
        //check if the index is valid
        if((index<0)||(index>length())){
            System.out.println("Array out of bound!");
            return;
        }
        //pass the value head to temp only in the first run
        if(this.index==0)
            temp=head;
        //if the given index is zero then move the head to next element and return
        if(index==0){
            head=head.next;
        return;}
        //if the array is empty or has only one element then move the head to null
        if(head.next==null||head==null){
            head=null;
        return;}
        if(temp!=null){
            prev=temp;
            temp=temp.next;
            this.index=this.index+1;
            //if the given index is reached
           //then link the node prev to the node that comes after temp
          //and unlink temp
            if(this.index==index){
                prev.next=temp.next;
                temp=null;
                return;
            }
         //if not then call the function again
           delete(index);
        }
    }
用户回答回答于

很难理解您的代码,但由于要求逻辑来提高您的理解,所以共享psuedocode,可以参考它来相应地更正代码

Node delete (index i, Node n) // pass index and head reference node and return head
   if (n==null) // if node is null
      return null;
   if (i==1)  // if reached to node, which needs to be deleted, return next node reference.
      return n.next;  
   n.next= delete(n.next,i-1);
   return n; // recursively return current node reference

扫码关注云+社区

领取腾讯云代金券

年度创作总结 领取年终奖励