标签:思路 class 返回 方式 head nod https div struct
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* res=new ListNode(0);
ListNode* head=res;
int jin=0;
int v1,v2;
while(l1||l2||jin){
v1=(l1)?l1->val:0;
v2=(l2)?l2->val:0;
// cout<<"v1:"<<v1<<" "<<"v2:"<<v2<<endl;
ListNode *t=new ListNode((v1+v2+jin)%10);
res->next=t;
res=t;
jin=(v1+v2+jin)>=10?1:0;
l1=l1?l1->next:NULL;
l2=l2?l2->next:NULL;
}
return head->next;
}
};
标签:思路 class 返回 方式 head nod https div struct
原文地址:https://www.cnblogs.com/52dxer/p/12523499.html