From e1dedc14640d1e106864468e0668a1324debf10c Mon Sep 17 00:00:00 2001 From: Ivan Reshetnikov Date: Thu, 17 Nov 2022 21:13:04 +0500 Subject: [PATCH] Problem 2: add two numbers --- problems/2-add-two-numbers/Makefile | 1 + problems/2-add-two-numbers/lib.c | 45 +++++++++++++++ problems/2-add-two-numbers/lib.h | 8 +++ problems/2-add-two-numbers/main.c | 86 ++++++++++++++++++++++++++++ problems/2-add-two-numbers/readme.md | 9 +++ 5 files changed, 149 insertions(+) create mode 120000 problems/2-add-two-numbers/Makefile create mode 100644 problems/2-add-two-numbers/lib.c create mode 100644 problems/2-add-two-numbers/lib.h create mode 100644 problems/2-add-two-numbers/main.c create mode 100644 problems/2-add-two-numbers/readme.md diff --git a/problems/2-add-two-numbers/Makefile b/problems/2-add-two-numbers/Makefile new file mode 120000 index 0000000..25eaf27 --- /dev/null +++ b/problems/2-add-two-numbers/Makefile @@ -0,0 +1 @@ +../template/Makefile \ No newline at end of file diff --git a/problems/2-add-two-numbers/lib.c b/problems/2-add-two-numbers/lib.c new file mode 100644 index 0000000..e0ab860 --- /dev/null +++ b/problems/2-add-two-numbers/lib.c @@ -0,0 +1,45 @@ +#include +#include "lib.h" + +struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) { + struct ListNode* current = NULL; + struct ListNode* first_node = NULL; + + int reminder = 0; + while (l1 != NULL || l2 != NULL || reminder != 0) { + // Read digits + int a = 0; + int b = 0; + if (l1 != NULL) + a = l1->val; + if (l2 != NULL) + b = l2->val; + + // Move pointers to the next node + if (l1 != NULL) + l1 = l1->next; + if (l2 != NULL) + l2 = l2->next; + + // Calculate the result + int digit = (a + b + reminder) % 10; + reminder = (a + b + reminder) / 10; + + // Create the first node + if (current == NULL) { + current = malloc(sizeof(struct ListNode)); + first_node = current; + } + // Create regular node + else { + current->next = malloc(sizeof(struct ListNode)); + current = current->next; + } + + // Save the result + current->val = digit; + current->next = NULL; + } + + return first_node; +} diff --git a/problems/2-add-two-numbers/lib.h b/problems/2-add-two-numbers/lib.h new file mode 100644 index 0000000..425301f --- /dev/null +++ b/problems/2-add-two-numbers/lib.h @@ -0,0 +1,8 @@ +#pragma once + +struct ListNode { + int val; + struct ListNode *next; +}; + +struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2); diff --git a/problems/2-add-two-numbers/main.c b/problems/2-add-two-numbers/main.c new file mode 100644 index 0000000..fc7c750 --- /dev/null +++ b/problems/2-add-two-numbers/main.c @@ -0,0 +1,86 @@ +#include +#include +#include "lib.h" + +// Creates single node +struct ListNode* mkNode(int val) { + struct ListNode* node = malloc(sizeof(struct ListNode)); + node->val = val; + node->next = NULL; + return node; +} + +// Creates linked list +struct ListNode* mkList(int* nums, int length) { + struct ListNode* first = NULL; + struct ListNode* current = NULL; + for (int i=0; inext = mkNode(nums[i]); + current = current->next; + } + } + return first; +} + +// Returns 1 if linked lists are equal. +// Returns 0 otherwise. +int cmpLists(struct ListNode* a, struct ListNode* b) { + while (a != NULL || b != NULL) { + // Both nodes exist + if (a == NULL || b == NULL) + return 0; + + // Check values + if (a->val != b->val) + return 0; + + // Go to next node + a = a->next; + b = b->next; + } + + return 1; +} + +// Free allocated memory +void freeLinkedList(struct ListNode* list) { + struct ListNode* current; + while (list != NULL) { + current = list; + list = list->next; + free(current); + } +} + +int main() { + // First list + int a[] = {2, 4, 3}; + struct ListNode* list_a = mkList(a, 3); + + // Second list + int b[] = {5, 6, 4}; + struct ListNode* list_b = mkList(b, 3); + + // Expected sum + int c[] = {7, 0, 8}; + struct ListNode* list_c = mkList(c, 3); + + // Computed result + struct ListNode* res = addTwoNumbers(list_a, list_b); + + printf("Test 1: %d\n", cmpLists(res, list_c)); + + freeLinkedList(list_a); + freeLinkedList(list_b); + freeLinkedList(list_c); + freeLinkedList(res); + + return 0; +} diff --git a/problems/2-add-two-numbers/readme.md b/problems/2-add-two-numbers/readme.md new file mode 100644 index 0000000..90d68b1 --- /dev/null +++ b/problems/2-add-two-numbers/readme.md @@ -0,0 +1,9 @@ +# 2. Add two numbers + +* [Leetcode](https://leetcode.com/problems/add-two-numbers/) + +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. + +You may assume the two numbers do not contain any leading zero, except the number 0 itself.