暂时不公布
/**
* 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 *ans = new ListNode(0), *t1=l1, *t2=l2;
ListNode *anshead=ans;
int carry=0;
while(t1||t2){
if(t1){
carry+= t1->val;
t1 = t1->next;
}
if(t2){
carry+= t2->val;
t2 = t2->next;
}
ans->val = carry%10;
carry /= 10;
if(t1||t2||carry){
ans->next = new ListNode(carry);
ans = ans->next;
}
}
return anshead;
}
};
执行用时 : 32 ms, 在Add Two Numbers的C++提交中击败了97.71% 的用户
内存消耗 : 10.3 MB, 在Add Two Numbers的C++提交中击败了84.13% 的用户