题目地址:
https://www.lintcode.com/problem/multiply-two-numbers/description
给定两个链表,表示十进制整数。求两个数的乘积。直接解析出两个整数然后相乘即可。代码如下:
public class Solution {
/**
* @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
return compute(l1) * compute(l2);
}
private long compute(ListNode l) {
long n = 0;
while (l != null) {
n *= 10;
n += l.val;
l = l.next;
}
return n;
}
}
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
时间复杂度,空间。