递归复制链表(Java)

递归复制链表(Java),java,recursion,Java,Recursion,一个漫长的夜晚结束后,我在递归复制链表时遇到了麻烦,我可以用一个简单的迭代方法来实现,但是当我尝试用递归设置它时,我遇到了堆栈溢出错误。然而,这在概念上对我来说是有意义的。谁能把我引向正确的方向?这就是我到目前为止所做的: public LinkedList<E> createCopyRecursive(Node<E> aNode) { LinkedList<E> copyList = new LinkedList<E>(); co

一个漫长的夜晚结束后,我在递归复制链表时遇到了麻烦,我可以用一个简单的迭代方法来实现,但是当我尝试用递归设置它时,我遇到了堆栈溢出错误。然而,这在概念上对我来说是有意义的。谁能把我引向正确的方向?这就是我到目前为止所做的:

public LinkedList<E> createCopyRecursive(Node<E> aNode) {
    LinkedList<E> copyList = new LinkedList<E>();
    copyList.myStart = myStart;

    if (copyList.size() == 0) {
        aNode = myStart.getLink();
    }

    if (aNode.getLink() == null) {
        return copyList;
    }
    else {
        copyList.add(aNode.getValue());
        return createCopyRecursive(aNode.getLink());
    }
}
公共链接列表createCopyRecursive(节点阳极){ LinkedList copyList=新建LinkedList(); copyList.myStart=myStart; if(copyList.size()=0){ 阳极=myStart.getLink(); } if(阳极.getLink()==null){ 返回文案; } 否则{ copyList.add(阳极.getValue()); 返回createCopyRecursive(阳极.getLink()); } }
每次递归到该方法时,您都在创建一个新的LinkedList


我怀疑您想在方法之外实例化它,每次传入并添加到它。

每次递归到该方法时,您都在创建一个新的LinkedList


我怀疑您想在方法之外实例化它,每次传入并添加到它。

每次递归到该方法时,您都在创建一个新的LinkedList


我怀疑您想在方法之外实例化它,每次传入并添加到它。

每次递归到该方法时,您都在创建一个新的LinkedList


我怀疑您想在方法之外实例化它,每次传入它并添加到它。

我认为它可以这么简单:

private LinkedList<E> copyRecursive(final Node<E> node, final LinkedList<E> accumulator) {
                if (node == null) {
                    // all nodes traversed, return the result.
                    return accumulator;
                }
                // add current node to the copy list that is under construction.
                accumulator.add(node.getElement());
                // recursive call to copy the rest of the nodes to the copy list and return it when finished.
                return copyRecursive(node.getNext(), accumulator);
            }
private LinkedList copyRecursive(最终节点,最终LinkedList累加器){
if(node==null){
//遍历的所有节点,返回结果。
回流蓄能器;
}
//将当前节点添加到正在构建的副本列表中。
add(node.getElement());
//递归调用,将其余节点复制到复制列表中,并在完成时返回它。
返回copyRecursive(node.getNext(),累加器);
}
首先创建一个空的新链表,其中将包含副本,然后逐节点递归地将副本复制到其中。您也不能像这样将累加器传递给它:

private LinkedList<E> copyRecursive(final Node<E> node) {
                    if (node == null) {
                        return new LinkedList<>();
                    }
                    final LinkedList<E> accumulator = copyRecursive(node.getNext());
                    accumulator.add(node.getElement());
                    return accumulator;
                }
private LinkedList copyRecursive(最终节点){
if(node==null){
返回新的LinkedList();
}
最终LinkedList累加器=copyRecursive(node.getNext());
add(node.getElement());
回流蓄能器;
}
但这将颠倒列表中节点的顺序

下面是一个使用递归复制和递归反转的完整工作示例:

public class RecursiveCopyTest {
    public static void main(String[] args) {
        final LinkedList<String> linkedList = new LinkedList<>();
        linkedList.add("first");
        linkedList.add("next");
        linkedList.add("last");
        System.out.println(linkedList);
        System.out.println(linkedList.copyRecursive());
        System.out.println(linkedList.reverse());
   }

private static class LinkedList<E> {

    private Node<E> first;

    public LinkedList() {
        first = null;
    }

    public LinkedList<E> copyRecursive() {
        return copyRecursive(first, new LinkedList<E>());
    }

    public LinkedList<E> reverse() {
        return reverse(first);
    }

    public void add(E element) {
        final Node<E> node = new Node<>(element);
        if (first == null) {
            first = node;
        } else {
            Node<E> current = first;
            while (current.getNext() != null) {
                current = current.getNext();
            }
            current.setNext(node);
        }

    }

    private LinkedList<E> reverse(final Node<E> node) {
        if (node == null) {
            return new LinkedList<>();
        }
        final LinkedList<E> accumulator = reverse(node.getNext());
        accumulator.add(node.getElement());
        return accumulator;
    }

    private LinkedList<E> copyRecursive(final Node<E> node, final LinkedList<E> accumulator) {
            if (node == null) {
                return accumulator;
            }
            accumulator.add(node.getElement());
            return copyRecursive(node.getNext(), accumulator);
        }

        @Override
        public String toString() {
            final StringBuilder stringBuilder = new StringBuilder();
            Node current = first;
            while (current != null) {
                stringBuilder.append(current.getElement().toString()).
                        append(" -> ");
                current = current.getNext();
            }
            stringBuilder.append(" _ ");
            return stringBuilder.toString();
        }

        private static final class Node<E> {
            private final E element;
            private Node<E> next;

            public Node(final E element) {
                this.element = element;
            }

            public E getElement() {
                return element;
            }

            public void setNext(final Node<E> next) {
                this.next = next;
            }

            public Node<E> getNext() {
                return next;
            }
        }
    }
}
公共类递归CopyTest{
公共静态void main(字符串[]args){
final LinkedList LinkedList=新LinkedList();
linkedList.add(“第一”);
linkedList.add(“下一步”);
linkedList.add(“last”);
System.out.println(linkedList);
System.out.println(linkedList.copyRecursive());
System.out.println(linkedList.reverse());
}
私有静态类LinkedList{
私有节点优先;
公共链接列表(){
第一个=空;
}
公共链接列表copyRecursive(){
返回copyRecursive(第一个,newlinkedlist());
}
公共链接列表反向(){
返回反向(第一);
}
公共无效添加(E元素){
最终节点=新节点(元素);
if(first==null){
第一个=节点;
}否则{
节点电流=第一;
while(current.getNext()!=null){
current=current.getNext();
}
current.setNext(节点);
}
}
私有LinkedList反向(最终节点){
if(node==null){
返回新的LinkedList();
}
最终LinkedList累加器=反向(node.getNext());
add(node.getElement());
回流蓄能器;
}
私有LinkedList copyRecursive(最终节点,最终LinkedList累加器){
if(node==null){
回流蓄能器;
}
add(node.getElement());
返回copyRecursive(node.getNext(),累加器);
}
@凌驾
公共字符串toString(){
最终StringBuilder StringBuilder=新StringBuilder();
节点电流=第一;
while(当前!=null){
stringBuilder.append(current.getElement().toString())。
附加(“->”);
current=current.getNext();
}
stringBuilder.append(“”);
返回stringBuilder.toString();
}
私有静态最终类节点{
私有最终E元素;
私有节点下一步;
公共节点(最终E元素){
this.element=元素;
}
公共E getElement(){
返回元素;
}
公共void setNext(最终节点next){
this.next=next;
}
公共节点getNext(){
下一步返回;
}
}
}
}

我认为可以这么简单:

private LinkedList<E> copyRecursive(final Node<E> node, final LinkedList<E> accumulator) {
                if (node == null) {
                    // all nodes traversed, return the result.
                    return accumulator;
                }
                // add current node to the copy list that is under construction.
                accumulator.add(node.getElement());
                // recursive call to copy the rest of the nodes to the copy list and return it when finished.
                return copyRecursive(node.getNext(), accumulator);
            }
private LinkedList copyRecursive(最终节点,最终LinkedList累加器){
if(node==null){
//遍历的所有节点,返回结果。
回流蓄能器;
}
//将当前节点添加到正在构建的副本列表中。
add(node.getElement());
//递归调用,将其余节点复制到复制列表中,并在完成时返回它。
返回copyRecursive(node.getNext(),累加器);
}
第一cr
Node<Integer> copiedHead = copy(head);    
private static Node<Integer> copy(Node<Integer> head) {
    if(head == null){
        return null;
    }   
    return new Node<>(head.getData(), copy(head.getNext()));            
}