Simple to understand c solution with malloc

• This solution creates two reversed linked list based on the given link list.
The original link list is not modified. But the idea is still the reverse link list.

``````/**
* struct ListNode {
*     int val;
*     struct ListNode *next;
* };
*/
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
while(l1!=NULL){
struct ListNode* temp;
temp = (struct ListNode*)malloc(sizeof(struct ListNode));
temp->val = l1->val;
l1 = l1->next;
}
while(l2!=NULL){
struct ListNode* temp;
temp = (struct ListNode*)malloc(sizeof(struct ListNode));
temp->val = l2->val;
l2=l2->next;
}

int carry=0;
struct ListNode* temp;
temp = (struct ListNode*)malloc(sizeof(struct ListNode));
if(temp->val>=10){
temp->val = temp->val % 10;
carry=1;
}else{
carry=0;
}
}

struct ListNode* temp;
temp = (struct ListNode*)malloc(sizeof(struct ListNode));
if(temp->val>=10){
temp->val = temp->val%10;
carry=1;
}else{
carry=0;
}
}
}

struct ListNode* temp;
temp = (struct ListNode*)malloc(sizeof(struct ListNode));
if(temp->val>=10){
temp->val = temp->val%10;
carry=1;
}else{
carry=0;
}
}
}
if(carry==1){
struct ListNode* temp;
temp = (struct ListNode*)malloc(sizeof(struct ListNode));
temp->val = 1;