-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode-2-Add_Two_Numbers.cpp
135 lines (121 loc) · 4.31 KB
/
leetcode-2-Add_Two_Numbers.cpp
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <stdio.h>
#include <stdlib.h>
// Definition for singly-linked list.
struct ListNode {
int val;
struct ListNode *next;
};
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
struct ListNode *result_node = NULL,
*temp_result = NULL,
*temp_l1 = l1,
*temp_l2 = l2;;
size_t sum = 0;
while(temp_l1 || temp_l2 || sum) {
if(temp_l1){
sum += temp_l1->val;
temp_l1 = temp_l1->next;
}
if(temp_l2){
sum += temp_l2->val;
temp_l2 = temp_l2->next;
}
if (!result_node) {
result_node = (struct ListNode*)malloc(sizeof(struct ListNode));
temp_result = result_node;
temp_result->next = NULL;
temp_result->val = sum%10;
sum /= 10;
} else {
temp_result->next = (struct ListNode*)malloc(sizeof(struct ListNode));
temp_result = temp_result->next;
temp_result->next = NULL;
temp_result->val = sum%10;
sum /= 10;
}
}
temp_result = NULL;
temp_l1 = NULL;
temp_l2 = NULL;
return result_node;
}
void freeNode(struct ListNode* node){
while (node)
{
struct ListNode *fast_l1 = node->next;
if(fast_l1 && fast_l1->next){
node->next = NULL;
free(node);
node = fast_l1;
fast_l1 = fast_l1->next;
}
else if (!fast_l1->next){
free(node);
node = NULL;
}
}
}
int main(){
struct ListNode* result = NULL,
*temp_result = NULL;
// l1 = [2,4,3], l2 = [5,6,4]
// struct ListNode *node_l1 = NULL,
// *node_l2 = NULL;
// node_l1 = (struct ListNode*)malloc(sizeof(struct ListNode));
// node_l1->val = 2;
// node_l1->next = (struct ListNode*)malloc(sizeof(struct ListNode));
// node_l1->next->val = 4;
// node_l1->next->next = (struct ListNode*)malloc(sizeof(struct ListNode));
// node_l1->next->next->val = 3;
// node_l1->next->next->next = NULL;
//
// node_l2 = (struct ListNode*)malloc(sizeof(struct ListNode));
// node_l2->val = 5;
// node_l2->next = (struct ListNode*)malloc(sizeof(struct ListNode));
// node_l2->next->val = 6;
// node_l2->next->next = (struct ListNode*)malloc(sizeof(struct ListNode));
// node_l2->next->next->val = 4;
// node_l2->next->next->next = NULL;
// l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
struct ListNode *node_l1 = NULL,
*node_l2 = NULL;
node_l1 = (struct ListNode*)malloc(sizeof(struct ListNode));
node_l1->val = 9;
node_l1->next = (struct ListNode*)malloc(sizeof(struct ListNode));
node_l1->next->val = 9;
node_l1->next->next = (struct ListNode*)malloc(sizeof(struct ListNode));
node_l1->next->next->val = 9;
node_l1->next->next->next = (struct ListNode*)malloc(sizeof(struct ListNode));
node_l1->next->next->next->val = 9;
node_l1->next->next->next->next = (struct ListNode*)malloc(sizeof(struct ListNode));
node_l1->next->next->next->next->val = 9;
node_l1->next->next->next->next->next = (struct ListNode*)malloc(sizeof(struct ListNode));
node_l1->next->next->next->next->next->val = 9;
node_l1->next->next->next->next->next->next = (struct ListNode*)malloc(sizeof(struct ListNode));
node_l1->next->next->next->next->next->next->val = 9;
node_l1->next->next->next->next->next->next = NULL;
node_l2 = (struct ListNode*)malloc(sizeof(struct ListNode));
node_l2->val = 9;
node_l2->next = (struct ListNode*)malloc(sizeof(struct ListNode));
node_l2->next->val = 9;
node_l2->next->next = (struct ListNode*)malloc(sizeof(struct ListNode));
node_l2->next->next->val = 9;
node_l2->next->next->next = (struct ListNode*)malloc(sizeof(struct ListNode));
node_l2->next->next->next->val = 9;
node_l2->next->next->next->next = NULL;
result = addTwoNumbers(node_l1, node_l2),
temp_result = result;
while (temp_result)
{
printf(", %d", temp_result->val);
temp_result = temp_result->next;
}
freeNode(node_l1);
freeNode(node_l2);
freeNode(result);
node_l1 = NULL;
node_l2 = NULL;
result = NULL;
temp_result = NULL;
return 0;
}