-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddTwoNumbers.c
33 lines (32 loc) · 926 Bytes
/
AddTwoNumbers.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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* createNode() {
struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode));
node->val = 0;
node->next = NULL;
return node;
}
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
int remainder = 0;
struct ListNode* sum = createNode();
struct ListNode* digit = sum;
for (int i = 0; i < 101; i++) {
int l1_val = (l1) ? l1->val : 0;
int l2_val = (l2) ? l2->val : 0;
int digit_sum = l1_val + l2_val + remainder;
remainder = digit_sum / 10;
digit->val = digit_sum % 10;
l1 = (l1) ? l1->next : l1;
l2 = (l2) ? l2->next : l2;
if (!l1 && !l2 && !remainder)
break;
digit->next = createNode();
digit = digit->next;
}
return sum;
}