From f28213c3ee593a22cf432a152b80bbbd0e891cd8 Mon Sep 17 00:00:00 2001 From: Krishna Vedala <7001608+kvedala@users.noreply.github.com> Date: Fri, 30 Oct 2020 00:51:33 -0400 Subject: [PATCH] fixed problem 2 Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> --- leetcode/problem_2/sol1.c | 130 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 124 insertions(+), 6 deletions(-) diff --git a/leetcode/problem_2/sol1.c b/leetcode/problem_2/sol1.c index d2bec030..7e4eed8e 100644 --- a/leetcode/problem_2/sol1.c +++ b/leetcode/problem_2/sol1.c @@ -1,11 +1,33 @@ -/* - * Definition for singly-linked list. - * struct ListNode { - * int val; - * struct ListNode *next; - * }; +/** + * \file + * \brief [2. Add two numbers](https://leetcode.com/problems/add-two-numbers/) + * solution \details You are given two non-empty linked lists representing two + * non-negative integers. The digits are stored in reverse order, and each of + * their nodes contains a single digit. Add the two numbers and return the sum + * as a linked list. */ +#include +#include +#include +#include + +/** + * @brief Definition for singly-linked list. + */ +struct ListNode +{ + int val; /**< value at node */ + struct ListNode *next; /**< pointer to next node in the list */ +}; + +/** + * @brief Solution implementation + * + * @param l1 first number with digits represented as linked list + * @param l2 second number with digits represented as linked list + * @return resulting number with digits represented as linked list + */ struct ListNode *addTwoNumbers(struct ListNode *l1, struct ListNode *l2) { struct ListNode *head = NULL; @@ -55,3 +77,99 @@ struct ListNode *addTwoNumbers(struct ListNode *l1, struct ListNode *l2) return head; } + +/** + * @brief Function to free dynamically allocated list + * + * @param node pointer to head of the list + */ +void free_list(struct ListNode *node) +{ + if (node->next != NULL) + free_list(node->next); + + free(node); + node = NULL; +}; + +/** + * @brief Generate a node list from an array of digits + * + * @param num array of digits. index of units place is '0' + * @param num_digits total number of digits + * @return pointer to the node containing the units place of the number + */ +struct ListNode *list_from_array(const unsigned char num[], + unsigned int num_digits) +{ + struct ListNode *prev_node = NULL; + struct ListNode *root_node = NULL; + + for (unsigned int i = 0; i < num_digits; i++) + { + struct ListNode *node = malloc(sizeof(struct ListNode)); + node->val = num[i]; + node->next = NULL; + if (!prev_node) + { + prev_node = node; + root_node = node; + } + else + { + prev_node->next = node; + prev_node = node; + } + } + + return root_node; +} + +/** + * @brief Support function to print the list of digits + * + * @param node pointer to units digit + * @return string representation of the digits with units place to he left + */ +const char *print_list(const struct ListNode *node) +{ + static char out_str[256]; + memset(out_str, '\0', 256); + const struct ListNode *ptr = node; + while (ptr) + { + if (ptr == node) + snprintf(out_str, 256, "%d", ptr->val); + else + snprintf(out_str, 256, "%s, %d", out_str, ptr->val); + ptr = ptr->next; + } + + return out_str; +} + +/** + * @brief Main function + * @return 0 + */ +int main() +{ + unsigned char num1[] = {2, 4, 3}; + struct ListNode *l1 = list_from_array(num1, 3); + unsigned char num2[] = {5, 6, 4}; + struct ListNode *l2 = list_from_array(num2, 3); + struct ListNode *out1 = addTwoNumbers(l1, l2); + + printf("<%s> + ", print_list(l1)); + printf("<%s> = ", print_list(l2)); + printf("<%s>\n", print_list(out1)); + assert(out1->val == 7 && out1->next->val == 0 && + out1->next->next->val == 8); + printf("Test 1 passed.\n"); + + free_list(l1); + free_list(l2); + free_list(out1); + + return 0; +}