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.
【leetcode|leetcode_2】第一次的写法,过于复杂繁琐,考虑了太多因素,也导致了很多代码的重读
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1,ListNode* l2) {
if (l1==NULL||l2==NULL) {
return NULL;
}
ListNode* ptr1 = l1;
ListNode* ptr2 = l2;
ListNode* l3 = new ListNode(0);
ListNode* ptr3 = l3;
while(ptr1!=NULL && ptr2!=NULL) {
ptr3->val=ptr1->val+ptr2->val;
if(ptr3->val >= 10) {
ptr3->val = ptr3->val%10;
ptr3->next = new ListNode(1);
ptr3 = ptr3->next;
if(ptr1->next==NULL && ptr2->next==NULL) {
ptr1 = ptr1->next;
ptr2 = ptr2->next;
}
else if(ptr1->next==NULL && ptr2->next!=NULL) {
ptr1->next=new ListNode(0);
ptr1 = ptr1->next;
ptr2 = ptr2->next;
ptr2->val+=1;
}
else if(ptr2->next==NULL && ptr1->next!=NULL) {
ptr2->next=new ListNode(0);
ptr2 = ptr2->next;
ptr1 = ptr1->next;
ptr1->val+=1;
}
else if (ptr1->next!=NULL && ptr2->next!=NULL) {
ptr1 = ptr1->next;
ptr1->val+=1;
ptr2 = ptr2->next;
}
} else {
ptr3->next = new ListNode(0);
if(ptr1->next==NULL && ptr2->next==NULL) {
ptr1 = ptr1->next;
ptr2 = ptr2->next;
delete ptr3->next;
ptr3->next=NULL;
break;
}
else if(ptr1->next==NULL && ptr2->next!=NULL) {
ptr1->next=new ListNode(0);
ptr1 = ptr1->next;
ptr2 = ptr2->next;
}else if(ptr2->next==NULL && ptr1->next!=NULL) {
ptr2->next=new ListNode(0);
ptr2 = ptr2->next;
ptr1 = ptr1->next;
}else if (ptr1->next!=NULL && ptr2->next!=NULL) {
ptr1 = ptr1->next;
ptr2 = ptr2->next;
}
ptr3 = ptr3->next;
}
}
return l3;
}
};
第二次换了一下思路,不管长度是否一致,都在短的数字后面加上一个零,方便与长的数字进行后续相加,也要判断后面是否为NULL
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1,ListNode* l2) {
if (l1==NULL||l2==NULL) {
return NULL;
}
ListNode* ptr1 = l1;
ListNode* ptr2 = l2;
ListNode* l3 = new ListNode(0);
ListNode* ptr3 = l3;
while(ptr3!=NULL) {
ptr3->val=ptr1->val+ptr2->val;
if(ptr1->next==NULL && ptr2->next!=NULL)
ptr1->next=new ListNode(0);
else if(ptr2->next==NULL && ptr1->next!=NULL)
ptr2->next=new ListNode(0);
else if(ptr1->next==NULL&&ptr2->next==NULL) {
if(ptr3->val==0)
break;
ptr1->next=new ListNode(0);
ptr2->next=new ListNode(0);
}
ptr1 = ptr1->next;
ptr2 = ptr2->next;
ptr1->val = ptr1->val + ptr3->val/10;
ptr3->val = ptr3->val%10;
if(ptr1->val+ptr2->val != 0)
ptr3->next = new ListNode(0);
else if(ptr1->next!=NULL || ptr2->next!=NULL)
ptr3->next = new ListNode(0);
ptr3 = ptr3->next;
}
return l3;
}
};
推荐阅读
- 数据结构与算法|【算法】力扣第 266场周赛
- leetcode|今天开始记录自己的力扣之路
- Python|Python 每日一练 二分查找 搜索旋转排序数组 详解
- 【LeetCode】28.实现strstr() (KMP超详细讲解,sunday解法等五种方法,java实现)
- LeetCode-35-搜索插入位置-C语言
- leetcode python28.实现strStr()35. 搜索插入位置
- Leetcode Permutation I & II
- python|leetcode Longest Substring with At Most Two Distinct Characters 滑动窗口法
- LeetCode 28 Implement strStr() (C,C++,Java,Python)
- Python|Python Leetcode(665.非递减数列)