如何以递归方式从linkedList删除项目?

时间:2019-12-29 01:53:59

标签: java recursion linked-list singly-linked-list

以递归方式实现var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; for (var i = 0; i < arr.length; i += 4) { console.log("Working with: " + arr.slice(i, i + 4)); }对我来说有点挑战,我被困在实现其LinkedList方法的过程中,想知道如何在递归中继续引用上一个项目吗?

MyLinkedList类

remove

MyLinkedListTester类

package linkedlist;

public class MyLinkedList {
private Integer value;
private MyLinkedList next;

public MyLinkedList() {
}

public MyLinkedList(Integer value) {
    this.value = value;
}

public void add(Integer value) {
    if (this.value == null) {
        this.value = value;
    } else if (this.next == null) {
        this.next = new MyLinkedList(value);
    } else {
        this.next.add(value);
    }
}

public MyLinkedList remove(Integer index) {
//
//        if (index < 0) {
//            return this;
//        }
//        if (index == 0) {
//            return this.next;
//        }
//        this.next = remove(index - 1);
    return this;
}

public Integer indexOf(Integer value) {
    if (this.value.equals(value)) {
        return 0;
    } else if (this.next == null) {
        return null;
    } else {
        return 1 + this.next.indexOf(value);
    }
}
}

1 个答案:

答案 0 :(得分:1)

如评论中所述,在大多数情况下,迭代方法更容易,更有效。无论如何,我认为您可以这样做,因为在Java中您已经有一个LinkedList。

所以首先,您的思维存在某种错误(据我所知)。这也是一种糟糕的设计选择。创建MyLinkedList并将数据直接保存到其中,下一个也是MyLinkedList类,但它不是列表,而是Node。列表应该只有一个,0个-​​许多节点。

例如,我不知道如何执行删除函数,该函数将返回已删除的Node(在您的情况下为MyLinkedList),并且在删除该{列表中的第一个元素。

如果您正在查看实现,这就是它们使用Nodes的原因,并且它也更合乎逻辑(列表不包含“ List elements”)等等……

其他说明:如果您尝试获取不存在的元素,则您的indexOf函数将返回错误(1 + null =>错误)。

无论如何。您要做的就是创建一个Node。 (顺便说一句,如果您真的想要一个真正的LinkedList,则可以使用通用而不是int / Integer)。

下面,我发布解决方案的方法(可能会更好,但这就是我的方法)。我还编写了一个toString方法,以查看List的外观(据我所知它可以正常工作)。如果您仍然想在没有Node的情况下使用代码,则应该为您提供一个如何解决remove问题的想法。您也可以将某些逻辑放入Node类中,但是对我来说Node只是一个容器,实际上不包含任何逻辑。

public class MyLinkedList {
    private Node head;

    public MyLinkedList() {
    }

    public class Node{
        private int value;
        private Node next = null;

        public Node(int value){
            this.value = value;
        }

        public int getValue(){
            return value;
        }

        public Node getNext(){
            return next;
        }

        public void setNext(Node next){
            this.next = next;
        }

    }

    public void add(int value) {
        Node next = new Node(value);
        if(head == null){
            head = next;
        } else {
            addRecursive(head,next);
        }
    }

    private void addRecursive(Node node, Node next) {
        if(node.next == null){
            node.setNext(next);
        } else {
            addRecursive(node.getNext(),next);
        }
    }

    public Node remove(int index){
        Node removeNode = head;
        if(index == 0){
            head = head.getNext();
        } else {
            removeNode = removeRecursive(head,index-1);
        }
        return removeNode;
    }

    private Node removeRecursive(Node node, int index){
        Node removeNode = node.getNext();
        if(index == 0){
            node.setNext(removeNode.getNext());
        } else {
            removeNode = removeRecursive(node.getNext(),index-1);
        }
        return removeNode;
    }

    public int indexOf(int value) {
        if (head == null){
            return -1;
        }  else if (head.getValue() == value){
            return 0;
        } else {
            return indexOfRecursive(head,value,0);
        }
    }

    private int indexOfRecursive(Node node, int value, int index) {
        if(node.getNext() == null){
            return -1;
        } else if(node.getNext().getValue() == value){
            return index + 1;
        } else {
            return indexOfRecursive(node.getNext(),value,index+1);
        }
    }

    @Override
    public String toString(){
        if(head == null){
            return "";
        } else {
            return toStringRecursive(head,"["+head.getValue());
        }
    }

    private String toStringRecursive(Node node, String output){
        if(node.getNext() == null){
            return output + "]";
        } else {
            return toStringRecursive(node.getNext(),output + ", " + node.getNext().getValue());
        }
    }
}
相关问题