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

Java 在方法中处理多个LinkedList对象,java,linked-list,Java,Linked List,我在Geeksforgeks.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; w

我在Geeksforgeks.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引用同一个节点实例,并且使用res reference(例如,res reference)对该实例所做的任何更改都将在使用tmp reference时保留。

以下是相同的代码,但带有附加注释,以帮助解释发生的情况。这段代码似乎是从链接列表中提取两个头部节点,每个节点都包含一个数字,然后将这些数字相加以创建一个新的单个数字列表(例如,1->2->3和5->6->7->8,其中=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;
    }
节点addTwoList(节点第一,节点第二){
Node res=null;//res是结果列表的头节点
Node prev=null;
节点温度=null;
整数进位=0,和;
while(first!=null | | second!=null)//当两个列表都存在时,实际上这是当至少一个列表存在时,我相信注释是错误的,代码是正确的
{
//计算结果列表中下一位的值。
//下一个数字是下列各项的总和
//(一)携带
//(ii)第一个列表的下一个数字(如果有下一个数字)
//(ii)第二个列表的下一个数字(如果有下一个数字)
总和=进位+(第一个!=null?第一个。数据:0)
+(秒!=null?秒。数据:0);
//为下一次计算更新进位
进位=(总和>=10)?1:0;//WCK-假定总和为0){
temp.next=新节点(进位);
}
//返回结果列表的标题
返回res;
}

因为我们在一个地方有res=temp,后来又有prev=temp,所以基本上这三个都指向同一个节点?如果res为null(在第一次迭代中)-是的,当然。我知道temp和prev会像你解释的那样移动到下一个指针,res不会再次更改(这是否意味着res仍在第一次迭代中)但是我们返回的res是正确的,并且它也有所有正确的数字?尽管在第一次迭代后res看起来没有移动,但由于prev、temp、res指向同一个节点,它将有prev和temp right更新的值?prev、temp和res仅在第一次迭代结束时指向同一个节点。prev、temp和res only在第一次迭代结束时指向同一个节点。在第二次迭代中,res将继续指向新列表中的第一个节点(头节点)。Temp将被重新分配(指向)另一个节点,即在第二次迭代中创建的节点(“Temp=新节点(sum)”。在同一次迭代之后不久,prev.next集合等于Temp(“上一个=临时”)。此行将上一个节点链接到刚刚创建的新节点。此时,prev和res仍然指向同一个节点,因此res.next与prev.next相同。在此之后,您会看到prev被分配给temp。此时,res仍然指向新列表的第一个节点,temp和prev指向第二个节点。作为第三个节点,它将在第二次迭代中,temp和prev遵循与第二次迭代相同的模式,因此在第三次迭代结束时,res仍然指向第一个节点,temp和prev将指向第三个节点。所有节点都与列表中的下一个节点保持链接,这意味着res是列表的头部,可以通过从res开始。这有帮助吗?明白。因为res在head,如果我们返回它,它将显示所有的值,这与prev和temp不同,但它们都是同一个节点的一部分。正确吗?