From ffb78c5f4c49b389b57198e22a9628f5ece22b38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Borba?= Date: Sat, 19 Sep 2026 09:34:10 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=9F=A1=20Port=20problem=202=20(Add=20Two?= =?UTF-8?q?=20Numbers)=20from=20TypeScript=20to=20C++23?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaces typescript/addTwoNumbers.ts with solutions/0001-0100/0002-add-two-numbers/. Builds the result list directly while summing with carry in one pass, instead of staging both lists through intermediate arrays first. --- .../0001-0100/0002-add-two-numbers/README.md | 12 +++++ .../0002-add-two-numbers/solution.cpp | 29 +++++++++++ .../0002-add-two-numbers/solution.hpp | 16 ++++++ .../0001-0100/0002-add-two-numbers/test.cpp | 37 +++++++++++++ typescript/addTwoNumbers.ts | 52 ------------------- 5 files changed, 94 insertions(+), 52 deletions(-) create mode 100644 solutions/0001-0100/0002-add-two-numbers/README.md create mode 100644 solutions/0001-0100/0002-add-two-numbers/solution.cpp create mode 100644 solutions/0001-0100/0002-add-two-numbers/solution.hpp create mode 100644 solutions/0001-0100/0002-add-two-numbers/test.cpp delete mode 100644 typescript/addTwoNumbers.ts diff --git a/solutions/0001-0100/0002-add-two-numbers/README.md b/solutions/0001-0100/0002-add-two-numbers/README.md new file mode 100644 index 0000000..74a0b1e --- /dev/null +++ b/solutions/0001-0100/0002-add-two-numbers/README.md @@ -0,0 +1,12 @@ +# 2. Add Two Numbers + +[LeetCode problem 2](https://leetcode.com/problems/add-two-numbers/) + +- **Difficulty**: Medium +- **Tags**: linked-list, math, recursion + +## Description + +Given two non-empty linked lists representing two non-negative integers, the +digits stored in reverse order, add the two numbers and return the sum as a +linked list in the same reversed-digit format. diff --git a/solutions/0001-0100/0002-add-two-numbers/solution.cpp b/solutions/0001-0100/0002-add-two-numbers/solution.cpp new file mode 100644 index 0000000..c220947 --- /dev/null +++ b/solutions/0001-0100/0002-add-two-numbers/solution.cpp @@ -0,0 +1,29 @@ +#include "solution.hpp" + +namespace leetcode::p0002 { + +ListNode* solve(ListNode* l1, ListNode* l2) { + ListNode dummy; + ListNode* current = &dummy; + int carry = 0; + + while (l1 != nullptr || l2 != nullptr || carry != 0) { + int sum = carry; + if (l1 != nullptr) { + sum += l1->val; + l1 = l1->next; + } + if (l2 != nullptr) { + sum += l2->val; + l2 = l2->next; + } + + carry = sum / 10; + current->next = new ListNode(sum % 10); + current = current->next; + } + + return dummy.next; +} + +} // namespace leetcode::p0002 diff --git a/solutions/0001-0100/0002-add-two-numbers/solution.hpp b/solutions/0001-0100/0002-add-two-numbers/solution.hpp new file mode 100644 index 0000000..3f36eb2 --- /dev/null +++ b/solutions/0001-0100/0002-add-two-numbers/solution.hpp @@ -0,0 +1,16 @@ +#pragma once + +namespace leetcode::p0002 { + +struct ListNode { + int val; + ListNode* next; + + explicit ListNode(int value = 0, ListNode* nextNode = nullptr) : val(value), next(nextNode) {} +}; + +// optimized: O(max(n, m)) time, O(max(n, m)) space — build the result list +// directly while walking both lists and carrying, in one pass. +ListNode* solve(ListNode* l1, ListNode* l2); + +} // namespace leetcode::p0002 diff --git a/solutions/0001-0100/0002-add-two-numbers/test.cpp b/solutions/0001-0100/0002-add-two-numbers/test.cpp new file mode 100644 index 0000000..08b0c9b --- /dev/null +++ b/solutions/0001-0100/0002-add-two-numbers/test.cpp @@ -0,0 +1,37 @@ +#include + +#include + +#include "solution.hpp" + +namespace { + +leetcode::p0002::ListNode* fromVector(const std::vector& values) { + leetcode::p0002::ListNode dummy; + leetcode::p0002::ListNode* current = &dummy; + for (int value : values) { + current->next = new leetcode::p0002::ListNode(value); + current = current->next; + } + return dummy.next; +} + +std::vector toVector(leetcode::p0002::ListNode* head) { + std::vector result; + for (auto* node = head; node != nullptr; node = node->next) { + result.push_back(node->val); + } + return result; +} + +} // namespace + +TEST_CASE("Problem 2", "[p0002]") { + CHECK(toVector(leetcode::p0002::solve(fromVector({2, 4, 3}), fromVector({5, 6, 4}))) == + std::vector{7, 0, 8}); + CHECK(toVector(leetcode::p0002::solve(fromVector({0}), fromVector({0}))) == + std::vector{0}); + CHECK(toVector(leetcode::p0002::solve(fromVector({9, 9, 9, 9, 9, 9, 9}), + fromVector({9, 9, 9, 9}))) == + std::vector{8, 9, 9, 9, 0, 0, 0, 1}); +} diff --git a/typescript/addTwoNumbers.ts b/typescript/addTwoNumbers.ts deleted file mode 100644 index ed1571c..0000000 --- a/typescript/addTwoNumbers.ts +++ /dev/null @@ -1,52 +0,0 @@ -class ListNode { - val: number; - next: ListNode | null; - constructor(val?: number, next?: ListNode | null) { - this.val = val ?? 0; - this.next = next ?? null; - } -} - -function listNodeToArray(node: ListNode | null): number[] { - const result: number[] = []; - while (node) { - result.push(node.val); - node = node.next; - } - return result; -} - -function arrayToListNode(arr: number[]): ListNode | null { - if (arr.length === 0) return null; - const head = new ListNode(arr[0]); - let current = head; - for (let i = 1; i < arr.length; i++) { - current.next = new ListNode(arr[i]); - current = current.next; - } - return head; -} - -function addTwoNumbers( - l1: ListNode | null, - l2: ListNode | null -): ListNode | null { - const arr1 = listNodeToArray(l1); - const arr2 = listNodeToArray(l2); - - const result: number[] = []; - let carry = 0; - let i = 0; - - while (i < arr1.length || i < arr2.length || carry > 0) { - const digit1 = i < arr1.length ? arr1[i] : 0; - const digit2 = i < arr2.length ? arr2[i] : 0; - - const sum = digit1 + digit2 + carry; - result.push(sum % 10); - carry = Math.floor(sum / 10); - i++; - } - - return arrayToListNode(result); -}