fixed problem 2

Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com>
This commit is contained in:
Krishna Vedala 2020-10-30 00:51:33 -04:00
parent 86676ac210
commit f28213c3ee
No known key found for this signature in database
GPG Key ID: BA19ACF8FC8792F7

View File

@ -1,11 +1,33 @@
/* /**
* Definition for singly-linked list. * \file
* struct ListNode { * \brief [2. Add two numbers](https://leetcode.com/problems/add-two-numbers/)
* int val; * solution \details You are given two non-empty linked lists representing two
* struct ListNode *next; * 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 <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/**
* @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 *addTwoNumbers(struct ListNode *l1, struct ListNode *l2)
{ {
struct ListNode *head = NULL; struct ListNode *head = NULL;
@ -55,3 +77,99 @@ struct ListNode *addTwoNumbers(struct ListNode *l1, struct ListNode *l2)
return head; 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;
}