用Java递归删除节点链接列表

时间:2018-09-22 17:21:26

标签: java recursion linked-list

我正在学习数据结构,并尝试了解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]。

例如,在此代码中,删除前的数组填充为:{0,1,13,8,6,5,4,2}。 调用delete(2)后,它具有以下条目:{13,8,6,5,4,2}

我只想删除13个 ,这样数组看起来像这样:{0,1,8,6,5,4,2}

我非常感谢任何改进代码的技巧。

2 个答案:

答案 0 :(得分:0)

很难理解您的代码,但是当您要求逻辑以提高理解力时,请共享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

答案 1 :(得分:0)

经过努力我设法解决了问题,这是答案,但是我仍然不确定它是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);
        }
    }