Java 如何将两个链表相乘为第三个链表?

Java 如何将两个链表相乘为第三个链表?,java,parsing,data-structures,illegalargumentexception,Java,Parsing,Data Structures,Illegalargumentexception,该函数将两个整数作为字符串,比如“123”和“456”,它们按连续顺序解析为各自的链表。因此,第一个将是3->2->1,第二个是6->5->4 我已经初始化了这些链表,但是如何将它们相乘以获得123*456的乘积?我不知道如何处理这个问题,特别是因为它们是按相反的顺序解析的 我不允许编辑这些链接列表。但是,我可以将它们解析为两个虚拟列表 感谢您的帮助 public static BigInteger multiply(BigInteger first, BigInteger second) {

该函数将两个整数作为字符串,比如“123”和“456”,它们按连续顺序解析为各自的链表。因此,第一个将是3->2->1,第二个是6->5->4

我已经初始化了这些链表,但是如何将它们相乘以获得123*456的乘积?我不知道如何处理这个问题,特别是因为它们是按相反的顺序解析的

我不允许编辑这些链接列表。但是,我可以将它们解析为两个虚拟列表

感谢您的帮助

public static BigInteger multiply(BigInteger first, BigInteger second) {

        /* IMPLEMENT THIS METHOD */

        // following line is a placeholder for compilation
        return null;
    }

// where earlier in the class

public BigInteger() {
        negative = false;
        numDigits = 0;
        front = null;
    }

// and in the same package

public class DigitNode {

    int digit;
    DigitNode next;

    DigitNode(int digit, DigitNode next) {
        this.digit = digit;
        this.next = next;
    }

    public String toString() {
        return digit + "";
    }
}

想想你是如何用手乘法的:

          456
        x 123
        -----
         1368
      +  9120
      + 45600
     --------
        56088
在列表中,
3->2->1
6->5->4
将3和6相乘,得到18的乘积。然后乘以3和5,得到15的乘积。但是您必须将其乘以10,因为数字
5
位于第二位。然后乘以3和4得到12,再乘以100。所以顺序是:

3*6 = 18
3*5*10 = 150
3*4*100 = 1200
把这些加起来得到1368

然后你从2开始。但它在第二个位置,所以实际上是20:

10*2*6 = 120
10*2*5*10 = 1000
10*2*4*100 = 8000
             ----
             9120
然后重复第三个数字1:

100*1*6 = 600
100*1*5*10 = 5000
100*1*4*100 = 40000
              -----
              45600
把你的分数加起来(45600+9120+1368)=56088

可以使用两个嵌套循环在链接列表上进行迭代。它看起来像这样:

total = 0
l1 = list1.head
l1Multiplier = 1
while l1 != null
    l2 = list2.head
    l2Multiplier = 1
    l1Sum = 0
    while l2 != null
        prod = l1Multiplier * l1.data * l2.data * l2Multiplier
        l1Sum = l1Sum + prod
        l2Multiplier = l2Multiplier * 10
        l2 = l2.next
    end while
    total = total + l1Sum
    l1Multiplier = l1Multiplier * 10
end while

// at this point, the result is in the total variable.
// You can extract the digits into another linked list.
这不是最有效的方法,但我怀疑这里的目标不是提出最有效的算法,而是学习如何迭代链表


如果您对更高效的算法感兴趣,请查看。

想想如何手动乘法:

          456
        x 123
        -----
         1368
      +  9120
      + 45600
     --------
        56088
在列表中,
3->2->1
6->5->4
将3和6相乘,得到18的乘积。然后乘以3和5,得到15的乘积。但是您必须将其乘以10,因为数字
5
位于第二位。然后乘以3和4得到12,再乘以100。所以顺序是:

3*6 = 18
3*5*10 = 150
3*4*100 = 1200
把这些加起来得到1368

然后你从2开始。但它在第二个位置,所以实际上是20:

10*2*6 = 120
10*2*5*10 = 1000
10*2*4*100 = 8000
             ----
             9120
然后重复第三个数字1:

100*1*6 = 600
100*1*5*10 = 5000
100*1*4*100 = 40000
              -----
              45600
把你的分数加起来(45600+9120+1368)=56088

可以使用两个嵌套循环在链接列表上进行迭代。它看起来像这样:

total = 0
l1 = list1.head
l1Multiplier = 1
while l1 != null
    l2 = list2.head
    l2Multiplier = 1
    l1Sum = 0
    while l2 != null
        prod = l1Multiplier * l1.data * l2.data * l2Multiplier
        l1Sum = l1Sum + prod
        l2Multiplier = l2Multiplier * 10
        l2 = l2.next
    end while
    total = total + l1Sum
    l1Multiplier = l1Multiplier * 10
end while

// at this point, the result is in the total variable.
// You can extract the digits into another linked list.
这不是最有效的方法,但我怀疑这里的目标不是提出最有效的算法,而是学习如何迭代链表


如果你对一个更高效的算法感兴趣,请查看。

广泛的问题,因此我将给出一个广泛的答案:K a R a T S U B即使第一个和第二个是带有链表的对象,也适用于这项工作?天知道,但Godspeed有一个关于如何使用以10为基数的数字进行Karatsuba的示例。不过,我强烈建议只使用简单的乘法算法来测试乘法是否有效。是否允许使用数据结构?这是一个广泛的问题,因此我将给出一个广泛的答案:K a R a T S U B即使第一个和第二个是带有链表的对象,也会采用这种方法?天知道,但是Godspeed有一个关于如何使用基数为10的数字进行Karatsuba的例子。我强烈建议使用简单的乘法算法来测试乘法是否有效。是否允许使用数据结构?