题目:
题解:
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
int stack1[100];
int stack2[100];
int top1 = 0;
int top2 = 0;
int carry = 0;
int sum = 0;
struct ListNode* temp = NULL;
struct ListNode* head = NULL;
while (l1) {
stack1[top1++] = l1->val;
l1 = l1->next;
}
while (l2) {
stack2[top2++] = l2->val;
l2 = l2->next;
}
while (top1 || top2 || carry) {
int m = top1 > 0 ? stack1[--top1] : 0;
int n = top2 > 0 ? stack2[--top2] : 0;
sum = m + n + carry;
carry = sum / 10;
head = malloc(sizeof(struct ListNode));
head->val = sum % 10;
head->next = temp;
temp = head;
}
return head;
}