lct8712
BAN USERlink_node_t *add_two_link_rec(link_node_t *link1, link_node_t *link2, int *carry)
{
if (link1 == NULL || link2 == NULL)
return NULL;
link_node_t *others = add_two_link_rec(link1->next, link2->next, carry);
int sum = link1->value + link2->value + *carry;
link_node_t *cur = create_link_node(sum % 10);
cur ->next = others;
*carry = sum / 10;
return cur;
}
link_node_t *add_two_link(link_node_t *link1, link_node_t *link2)
{
// let the link1 one be the longer/equal one
int len1 = get_link_list_len(link1);
int len2 = get_link_list_len(link2);
if (len1 < len2)
{
link_node_t *temp = link1;
link1 = link2;
link2 = temp;
}
// add leading zeros to link2 to make it the same length with link1
for (int longer_len = abs(len1 - len2); longer_len > 0; longer_len--)
{
link_node_t *zero_node = create_link_node(0);
zero_node->next = link2;
link2 = zero_node;
}
int carry = 0;
link_node_t *result = add_two_link_rec(link1, link2, &carry);
if (carry > 0)
{
link_node_t *node = create_link_node(carry);
node->next = result;
result = node;
}
return result;
}
void test_add_two_link()
{
int a[] = { 2 };
int b[] = { 9, 9, 9, 9 };
link_node_t *link_a = create_link_list(a, sizeof(a) / sizeof(a[0]));
link_node_t *link_b = create_link_list(b, sizeof(b) / sizeof(b[0]));
link_node_t *link_sum = add_two_link(link_a, link_b);
print_link(link_a);
print_link(link_b);
print_link(link_sum);
}
RepKoriScott, abc at Omli
Licensed mechanical maintenance engineer with extensive practical experience working with diverse systems and equipment. Solid professional knowledge base built upon ...
Repjomajogya, Applications Developer at Autonomy Zantaz
I’m a teacher assistant who supports the teacher in planning and presenting lessons, and helps students learn . I specialize ...
RepManbirTaylor, abc at 247quickbookshelp
I have strong communication skills that have been developed over a customer service career of nearly 3 years. I have ...
RepHeldaNate, Dev Lead at AMD
I am Helda , a certified Swimming Instructor capable of providing professional lessons and instructions on different swimming styles to various ...
RepDukeRollin, Java Experienced at AMD
Duke, a Qualified psychiatrist with seven years of experience effectively treating patients with a wide range of conditions with a ...
time complex O(n)
tell me if I'm wrong
- lct8712 August 25, 2011