/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
struct ListNode* dummy = malloc(sizeof(struct ListNode));
struct ListNode* cur = dummy;
int t = 0;
while(l1 || l2 || t){
if(l1) t += l1->val,l1=l1->next;
if(l2) t += l2->val,l2=l2->next;
cur->next = malloc(sizeof(struct ListNode));//防止野指针
cur->next->val = t%10;
cur->next->next = NULL;
cur = cur->next;
t /= 10;
}
return dummy->next;
}
2. 两数相加解法
最新推荐文章于 2024-08-05 17:56:32 发布