/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};*/
class Plus {
public:
ListNode* plusAB(ListNode* a, ListNode* b) {
ListNode* head =new ListNode(0);
ListNode* cur = head;
int plus = 0;
while(a||b)
{
int num = (a?a->val:0)+(b?b->val:0)+plus;
plus = num /10;
num = num % 10;
cur->next =new ListNode(num);
cur = cur->next;
if(a)
a =a->next;
if(b)
b=b->next;
}
if(plus)
cur->next =new ListNode(plus);
return head->next;
}
};
链表A+B
最新推荐文章于 2020-06-26 21:33:58 发布