简体   繁体   English

java函数找到两个列表的总和

[英]java function to find the sum of two lists

everybody 每个人

I wrote a code to solve this problem : 我写了一个代码来解决这个问题:

Write a Java function Sum2List that takes two lists L1 and L2 of the same size and returns list L that contains the sum of data inside the corresponding nodes of lists L1 and L2 编写一个Java函数Sum2List,它获取两个相同大小的列表L1和L2,并返回包含列表L1和L2的相应节点内的数据总和的列表L.

ex : l1 = {1,2,3}
 l2 = {4,5,6}
 l = l1 + l2 => {5,7,9}

my code is : 我的代码是:

public class Test {

public static List Sum2List (List l1 , List l2){

List l = new List();
ListNode lNode = new ListNode(0,null);
l.first = lNode;

ListNode p = l1.first;
ListNode q = l2.first;

for (p = l1.first; p.next!=null; p=p.next,q=q.next){

    lNode.data = p.data + q.data;

    if (p.next != null)
        lNode.next = new ListNode(0,null);

}

return l;
}

public static void main (String[] args){

List l1= new List();
ListNode node4 = new ListNode(4,null);
ListNode node3 = new ListNode(3,node4);
ListNode node2 = new ListNode(2,node3);
ListNode node1 = new ListNode(1,node2);

l1.first = node1;

List l2= new List();
ListNode node8 = new ListNode(8,null);
ListNode node7 = new ListNode(7,node8);
ListNode node6 = new ListNode(6,node7);
ListNode node5 = new ListNode(5,node6);

l2.first = node5;

List l = Sum2List(l1,l2);

for(ListNode p = l.first; p.next !=null; p=p.next){
    System.out.println(p.data);
}
}   
}

The problem is that the output is ( 10 ) , While it must be a list contain => 5,7,9 问题是输出是(10),而它必须是一个包含=> 5,7,9的列表

So , where is the mistake in the code ?? 那么,代码中的错误在哪里?

The problem is that you don't add any new node in the list you return. 问题是您没有在返回的列表中添加任何新节点。 You're always modifying the data of the first node. 您总是在修改第一个节点的数据。

Currently your method was summing the datas of the second to last node of both lists, so the list you returned only contains one node with the sum of 3 and 7 (=10). 目前,您的方法是对两个列表的倒数第二个节点的数据求和,因此您返回的列表只包含一个节点,其总和为3和7(= 10)。

You need to create a new node at each iteration and update the next node of the previous one. 您需要在每次迭代时创建一个新节点并更新前一个节点的下一个节点。 So something like this: 所以像这样:

for (p = l1.first; p != null; p = p.next, q = q.next){
    lNode.data = p.data + q.data;
    ListNode temp = new ListNode(0, null);
    lNode.next = temp;
    lNode = temp;
}

Also note that you should modify the for condition to be p!=null , otherwise you won't iterate through the last node. 另请注意,您应该将for条件修改为p!=null ,否则您将不会遍历最后一个节点。

If you walked through your code with a debugger (or with pen and paper!), you would solve this in less time than posting the question here. 如果您使用调试器(或使用笔和纸!)浏览代码,您可以在比此处发布问题更短的时间内解决此问题。

You are not stepping into the new node. 您没有进入新节点。

for (p = l1.first; p.next!=null; p=p.next,q=q.next){

    lNode.data = p.data + q.data;

    if (p.next != null){
        lNode.next = new ListNode(0,null);

        lNode = lNode.next;
    }
}

Add the line lNode = lNode.next; 添加行lNode = lNode.next; and the braces around the if block. 以及if块周围的括号。

Without running it, I'm not sure if thats the whole problem, but its certainly part of it 没有运行它,我不确定这是不是问题,但它肯定是它的一部分

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

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