java 迭代深度复制链接列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13245908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:00:37  来源:igfitidea点击:

iteratively deep copy a link list

javalinked-listdeep-copyiteration

提问by Hank

This is an homework assignment. To change the following recursive deep copy method into an iterative equivalent. I came up close, and need your help to make it right. Recursive implementation:

这是家庭作业。将以下递归深复制方法更改为迭代等效方法。我靠得很近,需要你的帮助才能把它弄好。递归实现:

public static StringNode copy(StringNode str) {
        if (str == null)
            return null;

        StringNode copyFirst = new StringNode(str.ch, null);
        copyFirst.next = copy(str.next);
        return copyFirst;
    }

Here is what I came up, the iterative equivalent. The static length()method has been implemented already to return how many nodes are there in a given link list.

这是我提出的,迭代等价物。该static length()方法已经被实现以返回给定链表中有多少节点。

public static StringNode copy(StringNode str) {
    if (str == null)
        return null;

    StringNode firstNode = new StringNode(str.ch ,null);
    StringNode prevNode = firstNode;
    StringNode nextNode;

    for (int i = 1; i < length(str); i++) {
        nextNode = new StringNode(str.next.ch, null);
        prevNode.next = nextNode;
        prevNode = nextNode;
    }

    return firstNode;
}

The problem: to test my implementation, I create a linked list str1with character value, 'n', 'b', 'a', then call

问题:为了测试我的实现,我创建了一个str1带有字符值的链表'n', 'b', 'a',然后调用

StringNode copy = StringNode.copy(str1);

then I delete the last node of str1, leave it as 'n','b',however, when i try to print out the content stored in copy, I get 'n', 'b', 'b'instead of 'n', 'b', 'a'.

然后我删除 str1 的最后一个节点,将其保留为'n','b',但是,当我尝试打印存储在副本中的内容时,我得到 'n', 'b', 'b'而不是'n', 'b', 'a'.

Any suggestions?

有什么建议?

采纳答案by Rohit Jain

You also need to move the strforward in your loop, else you are continuously adding the same strin your listin every iteration. First element is different for the first time invocation of method. and then str.nextis same through out your loop.

您还需要str在循环中向前移动,否则您same strlist在每次迭代中不断添加。第一次调用方法时,第一个元素是不同的。然后str.next在整个循环中都是相同的。

So, you need to add this code in your for loop: -

因此,您需要在 for 循环中添加此代码:-

str = str.next;

Also, your loop has some problem. You should not iterate till the length(str). But till str == null.

另外,你的循环有一些问题。你不应该迭代直到length(str). 但直到str == null.

So, finally your loop should look like this: -

所以,最后你的循环应该是这样的: -

while (str.next != null) {  // Iterate till str.next != null, as we are creating 
                            // the next node in the loop for current node str

    nextNode = new StringNode(str.next.ch, null);
    prevNode.next = nextNode;
    prevNode = nextNode;

    str = str.next;  // Move to next node.
}

A while loop has to be used in this case, since you don't know how many times the loop should iterate.

在这种情况下必须使用 while 循环,因为您不知道循环应该迭代多少次。