一、题目
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/31e7eaa5b8fc3178350fd8ef9b3a9f8a.png)
二、代码
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *root = new ListNode(0);
ListNode *cursor = root;
int carry = 0;
while(l1 != nullptr || l2 != nullptr || carry != 0) {
int l1Val = l1 != nullptr ? l1->val : 0;
int l2Val = l2 != nullptr ? l2->val : 0;
int sumVal = l1Val + l2Val + carry;
carry = sumVal / 10;
ListNode *sumNode = new ListNode(sumVal % 10);
cursor->next = sumNode;
cursor = sumNode;
if(l1 != nullptr) l1 = l1->next;
if(l2 != nullptr) l2 = l2->next;
}
return root->next;
}
};