-
Notifications
You must be signed in to change notification settings - Fork 118
/
2.c
102 lines (83 loc) · 2.11 KB
/
2.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int val;
struct ListNode *next;
};
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
if (l1 == NULL) return l2;
if (l2 == NULL) return l1;
struct ListNode *p1 = l1, *p2 = l2;
struct ListNode *ans = (struct ListNode *)calloc(1, sizeof(struct ListNode));
struct ListNode *p = ans, *last = NULL, *t = NULL;
int sum = 0;
while (1) {
if (p1 && p2) {
sum += p1->val + p2->val;
p1 = p1->next;
p2 = p2->next;
}
else if (p1 && (p2 == NULL)) {
sum += p1->val;
p1 = p1->next;
}
else if (p2 && (p1 == NULL)) {
sum += p2->val;
p2 = p2->next;
}
else {
break;
}
p->val = sum % 10;
sum /= 10;
/* for next node or the final node */
p->next = t = (struct ListNode *)calloc(1, sizeof(struct ListNode));
last = p;
p = p->next;
}
if (sum != 0) {
t->val = sum;
}
else {
/* sum is 0, we don't need a redundant 0, delete it */
free(t);
last->next = NULL;
}
return ans;
}
int main() {
struct ListNode *l1 = (struct ListNode *)calloc(3, sizeof(struct ListNode));
struct ListNode *p1 = l1;
p1->val = 2;
p1->next = p1 + 1;
p1++;
p1->val = 4;
p1->next = p1 + 1;
p1++;
p1->val = 5;
p1->next = NULL;
struct ListNode *l2 = (struct ListNode *)calloc(5, sizeof(struct ListNode));
struct ListNode *p2 = l2;
p2->val = 5;
p2->next = p2 + 1;
p2++;
p2->val = 6;
p2->next = p2 + 1;
p2++;
p2->val = 4;
p2->next = p2 + 1;
p2++;
p2->val = 9;
p2->next = p2 + 1;
p2++;
p2->val = 9;
p2->next = NULL;
struct ListNode *p = addTwoNumbers(l1, l2);
/* 542 + 99465 = 100007 */
while (p != NULL) {
printf("%d ", p->val);
p = p->next;
}
printf("\n");
return 0;
}