Statement

• Difficulty: Medium
• Tag: `递归` `链表` `数学`

``````输入：l1 = [2,4,3], l2 = [5,6,4]

``````

``````输入：l1 = [0], l2 = [0]

``````

``````输入：l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]

``````

• 每个链表中的节点数在范围 `[1, 100]`
• `0 <= Node.val <= 9`
• 题目数据保证列表表示的数字不含前导零

• Difficulty: Medium
• Tag: `Recursion` `Linked List` `Math`

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 contains a single digit. Add the two numbers and return the sum as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example 1:

``````Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807.
``````

Example 2:

``````Input: l1 = [0], l2 = [0]
Output: [0]
``````

Example 3:

``````Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
Output: [8,9,9,9,0,0,0,1]
``````

Constraints:

• The number of nodes in each linked list is in the range `[1, 100]`.
• `0 <= Node.val <= 9`
• It is guaranteed that the list represents a number that does not have leading zeros.

Solution

``````#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define endl "\n"
#define fi first
#define se second
#define all(x) begin(x), end(x)
#define rall rbegin(a), rend(a)
#define lowbit(x) ((x) & (-(x)))
#define bitcnt(x) (__builtin_popcountll(x))
#define complete_unique(a) a.erase(unique(begin(a), end(a)), end(a))
#define mst(x, a) memset(x, a, sizeof(x))
#define MP make_pair

using ll = long long;
using ull = unsigned long long;
using db = double;
using ld = long double;
using VLL = std::vector<ll>;
using VI = std::vector<int>;
using PII = std::pair<int, int>;
using PLL = std::pair<ll, ll>;

using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const ll mod = 1e9 + 7;

template <typename T, typename S>
inline bool chmax(T &a, const S &b) {
return a < b ? a = b, 1 : 0;
}

template <typename T, typename S>
inline bool chmin(T &a, const S &b) {
return a > b ? a = b, 1 : 0;
}

#ifdef LOCAL
#include <debug.hpp>
#else
#define dbg(...)
#endif

/**
* 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) {}
* };
*/

#ifdef LOCAL

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) {}
};

#endif

class Solution {
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
ListNode *rt = new ListNode();
ListNode *pre = rt;

int remind = 0;
while (l1 != nullptr || l2 != nullptr || remind) {
int current = remind;

if (l1 != nullptr) {
current += l1->val;
l1 = l1->next;
}

if (l2 != nullptr) {
current += l2->val;
l2 = l2->next;
}

ListNode *cur = new ListNode(current % 10);
pre->next = cur;
pre = cur;

remind = current / 10;
}

return rt->next;
}
};

#ifdef LOCAL

int main() {
return 0;
}

#endif
``````