756. 两数相乘
给出两个链表形式表示的数字,写一个函数得到这两个链表相乘乘积。
样例
样例 1:
输入:9->4->6->null,8->4->null
输出:79464
解释:946*84=79464
样例 2:
输入:3->2->1->null,1->2->null
输出:3852
解释:321*12=3852
/**
* Definition for ListNode
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
int x = 1;
long sum = 0;
/**
* @param l1: the first list
* @param l2: the second list
* @return: the product list of l1 and l2
*/
public long multiplyLists(ListNode l1, ListNode l2) {
// write your code here
long a=0;
long b=0;
while (l1 != null) {
a=a*10+l1.val;
l1 = l1.next;
}
while (l2 != null) {
b=b*10+l2.val;
l2 = l2.next;
}
return a*b;
}
}