自己运行:
提交以后:
代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
struct ListNode *l,*p;
l = (struct ListNode*)malloc(sizeof(struct ListNode));
l->val = 0;
l->next = NULL;
p = l;
int carry = 0;
while(l1 || l2 || carry){
if(l1){
carry += l1->val;
l1=l1->next;
}
if(l2){
carry += l2->val;
l2=l2->next;
}
p->next = (struct ListNode*)malloc(sizeof(struct ListNode));
p = p->next;
p->val = carry%10;
carry /= 10;
}
return l->next;
}