繁体   English   中英

在一个方法中处理多个 LinkedList 对象

[英]Working of multiple LinkedList object in a method

我正在 geeksforgeeks.com 上汇总添加两个链表。 我对提供的答案感到困惑。

Node addTwoLists(Node first, Node second) {
        Node res = null; // res is head node of the resultant list
        Node prev = null;
        Node temp = null;
        int carry = 0, sum;

        while (first != null || second != null) //while both lists exist
        {
            // Calculate value of next digit in resultant list.
            // The next digit is sum of following things
            // (i)  Carry
            // (ii) Next digit of first list (if there is a next digit)
            // (ii) Next digit of second list (if there is a next digit)
            sum = carry + (first != null ? first.data : 0)
                    + (second != null ? second.data : 0);

            // update carry for next calulation
            carry = (sum >= 10) ? 1 : 0;

            // update sum if it is greater than 10
            sum = sum % 10;

            // Create a new node with sum as data
            temp = new Node(sum);

            // if this is the first node then set it as head of
            // the resultant list
            if (res == null) {
                res = temp;
            } else // If this is not the first node then connect it to the rest.
            {
                prev.next = temp;
            }

            // Set prev for next insertion
            prev = temp;

            // Move first and second pointers to next nodes
            if (first != null) {
                first = first.next;
            }
            if (second != null) {
                second = second.next;
            }
        }

        if (carry > 0) {
            temp.next = new Node(carry);
        }

        // return head of the resultant list
        return res;
    }

我知道我们已经创建了三个节点 res、prev 和 temp,但我不明白它们中的每一个是如何同时更新的。

if (res == null) {
                res = temp;
            } else // If this is not the first node then connect it to the rest.
            {
                prev.next = temp;
            }

就像这里,当我们在 prev.next 中添加它时,第二个元素将如何添加到 res 中。 和下面:

if (carry > 0) {
            temp.next = new Node(carry);
        }

如果我们在 temp 中添加最后一个元素,它将如何反映在 res 中? 代码似乎正在运行,但我很难理解这个概念。

请帮忙。 提前致谢。

res、prev 和 temp 是节点的引用 当你写类似res = tmp; 这意味着现在 res 和 tmp指的是同一个 Node 实例,并且当您使用 tmp 引用时,使用例如 res 引用对此实例所做的任何更改都将保留。

这是相同的代码,但有额外的注释以试图帮助解释正在发生的事情。 此代码似乎从链接列表中获取两个头节点,其中每个节点包含一个数字,然后将这些数字加在一起以创建一个新的单个数字列表(例如 1->2->3 和 5->6->7 ->8 with = 1+5->2+6->3+7->0+8. 请注意,第三组导致结转,因此返回列表中的最终值将是 6->8-> 0->9)。 因此,此代码不是将两个链表相加,而是将两个链表的每个节点中包含的数据相加,并将结果放入第三个链表中。

我在代码中的附加注释以 WCK 为前缀 -

Node addTwoLists(Node first, Node second) {
        Node res = null; // res is head node of the resultant list
        Node prev = null;
        Node temp = null;
        int carry = 0, sum;

        while (first != null || second != null) //while both lists exist WCK actually this is While at least one of the lists exists, I believe the comment is wrong and the code is right
        {
            // Calculate value of next digit in resultant list.
            // The next digit is sum of following things
            // (i)  Carry
            // (ii) Next digit of first list (if there is a next digit)
            // (ii) Next digit of second list (if there is a next digit)
            sum = carry + (first != null ? first.data : 0)
                    + (second != null ? second.data : 0);

            // update carry for next calulation
            carry = (sum >= 10) ? 1 : 0; // WCK - assumes sum <20; is only carrying at most 1

            // update sum if it is greater than 10
            sum = sum % 10;

            // Create a new node with sum as data
            temp = new Node(sum); 

            // if this is the first node then set it as head of
            // the resultant list
            // WCK - the first time through the loop, res will be null
            // WCK - each subsequent time through the loop, the else is invoked to link the new node to the one from the previous iteration thus linking them
            if (res == null) {
                res = temp; //WCK - as @ardenit says, both res and temp point to the same node at this point
            } else // If this is not the first node then connect it to the rest.
            {
                prev.next = temp;
            }

            // Set prev for next insertion
            // WCK - now they are starting to set up for the next iteration.
            // WCK - the first time through, res, prev and temp all point to the same node after this statement is executed.
            prev = temp;

            // Move first and second pointers to next nodes
            if (first != null) {
                first = first.next;
            }
            if (second != null) {
                second = second.next;
            }

            // WCK - the first time through the loop, both of the lists passed in
            // have been advanced to the next node in their respective lists.
            // res and prev will point to the temp. As the next iteration of the loop
            // starts, temp will be pointed to a new node while res and prev will 
            // still point to the same node created in the first iteration. As the 
            // second iteration executes, prev is set to point to the new temp 
            // where you see (prev.next = temp) and then prev is advanced to the 
            // this new node as well. res will not be changed again (it is only set
            // once in the first iteration)

        }

        // WCK - now that the loop is finished, there may be a final carry value
        if (carry > 0) {
            temp.next = new Node(carry);
        }

        // return head of the resultant list
        return res;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM