- Add Two Numbers
You are given two linked lists representing two non-negative numbers. 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.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
注意考虑进位的问题,还有一个链表还未遍历完,以及当最后两个链表都遍历结束,有进位的问题需要注意
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
if (!l1 && !l2) return NULL;
else if (!l1) return l2;
else if (!l2) return l1;
ListNode* p1 = l1;
ListNode* p2 = l2;
ListNode result(0);
ListNode* tail = &result;
/*不能使用带头节点的插入,使用带头节点的插入,最后返回:[0,7,0,8]错误
ListNode* result;
result= (ListNode*)malloc(sizeof(ListNode));//存储最终需要返回的连表
result->next = NULL;
ListNode* tail = result;*/
int carry_num = 0;//存储进位的数
while(p1 != NULL && p2 != NULL)
{
ListNode* tmp = (ListNode*)malloc(sizeof(ListNode));
int tmpNum = p1->val + p2->val + carry_num;
tmp->val = tmpNum % 10;
carry_num = tmpNum / 10;
tmp->next = NULL;
tail->next = tmp;
tail = tmp;
p1 = p1->next;
p2 = p2->next;
}
while(p1 != NULL)
{
ListNode* tmp = (ListNode*)malloc(sizeof(ListNode));
int tmpNum = p1->val + carry_num;
tmp->val = tmpNum % 10;
carry_num = tmpNum /10;
tmp->next = NULL;
tail->next = tmp;
tail = tmp;
p1 = p1->next;
}
while(p2 != NULL)
{
ListNode* tmp = (ListNode*)malloc(sizeof(ListNode));
int tmpNum = p2->val + carry_num;
tmp->val = tmpNum % 10;
carry_num = tmpNum /10;
tmp->next = NULL;
tail->next = tmp;
tail = tmp;
p2 = p2->next;
}
if(carry_num > 0)
{
ListNode* tmp = (ListNode*)malloc(sizeof(ListNode));
tmp->val = carry_num;
tmp->next = NULL;
tail->next = tmp;
tail = tmp;
}
return result.next;
//以下错误的原因是把第一个数当作顺序存储的,不是按照反的存储,所以出错
/*
ListNode* newL1 = NULL;
ListNode* result = NULL;
ListNode* p1 = l1;
ListNode* p2 = l2;
while(p1)
{
ListNode* tmpNode = (ListNode*)malloc(sizeof(ListNode));
tmpNode->val = p1->val;
tmpNode->next = newL1;
newL1 = tmpNode;
p1 = p1->next;
}
p1 = newL1;
//while(p1)
//{
// cout<<p1->val<<" hi"<<endl;
// p1 = p1->next;
//}
int carry_num = 0;
while(p1!=NULL || p2!=NULL)
{
if(p1!=NULL && p2!=NULL)
{
//cout<<p1->val<<" "<<p2->val<<" "<<carry_num<<endl;
int tmpNum = p1->val+p2->val+carry_num;
//cout<<tmpNum<<"hi"<<endl;
//carry_num = 0;
carry_num = tmpNum/10;
// if(carry_num)
//{
ListNode* tmpNode = (ListNode*)malloc(sizeof(ListNode));
tmpNode->val = tmpNum%10;
tmpNode->next = result;
result = tmpNode;
p1 = p1->next;
p2 = p2->next;
// }
}
if(p1!=NULL && p2==NULL)
{
//cout<<"hi2"<<p1->val<<endl;
int tmpNum = p1->val+carry_num;
//carry_num = 0;
carry_num = tmpNum/10;
// if(carry_num)
//{
ListNode* tmpNode = (ListNode*)malloc(sizeof(ListNode));
tmpNode->val = tmpNum%10;
tmpNode->next = result;
result = tmpNode;
p1 = p1->next;
//p2 = p2->next;
}
if(p1==NULL && p2!=NULL)
{
int tmpNum = p2->val+carry_num;
//carry_num = 0;
carry_num = tmpNum/10;
// if(carry_num)
//{
ListNode* tmpNode = (ListNode*)malloc(sizeof(ListNode));
tmpNode->val = tmpNum%10;
tmpNode->next = result;
result = tmpNode;
// p1 = p1->next;
p2 = p2->next;
}
}
if(carry_num == 1)
{
ListNode* tmpNode = (ListNode*)malloc(sizeof(ListNode));
tmpNode->val = 1;
tmpNode->next = result;
result = tmpNode;
}
return result;*/
//以下采用的是直接暴力方法,不行,考虑的是大数相加
/* if(l1 == NULL && l2 == NULL)
{
return NULL;
}
int sum1 = 0;
int sum2 = 0;
ListNode* p1 = l1;
ListNode* p2 = l2;
ListNode* result=NULL;
int arr[100] = {};
int len = 0;
// int mag1 = 1;
int mag = 1;
while(p1!=NULL)
{
//sum1 += p1->val*10;
arr[len] = p1->val;
//cout<<arr[len]<<endl;
len++;
p1 = p1->next;
}
//cout<<len<<endl;
for(int i = len-1; i>=0; i--)
{
sum1+=mag*arr[i];
mag*=10;
}
//cout<<sum1<<endl;
mag = 1;
while(p2!=NULL)
{
sum2 = sum2+p2->val*mag;
p2 = p2->next;
mag = mag * 10;
}
//cout<<sum2<<endl;
int sum = sum1+sum2;
//cout<<sum<<endl;
int tmp1 = sum%10;
int tmp2 = sum;
while(tmp2)
{
ListNode* tmp = (ListNode*)malloc((sizeof(ListNode)));
tmp->val = tmp1;
tmp->next= result;
result = tmp;
tmp2 = tmp2/10;
tmp1 = tmp2%10;
}
return result;*/
}