Today,We talk about example in leecode N0.2
题目(Description)
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
我的思路(Algorithm)
- 新建一个链表(ListNode*)并用关键字new初始化,命名为l3;(这一步主要时用来存放计算出来的数据)
- 再建一个ListNode* i4,并将i3赋值给i4;(主要是为了记录i3的头地址)
- 声明三个整型变量a,temp,s,设置一个循环;
- 循环中的实现:计算当前两个链表指针的指向元素,计算两个元素之和存入s,然后计算s+temp后取余放入a;并建立一个节点存放a,计算往前一位的进位数的大小temp=(s-temp)/10,然后指针l1,l2,l3都向后挪一位;
- 如果这两个链表不等长,必有一个li(i=1,2)出现等于NULL的情况,所以我们分情况来计算接下来的内容;
- 如果(l1!=NULL)设置循环(这个循环我记为循环L1):余数s为l1当前指向的节点中的数字+之前的进位数然后求余,并创建节点存放他,再求进位数temp;指针l1和l3都向后移一位。
- 如果(l2!=NULL)同样的设置循环L2类似L1;
- 然后处理temp的问题:如果temp存在进位的话,但是此时l1和l2都等于NULL,该怎么办,我们可以设置下面的循环来解决
if (temp!=0) {
l3->next = new ListNode(temp % 10);
temp /= 10;
l3 = l3->next;
}
- 最后要返回l4->next;这个一定要注意。
代码(Code)
/**
1. Definition for singly-linked list.
2. struct ListNode {
3. int val;
4. ListNode *next;
5. ListNode(int x) : val(x), next(NULL) {}
6. };
*/
class Solution {
ListNode *l3 = new ListNode(0);
ListNode *l4 = l3;
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
int a = 0, temp = 0, s = 0;
while(l1!=NULL||l2!=NULL) {
if (l1 != NULL)
{
a += l1->val;
l1 = l1->next;
}
if (l2 != NULL)
{
a += l2->val;
l2 = l2->next;
}
a += temp;
s = a % 10;
l3->next = new ListNode(s);
temp = (a - s) / 10;
l3 = l3->next;
a = 0;
}
/*if (l1 != NULL) {
for (; l1 != NULL;) {
s = (l1->val + temp) % 10;
l3->next = new ListNode(s);
temp = (l1->val + temp - s) / 10;
l1 = l1->next;
l3 = l3->next;
}
}
if (l2 != NULL )
{
do {
s = (l2->val + temp) % 10;
l3->next = new ListNode(s);
temp = (l2->val + temp - s) / 10;
l2 = l2->next;
l3 = l3->next;
} while (l2 != NULL);
}*/
if (temp!=0) {
l3->next = new ListNode(temp % 10);
temp /= 10;
l3 = l3->next;
}
return l4 ->next;
}
public:void show()
{
for (; l4 ->next!= NULL;l4->next = l4->next->next)
{
cout << l4->next->val << "\t";
}
}
};
结果(Result)
- RunTime:68ms;
- Memory:19.1M
改进(Improvement)
发现的问题:我们仔细去看上面的程序,会发现其中的for循环语句有很多重复的部分,这就引发思考,能不能去improve这个problem呢?当然可以;
我们看下面的程序:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
ListNode *l3 = new ListNode(0);
ListNode *l4 = l3;
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
int a = 0, temp = 0, s = 0;
while(l1!=NULL||l2!=NULL) {
if (l1 != NULL)
{
a += l1->val;
l1 = l1->next;
}
if (l2 != NULL)
{
a += l2->val;
l2 = l2->next;
}
a += temp;
s = a % 10;
l3->next = new ListNode(s);
temp = (a - s) / 10;
l3 = l3->next;
a = 0;
}
/*if (l1 != NULL) {
for (; l1 != NULL;) {
s = (l1->val + temp) % 10;
l3->next = new ListNode(s);
temp = (l1->val + temp - s) / 10;
l1 = l1->next;
l3 = l3->next;
}
}
if (l2 != NULL )
{
do {
s = (l2->val + temp) % 10;
l3->next = new ListNode(s);
temp = (l2->val + temp - s) / 10;
l2 = l2->next;
l3 = l3->next;
} while (l2 != NULL);
}*/
if (temp!=0) {
l3->next = new ListNode(temp % 10);
temp /= 10;
l3 = l3->next;
}
return l4 ->next;
}
public:void show()
{
for (; l4 ->next!= NULL;l4->next = l4->next->next)
{
cout << l4->next->val << "\t";
}
}
};