1 题目2 思路3 代码4 实例
1 题目 2 思路遍历链表,逐位进行累加,每次记录进位数,加入到下一位的累加中,模拟加法运算,边累加边使用尾插法,进行结果链表的创建,最后返回结果链表。
3 代码class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
//ListNode* ans = (ListNode*) malloc(sizeof(ListNode));
ListNode* ans = new ListNode;
ListNode* r = ans;
bool isFisrtNode = true;
int carry = 0;//进位标识符
while(l1 != nullptr && l2 != nullptr){
int tmpValue = carry;
carry = 0;
tmpValue += l1->val + l2->val;
if(tmpValue > 9){
carry = tmpValue/10;
tmpValue %= 10;
}else{}
if(!isFisrtNode){
ListNode* s = new ListNode;
s->val = tmpValue;
r->next = s;
r = s;
}else{
r->val = tmpValue;
isFisrtNode = false;
}
l1 = l1->next;
l2 = l2->next;
}
//处理多余的位数(注意处理进位的结果)
while(l1 != nullptr){
ListNode* s = new ListNode;
int tmpValue = l1->val;
if(carry != 0){
tmpValue += carry;
carry = 0;
if(tmpValue > 9){
carry = tmpValue/10;
tmpValue %= 10;
}else{}
}
s->val = tmpValue;
r->next = s;
r = s;
l1 = l1->next;
}
while(l2 != nullptr){
ListNode* s = new ListNode;
int tmpValue = l2->val;
if(carry != 0){
tmpValue += carry;
carry = 0;
if(tmpValue > 9){
carry = tmpValue/10;
tmpValue %= 10;
}else{}
}
s->val = tmpValue;
r->next = s;
r = s;
l2 = l2->next;
}
//处理最后进位的结果
if(carry != 0){
ListNode* s = new ListNode;
s->val = carry;
r->next = s;
r = s;
}
r->next = nullptr;
return ans;
}
};
4 实例
#include#include #include using namespace std; //数据结构 struct ListNode { int val; ListNode *next; ListNode() : val(0), next(nullptr) {} ListNode(int x) : val(x), next(nullptr) {} ListNode(int x, ListNode *next) : val(x), next(next) {} }; //尾插法创建链表 ListNode* createListNode(deque input){ ListNode* p = (ListNode*) malloc(sizeof(ListNode)); ListNode* r = p; p->val = input.front(); input.pop_front(); while(!input.empty()){ ListNode* s = (ListNode*) malloc(sizeof(ListNode)); s->val = input.front(); input.pop_front(); r->next = s; r = s; } r->next = nullptr; return p; } //打印链表 void printListNode(ListNode* p){ ListNode* q = p; while(q != nullptr){ cout< val<<" "; q = q->next; }cout< val + l2->val; if(tmpValue > 9){ carry = tmpValue/10; tmpValue %= 10; }else{} if(!isFisrtNode){ ListNode* s = new ListNode; s->val = tmpValue; r->next = s; r = s; }else{ r->val = tmpValue; isFisrtNode = false; } l1 = l1->next; l2 = l2->next; } // printListNode(ans); while(l1 != nullptr){ ListNode* s = new ListNode; int tmpValue = l1->val; if(carry != 0){ tmpValue += carry; carry = 0; if(tmpValue > 9){ carry = tmpValue/10; tmpValue %= 10; }else{} } s->val = tmpValue; r->next = s; r = s; l1 = l1->next; } while(l2 != nullptr){ ListNode* s = new ListNode; int tmpValue = l2->val; if(carry != 0){ tmpValue += carry; carry = 0; if(tmpValue > 9){ carry = tmpValue/10; tmpValue %= 10; }else{} } s->val = tmpValue; r->next = s; r = s; l2 = l2->next; } if(carry != 0){ ListNode* s = new ListNode; s->val = carry; r->next = s; r = s; } r->next = nullptr; return ans; } }; int main() { //[2,4,9] [5,6,4,9] [7,0,4,0,1] //[2,4,3], l2 = [5,6,4] //输入:l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9] //输出:[8,9,9,9,0,0,0,1] deque node1{2,4,9}; deque node2{5,6,4,9}; ListNode* p1 = createListNode(node1); // printListNode(p1); ListNode* p2 = createListNode(node2); // printListNode(p2); Solution s; ListNode* ans = s.addTwoNumbers(p1, p2); printListNode(ans); }



