MyAnwser:
/**
* 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) {
auto* res = new ListNode(0);
res->val = (l1->val + l2->val)%10;
bool remainder = ((l1->val + l2->val)>=10) ? true:false;
auto* cur = res;
while(l1->next != 0 && l2->next != 0)
{
cur -> next = new ListNode(0);
cur = cur -> next;
l1 = l1->next;
l2 = l2->next;
cur->val = (l1->val + l2->val + (remainder ? 1:0) ) % 10;
if(l1->val + l2->val + (remainder ? 1:0) >= 10)
remainder = true;
else
remainder = false;
}
while(l1->next != 0){
l1 = l1->next;
cur->next = new ListNode(0);
cur = cur->next;
cur->val = (l1->val + (remainder ? 1:0)) % 10;
if((l1->val + (remainder ? 1:0)) >= 10)
remainder = true;
else
remainder = false;
}
while(l2->next != 0){
l2 = l2->next;
cur->next = new ListNode(0);
cur = cur->next;
cur->val = (l2->val + (remainder ? 1:0)) % 10;
if((l2->val + (remainder ? 1:0)) >= 10)
remainder = true;
else
remainder = false;
}
if(remainder){
cur->next = new ListNode(1);
}
return res;
}
};
原作者:jhanggj
#define BASE_NUM 10
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
if (l1 == NULL || l2 == NULL) {
return NULL;
}
int sumOfAandB = 0;
int remainder = 0;
bool createNewNodeFlag = false;
//Create curNode
struct ListNode* curNode = (struct ListNode*) malloc(sizeof(struct ListNode));
curNode->val = 0;
curNode->next = NULL;
//For return
struct ListNode* returnListNode = curNode;
while (l1 != NULL || l2 != NULL || createNewNodeFlag) {
//STEP.1 Cal
createNewNodeFlag = false;
sumOfAandB = ((l1) ? l1->val : 0) + ((l2) ? l2->val : 0) + remainder;
printf("sumOfAandB is %d\n", sumOfAandB);
//STEP.2 decide to create NewNode
//forward next one
if(l1 != NULL)
l1 = l1->next;
if(l2 != NULL)
l2 = l2->next;
if (l1 != NULL || l2 != NULL) {
createNewNodeFlag = true;
}
if (sumOfAandB > BASE_NUM) {
createNewNodeFlag = true;
curNode->val = sumOfAandB % BASE_NUM;
remainder = 1;
} else if (sumOfAandB == BASE_NUM) {
createNewNodeFlag = true;
curNode->val = 0;
remainder = 1;
} else if (sumOfAandB < BASE_NUM) {
curNode->val = sumOfAandB % BASE_NUM;
remainder = 0;
}
//STEP.3 Create the NewNode
if (createNewNodeFlag) {
curNode->next = (struct ListNode*) malloc(sizeof(struct ListNode));
curNode = curNode->next;
//Reset
curNode->val = 0;
curNode->next = NULL; //be noticed
}
}
return returnListNode;
}
原作者:Leeeeen
class Solution {
public:
//ListNode*
static ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
auto lhs = l1, rhs = l2;
int mul = 1;
bool flow = false;
auto *result = new ListNode(0);
auto node_it = result;
while(lhs || rhs) {
if(lhs)
node_it->val += lhs->val;
if(rhs)
node_it->val += rhs->val;
if(lhs || rhs || (node_it->val > 9)) {
auto *it = new ListNode(0);
if(node_it->val > 9) {
it->val += 1;
node_it->val -= 10;
}
lhs = (lhs && lhs->next) ? lhs->next : nullptr;
rhs = (rhs && rhs->next) ? rhs->next : nullptr;
if(lhs || rhs || it->val > 0) {
node_it->next = it;
node_it = node_it->next;
}
}
}
return result;
}
};
原作者:Favoryoung
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
struct ListNode *ll1, *ll2;
struct ListNode *head = NULL;
struct ListNode **lp = &head;
int carry = 0, sum = 0, val1, val2;
for (ll1 = l1, ll2 = l2; ll1 || ll2;) {
val1 = ll1 ? ll1->val : 0;
val2 = ll2 ? ll2->val : 0;
sum = val1 + val2 + carry;
*lp = malloc(sizeof(struct ListNode));
(*lp)->val = sum % 10;
(*lp)->next = NULL;
carry = sum / 10;
lp = &(*lp)->next;
ll1 = ll1 ? ll1->next : NULL;
ll2 = ll2 ? ll2->next : NULL;
}
if (carry) {
*lp = malloc(sizeof(struct ListNode));
(*lp)->val = carry;
(*lp)->next = NULL;
}
return head;
}