/** * Definition for singly-linked list. * 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) {} * }; */ class Solution { public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode* H = new ListNode(); ListNode* ptr = H; int next = 0; while(l1||l2||next){ int val = 0; if(l1) val += l1->val,l1 = l1->next; if(l2) val += l2 ->val,l2 = l2 -> next; val += next; ListNode* node = new ListNode(val%10); ptr -> next = node; ptr = node; next = val/10; } return H->next; } }; 标签:ListNode,val,int,相加,next,l2,l1 From: https://www.cnblogs.com/hbro/p/17398813.html