1 class Solution {
2 public:
3 ListNode* addTwoNumbers(ListNode* l1, ListNode* l2)
4 {
5 int sign = 0;
6 int sum;
7 ListNode* root = nullptr;
8 ListNode* p_root = nullptr;
9 while (l1!=NULL || l2 != NULL || sign)
10 {
11 int val1 = 0;
12 int val2 = 0;
13 if (l1 != NULL){
14 val1 = l1->val;
15 l1 = l1->next;
16 }
17 if (l2 != NULL) {
18 val2 = l2->val;
19 l2 = l2->next;
20 }
21
22 sum = ((val1 + val2)%10 + sign)%10;
23 sign = (val1 + val2 + sign) / 10;
24 if (root == nullptr)
25 {
26 root = new ListNode(sum);
27 p_root = root;
28 }
29 else
30 {
31 p_root->next = new ListNode(sum);
32 p_root = p_root->next;
33 }
34 }
35 return root;
36 }
37 };