-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0002_add_two_numbers.c
45 lines (41 loc) · 1015 Bytes
/
0002_add_two_numbers.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* Author: Xiangyue Cai
* Date: 2019-07-16
*/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
struct ListNode *res = NULL;
struct ListNode *temp = NULL;
res = temp = malloc(sizeof(struct ListNode));
int carry = 0;
while(l1 || l2)
{
int v1 = l1 == NULL ? 0 : l1->val;
int v2 = l2 == NULL ? 0 : l2->val;
int sum = v1 + v2 + carry;
carry = sum / 10;
struct ListNode *new = malloc(sizeof(struct ListNode));
new->val = sum % 10;
temp->next = new;
temp = new;
if(l1)
l1 = l1->next;
if(l2)
l2 = l2->next;
}
if(carry)
{
struct ListNode *new = malloc(sizeof(struct ListNode));
new->val = carry;
temp->next = new;
temp = new;
}
temp->next = NULL;
return res->next;
}